题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102
题意:中文题就不翻译了,在T时刻前找到也行。
(在变量设置的时候T和cnt一开始都是T......然后wa了好几次才发现。太蠢了)
简单的bfs
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
const int maxn = 15;
int n, m, cnt;
char Map[2][maxn][maxn];
int x[] = {0, 1, 0, -1};
int y[] = {1, 0, -1, 0};
struct P
{
int sig;
int x;
int y;
int t;
P(){t = 0; }
P(int X, int Y, int Sig, int T):
x(X), y(Y), sig(Sig), t(T)
{}
} node;
int BFS(P s)
{
s.t = 0;
bool vis[2][maxn][maxn];
memset(vis, 0, sizeof(vis));
queue<P> Q;
Q.push(s);
vis[s.sig][s.x][s.y] = 1;
while(!Q.empty())
{
P cur = Q.front();
Q.pop();
if(Map[cur.sig][cur.x][cur.y] == 'P')
{
return cur.t;
}
for(int i = 0; i < 4; i++)
{
int X = cur.x + x[i];
int Y = cur.y + y[i];
int Sig = cur.sig;
int T = cur.t + 1;
if(X < 0 || X >= n || Y < 0 || Y >= m ||
(vis[Sig][X][Y] == 1 && Map[Sig][X][Y] == '.') || Map[Sig][X][Y] == '*' ||
(Map[Sig][X][Y] == '#' && Map[Sig ^ 1][X][Y] == '*') ||
(Map[Sig][X][Y] == '#' && Map[Sig ^ 1][X][Y] == '#'))
continue;
P tmp(X, Y, Sig, T);
if(Map[Sig][X][Y] == '#')
tmp.sig ^= 1;
if(vis[tmp.sig][tmp.x][tmp.y] == 0)
{
Q.push(tmp);
vis[tmp.sig][tmp.x][tmp.y] = 1;
}
}
}
return 99999;
}
int main()
{
int T;
cin >> T;
while(T--)
{
cin >> n >> m >> cnt;
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < n; j++)
{
scanf("%s", Map[i][j]);
for(int k = 0; k < m; k++)
{
if(Map[i][j][k] == 'S')
{
node.x = j;
node.y = k;
node.sig = i;
}
}
}
}
int ans = BFS(node);
if(ans <= cnt)
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
}
return 0;
}