题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102
就是一简单的纯裸的BFS,只不过需要再加上一维
代码如下:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include<algorithm>
using namespace std;
int n, m, vis[3][20][20], t;
char map[3][20][20];
int jx[]= {0, 0, 1, -1};
int jy[]= {1, -1, 0, 0};
struct node
{
int x, y, ans, z;
} q[10000000];
void bfs()
{
node f1, f2;
int s=0, e=0, i, j;
f1.x=0;
f1.y=0;
f1.ans=0;
f1.z=0;
q[s++]=f1;
vis[0][0][0]=1;
while(s>e)
{
f1=q[e++];
//printf("%d %d %d %d\n",f1.x, f1.y, f1.z, f1.ans);
if(map[f1.z][f1.x][f1.y]=='P')
{
printf("YES\n");
return ;
}
for(i=0; i<4; i++)
{
f2.x=f1.x+jx[i];
f2.y=f1.y+jy[i];
if(map[f1.z][f2.x][f2.y]=='#')
{
f2.z=1-f1.z;
}
else
f2.z=f1.z;
if(f2.x>=0&&f2.x<n&&f2.y>=0&&f2.y<m&&!vis[f2.z][f2.x][f2.y]&&map[f1.z][f1.x][f1.y]!='*'&&f1.ans+1<=t)
{
f2.ans=f1.ans+1;
vis[f2.z][f2.x][f2.y]=1;
q[s++]=f2;
}
}
}
printf("NO\n");
}
int main()
{
int T, i, j, k;
scanf("%d",&T);
while(T--)
{
memset(vis,0,sizeof(vis));
scanf("%d%d%d",&n,&m,&t);
for(i=0; i<n; i++)
{
scanf("%s",map[0][i]);
}
getchar();
for(i=0; i<n; i++)
{
scanf("%s",map[1][i]);
}
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
if(map[1][i][j]=='#'&&map[0][i][j]=='*')
{
map[1][i][j]='*';
}
if(map[0][i][j]=='#'&&map[1][i][j]=='*')
{
map[0][i][j]='*';
}
else if(map[1][i][j]=='#'&&map[0][i][j]=='#')
{
map[1][i][j]=map[0][i][j]='*';
}
}
}
bfs();
}
return 0;
}