一天一個錯誤,找死人了;
有些東西,看似沒錯;可是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();
}
}