真是頭大。老是犯些超級低級的錯誤;
數組我存的時候下標寫的是 1 開始,
我用check函數判斷的時候寫的確實 a.x<0||b.x<0
一個錯誤找一天。
唉。。。說多了都是淚;
發現錯了就重新拍代碼,拍了還是犯這樣的錯; 絕對是太粗心了;
///////////////////////////////////
********************
思路:
對每個點 進行走路和飛行的判斷和遍曆;
visit 三位元組 存放當前點 當前magic是否遍曆過。
********************
/////////////////////////////////
/* *Author ID:fuqiang11 *Problem ID:HDU 2653 *Submit Time:2013/7/26 *Algorithm :BFS + priority_queue*/ #include <iostream>#include <cstdio>#include <string>#include <queue>#include <cstring>using namespace std;#define maxn 82int N,M,T,P;struct point{ int x; int y; int t; int p; friend bool operator < (point a, point b) { return a.t > b.t; }}st,ed;char maze[maxn][maxn];int visit[maxn][maxn][maxn];int xx[] = {0,0,1,-1};int yy[] = {1,-1,0,0};bool check(point a){ if(a.x<1||a.y<1||a.x>N||a.y>M||maze[a.x][a.y]=='#'||visit[a.x][a.y][a.p]) return false; return true;}priority_queue <point> q;void BFS(){ while(!q.empty()) //release queue q.pop(); q.push(st); point a,b; while(!q.empty()) { a = q.top(); q.pop(); if(a.t > T) break; if(a.x==ed.x && a.y==ed.y) { printf("Yes, Yifenfei will kill Lemon at %d sec.\n",a.t); return ; } for(int i = 0; i < 4; i++) { b.x = a.x + xx[i]; b.y = a.y + yy[i]; b.t = a.t; b.p = a.p; if(check(b)) { if(maze[a.x][a.y]!='@' && maze[b.x][b.y]!='@') //能走能飛的情況 選擇走 { visit[b.x][b.y][b.p] = 1; b.t += 2; q.push(b); b.t -= 2; } if(!visit[b.x][b.y][b.p-1] && b.p>0)//選擇飛 { b.p -= 1; b.t += 1; visit[b.x][b.y][b.p] = 1; q.push(b); } } } }//end while printf("Poor Yifenfei, he has to wait another ten thousand years.\n"); return ;}int main(){ int Case = 1; while(scanf("%d%d%d%d",&N,&M,&T,&P)!=EOF) { int i,j; for (i=1;i<=N;i++) scanf("%s",maze[i]+1); for (i=1;i<=N;i++) { for (j=1;j<=M;j++) { if (maze[i][j]=='Y') {st.x=i; st.y=j;} if (maze[i][j]=='L') {ed.x=i; ed.y=j;} } } printf("Case %d:\n",Case++); memset(visit,0,sizeof(visit)); st.p = P; st.t = 0; visit[st.x][st.y][st.p] = 1; BFS(); }}