一天一个错误,找死人了;
有些东西,看似没错;可是online juge 就是给你报错,那99% 你就是错了
考虑的东西不全面,错了也活该;
刚开始visit 弄了二维的,觉得没错,因为自己拿着k=2举例子,其实你换个数,比如k=5 你就会发现,我们这个visit还需要加一维
visit[x][y][z] 表示的是点 (x,y) 在z状态下是否已经经历过;这个z为 时间对k的余数; 比如对于5来说,在同一个点,t=10 和t=5时应该视为一样的;
对接下去他可能经过#的地方造成的影响是一样的;
以下是AC代码;
如果你将//××× 的注释掉,再将下面注释的取消注释,你觉得对了吗;
我开始也以为是对的;
后来发现,我忽略了一个点,起点。所以如果要那样做,必须:if(maze[b.x][b.y]=='.'||maze[b.x][b.y]=='Y')
或者你输入了数据记录了开始点之后,把Y改为.也行的;
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
#define maxn 100+3
struct point
{
int x;
int y;
int t;
} st;
int n,m,k;
char maze[maxn][maxn];
bool visit[maxn][maxn][15];
int xx[] = {1,-1,0,0};
int yy[] = {0,0,1,-1};
queue <point> q;
inline bool check(int x, int y,int z)
{
if(x<1||y<1||x>n||y>m||visit[x][y][z%k])
return false;
if(maze[x][y]=='#' && z%k!=0)//×××
return false;//×××
return true;
}
void BFS()
{
while(!q.empty())
q.pop();
q.push(st);
visit[st.x][st.y][0] = true;
point a,b;
while(!q.empty())
{
a = q.front();
q.pop();
for(int i = 0; i < 4; i++)
{
b.x = a.x + xx[i];
b.y = a.y + yy[i];
b.t = a.t + 1;
if(check(b.x,b.y,b.t))
{
if(maze[b.x][b.y]=='G')
{
printf("%d\n",b.t);
return ;
}
// if(maze[b.x][b.y]=='.')
// {
q.push(b);
visit[b.x][b.y][b.t%k] = true;
// }
// if(maze[b.x][b.y]=='#')
// {
// if(b.t%k==0)
// {
// q.push(b);
// visit[b.x][b.y][b.t%k] = true;
// }
// }
}
}
}
puts("Please give me another chance!");
return ;
}
int main()
{
int T;
cin>>T;
while(T--)
{
cin>>n>>m>>k;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
cin>>maze[i][j];
if(maze[i][j]=='Y')
{
st.x = i;
st.y = j;
st.t = 0;
}
}
}
memset(visit,false,sizeof(visit));
BFS();
}
}