HDU 2102 BFS A计划
2016-07-30 21:14
330 查看
Problem Description
可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老。年迈的国王正是心急如焚,告招天下勇士来拯救公主。不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出。
现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。
Input
输入的第一行C表示共有C个测试数据,每个测试数据的前一行有三个整数N,M,T。 N,M迷宫的大小N*M(1 <= N,M <=10)。T如上所意。接下去的前N*M表示迷宫的第一层的布置情况,后N*M表示迷宫第二层的布置情况。
Output
如果骑士们能够在T时刻能找到公主就输出“YES”,否则输出“NO”。
Sample Input
1
5 5 14
S*#*.
.#...
.....
****.
...#.
..*.P
#.*..
***..
...*.
*.#..
Sample Output
YES
AC代码
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
int n,m,T;
char first[15][15],second[15][15]; //分别存储两层楼
int vis[2][15][15];
struct point
{
int x,y,step,type; //type为0表示第0层,为1表示第一层
}p[105];
queue<point> que;
bool inside(int x,int y)
{
i
a617
f(x>=n||x<0) return false;
if(y>=m||y<0) return false;
return true;
}
bool bfs()
{
while(!que.empty()) que.pop(); // 初始化
memset(vis,0,sizeof(vis));
point t;
t.x=0;t.y=0;t.step=0;t.type=0;
vis[0][0][0]=1;
que.push(t); //骑士位置入队
while(!que.empty()){
point u=que.front();
if(u.type==0){
if(first[u.x][u.y]=='P') //找到公主
if(u.step<=T)
return true;
else
return false;
}
else{
if(second[u.x][u.y]=='P')
if(u.step<=T)
return true;
else
return false;
}
que.pop();
int cx=u.x,cy=u.y;
for(int i=0;i<4;i++){
point temp;
temp.x=cx+dx[i];
temp.y=cy+dy[i];
temp.step=u.step+1;
if(inside(temp.x,temp.y)&&!vis[u.type][temp.x][temp.y]){
if(u.type==0){ //在第0层
if(first[temp.x][temp.y]=='.'){
temp.type=u.type;
vis[u.type][temp.x][temp.y]=1;
que.push(temp); //可行则入队
}
else if(first[temp.x][temp.y]=='#'){
vis[u.type][temp.x][temp.y]=1;
temp.type=!u.type;
que.push(temp); //传送门,另一层相应位置入队
}
else if(first[temp.x][temp.y]=='P'){ //找到公主
if(temp.step<=T) return true;
else return false;
}
}
else{ //在第一层
if(second[temp.x][temp.y]=='.'){ //操作同在第0层是类似
temp.type=u.type;
vis[u.type][temp.x][temp.y]=1;
que.push(temp);
}
else if(second[temp.x][temp.y]=='#'){
temp.type=!u.type;
vis[u.type][temp.x][temp.y]=1;
que.push(temp);
}
else if(second[temp.x][temp.y]=='P'){
if(temp.step<=T) return true;
else return false;
}
}
}
}
}
return false;//找不到公主
}
int main()
{
int t;
char temp[15];
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&m,&T);
for(int i=0;i<n;i++){
scanf("%s",&temp);
for(int j=0;j<m;j++){
first[i][j]=temp[j];
}
}
for(int i=0;i<n;i++){
scanf("%s",&temp);
for(int j=0;j<m;j++){
second[i][j]=temp[j];
if(first[i][j]=='*'&&second[i][j]=='#') //避免到达传送门时判断是否会撞墙,注意若另一层也是传送门则也是不可行的
second[i][j]='*';
if(first[i][j]=='#'&&second[i][j]=='*')
first[i][j]='*';
if(first[i][j]=='#'&&second[i][j]=='#')
first[i][j]=second[i][j]='*';
}
}
if(bfs()) printf("YES\n");
else printf("NO\n");
}
return 0;
}
可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老。年迈的国王正是心急如焚,告招天下勇士来拯救公主。不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出。
现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。
Input
输入的第一行C表示共有C个测试数据,每个测试数据的前一行有三个整数N,M,T。 N,M迷宫的大小N*M(1 <= N,M <=10)。T如上所意。接下去的前N*M表示迷宫的第一层的布置情况,后N*M表示迷宫第二层的布置情况。
Output
如果骑士们能够在T时刻能找到公主就输出“YES”,否则输出“NO”。
Sample Input
1
5 5 14
S*#*.
.#...
.....
****.
...#.
..*.P
#.*..
***..
...*.
*.#..
Sample Output
YES
AC代码
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
int n,m,T;
char first[15][15],second[15][15]; //分别存储两层楼
int vis[2][15][15];
struct point
{
int x,y,step,type; //type为0表示第0层,为1表示第一层
}p[105];
queue<point> que;
bool inside(int x,int y)
{
i
a617
f(x>=n||x<0) return false;
if(y>=m||y<0) return false;
return true;
}
bool bfs()
{
while(!que.empty()) que.pop(); // 初始化
memset(vis,0,sizeof(vis));
point t;
t.x=0;t.y=0;t.step=0;t.type=0;
vis[0][0][0]=1;
que.push(t); //骑士位置入队
while(!que.empty()){
point u=que.front();
if(u.type==0){
if(first[u.x][u.y]=='P') //找到公主
if(u.step<=T)
return true;
else
return false;
}
else{
if(second[u.x][u.y]=='P')
if(u.step<=T)
return true;
else
return false;
}
que.pop();
int cx=u.x,cy=u.y;
for(int i=0;i<4;i++){
point temp;
temp.x=cx+dx[i];
temp.y=cy+dy[i];
temp.step=u.step+1;
if(inside(temp.x,temp.y)&&!vis[u.type][temp.x][temp.y]){
if(u.type==0){ //在第0层
if(first[temp.x][temp.y]=='.'){
temp.type=u.type;
vis[u.type][temp.x][temp.y]=1;
que.push(temp); //可行则入队
}
else if(first[temp.x][temp.y]=='#'){
vis[u.type][temp.x][temp.y]=1;
temp.type=!u.type;
que.push(temp); //传送门,另一层相应位置入队
}
else if(first[temp.x][temp.y]=='P'){ //找到公主
if(temp.step<=T) return true;
else return false;
}
}
else{ //在第一层
if(second[temp.x][temp.y]=='.'){ //操作同在第0层是类似
temp.type=u.type;
vis[u.type][temp.x][temp.y]=1;
que.push(temp);
}
else if(second[temp.x][temp.y]=='#'){
temp.type=!u.type;
vis[u.type][temp.x][temp.y]=1;
que.push(temp);
}
else if(second[temp.x][temp.y]=='P'){
if(temp.step<=T) return true;
else return false;
}
}
}
}
}
return false;//找不到公主
}
int main()
{
int t;
char temp[15];
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&m,&T);
for(int i=0;i<n;i++){
scanf("%s",&temp);
for(int j=0;j<m;j++){
first[i][j]=temp[j];
}
}
for(int i=0;i<n;i++){
scanf("%s",&temp);
for(int j=0;j<m;j++){
second[i][j]=temp[j];
if(first[i][j]=='*'&&second[i][j]=='#') //避免到达传送门时判断是否会撞墙,注意若另一层也是传送门则也是不可行的
second[i][j]='*';
if(first[i][j]=='#'&&second[i][j]=='*')
first[i][j]='*';
if(first[i][j]=='#'&&second[i][j]=='#')
first[i][j]=second[i][j]='*';
}
}
if(bfs()) printf("YES\n");
else printf("NO\n");
}
return 0;
}
相关文章推荐
- hdu 2102 A计划(BFS,基础)
- hdu 2102 A计划(双层BFS)(详解)
- hdu 2102 A计划(bfs)
- hdu 2102 A计划(bfs)
- HDU 2102 A计划(BFS)
- HDU-2102 A计划 BFS
- HDU 2102 A计划(三维BFS)
- HDU 2102 A计划 BFS求解
- HDU 2102--A计划【BFS】
- hdu 2102 A计划 BFS
- hdu 2102 A计划_bfs搜索
- hdu 2102 A计划(bfs)
- hdu 2102 A计划 BFS
- hdu 2102 A计划-bfs
- Hdu 2102 A计划 [Bfs]
- HDU 2102 A计划(简单3维BFS)
- HDU 2102 A计划 双层BFS
- HDU 2102 A计划 bfs
- hdu - 2102 A计划 (简单bfs)
- HDU 2102 A计划(三维BFS)