https://cn.vjudge.net/problem/UVA-11624
这其实是一个很水的题!!但我为啥能卡这么久呢!!!
忽略了题目一个问题!F的位置有多个!!!
解题的大致思路:因为他是交替进行的,于是就能用z标记一下,
然后分别标记一起进行BFS。
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
const int MAX = 0x3f3f3f3f;
int n, m;
char st[1010][1010];
int next1[4]= {1, -1, 0, 0};
int next2[4]= {0, 0, 1, -1};
struct node
{
int x, y, z;
int step;
node(int xx, int yy, int zz, int ss)
{
x = xx;
y = yy;
z = zz;
step = ss;
}node(){}
}t, f;
int dis[1010][1010];
queue<node>q;
void bfs(int a, int b)
{
dis[a][b] = 0;
q.push(node(a, b, 0, 0));
while(!q.empty())
{
f = q.front();q.pop();
if(f.z==0)
if(f.x==0 || f.x==n-1 || f.y==0 || f.y==m-1)
{
printf("%d\n", f.step+1);
return;
}
for(int i=0; i<4; i++)
{
int dx = f.x + next1[i];
int dy = f.y + next2[i];
int step = f.step + 1;
int dz = f.z;
if(dx>=0&&dx<n&&dy>=0&&dy<m&&st[dx][dy]=='.')
{
if(dz)
{
st[dx][dy] = 'F';
q.push(node(dx, dy, dz, step));
}
else if(dis[dx][dy] > dis[f.x][f.y]+1)
{
dis[dx][dy] = dis[f.x][f.y]+1;
q.push(node(dx, dy, dz, step));
}
}
}
}
printf("IMPOSSIBLE\n");
return;
}
int main()
{
int T, a, b ,c, d;
scanf("%d", &T);
while(T--)
{
while(!q.empty()) q.pop();
memset(st, '\0', sizeof(st));
scanf("%d%d", &n, &m);
for(int i=0; i<n; i++)
{
scanf("%s", st[i]);
for(int j=0; j<m; j++)
{
dis[i][j] = MAX;
if(st[i][j]=='J')
{
a = i;
b = j;
}
if(st[i][j]=='F')
{
c = i;
d = j;
q.push(node(c, d, 1, 0));
}
}
}
bfs(a, b);
}
return 0;
}