解题思路:一道简单题,却WA了十几发,犯几个低级错误。还是不能急躁,
内心要平静,具体分析见代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn = ;
char mapp[maxn][maxn][];
int dir[][] = {, , -, , , , , -};
int n, m, t, c; struct node{
int x, y;
int f;
int step;
}s, e; queue<node> q; int bfs()
{
while(!q.empty()) q.pop(); //没加这步WA了好多发
q.push(s); while(!q.empty())
{
s = q.front(); q.pop();
//debug时可打印路径
//printf("s.x = %d, s.y = %d, s.step = %d, s.flag = %d\n", s.x, s.y, s.step, s.flag); for(int i = ; i < ; i++)
{
e.x = s.x + dir[i][];
e.y = s.y + dir[i][];
e.step = s.step + ;
e.f = s.f; if(e.step > t || mapp[e.x][e.y][e.f] == '*') continue;//超过时间或不能走
if(mapp[e.x][e.y][e.f] == '#')
{
mapp[e.x][e.y][e.f] = '*'; //标记为已走过
e.f = - e.f; //穿越到另一层
//另一层若为#或*则标记为不能走
if(mapp[e.x][e.y][e.f] == '#' || mapp[e.x][e.y][e.f] == '*')
{
mapp[e.x][e.y][e.f] = '*';
continue; //必不可少
}
} if(mapp[e.x][e.y][e.f] == 'P') return ; //找到公主
mapp[e.x][e.y][e.f] = '*'; //标记为已走过
q.push(e);
}
}
return ; //规定时间没找到
} int main()
{
scanf("%d", &c);
while(c --)
{
memset(mapp, '*', sizeof(mapp));
scanf("%d %d %d", &n, &m, &t); for(int i = ; i <= n; i++) for(int j = ; j <= m; j++)
scanf(" %c", &mapp[i][j][]); for(int i = ; i <= n; i++) for(int j = ; j <= m; j++)
scanf(" %c", &mapp[i][j][]); s.x = , s.y = , s.step = , s.f = ;
if(bfs()) printf("YES\n");
else printf("NO\n");
}
return ;
}