非常鬱悶的–hdu2653(有心的朋友幫幫忙)

來源:互聯網
上載者:User

這個題真的非常鬱悶,錯了一下午、、、、、

明明思路非常的正確,怎麼修改就是WA、、、

題意:

由起點到終點的最小時間。需要注意的是如果遇見@則此次只能fly並且@的下一步也只能fly

分析:

看到題就明白是個有特殊條件的BFS。寫好後一直WA,才知道每個狀態需要記錄power,三維數組選項組。寫好後就開始了悲劇的找錯時光、、、、

漫長的一下午過去了,晚上也好一會了,還是沒有發現究竟錯在哪、、、、找了個跟自己的思路一樣的AC的代碼。一點點對照,仍舊沒有找到原因,持續鬱悶中

到底哪錯啦??????????

My Code:

#include<stdio.h>#include<queue>#include<iostream>#include<string.h>using namespace std;char N[81][81];bool mark[81][81][81];int A,B,T;int matrix[4][2]={0,1,0,-1,1,0,-1,0};struct info{int a;int b;int step;int p;//剩餘能量};info e;std::queue<info> Q;bool operator<(info x,info y){return x.step>y.step;}int BFS(info x){info y;Q.push(x);while(!Q.empty()){x=Q.front();Q.pop();if(x.step>T) return -1;if(N[x.a][x.b]=='L') return x.step;for(int i=0;i<4;i++){y.a=x.a+matrix[i][0];y.b=x.b+matrix[i][1];if(y.a>=0&&y.a<A&&y.b>=0&&y.b<B&&N[y.a][y.b]!='#') {//飛行if(mark[y.a][y.b][x.p-1]==0&&x.p>0) {y.p=x.p-1; y.step=x.step+1;mark[y.a][y.b][y.p]=1;Q.push(y);}//walkif(mark[y.a][y.b][x.p]==0&&N[y.a][y.b]!='@'&&N[x.a][x.b]!='@') {y.p=x.p;y.step=x.step+2;mark[y.a][y.b][y.p]=1;Q.push(y);}}}}return -1;}int main(){int cnt=0;int P;while(~scanf("%d%d%d%d",&A,&B,&T,&P)){memset(mark,0,sizeof(mark));memset(N,0,sizeof(N));getchar();printf("Case %d:\n",++cnt);int i,j,k;info X;for(i=0;i<A;i++){for(j=0;j<B;j++){scanf("%c",&N[i][j]);if(N[i][j]=='Y') {X.a=i;X.b=j;X.step=0;X.p=P;mark[i][j][P]=1;}}getchar();}int s=BFS(X);if(s<0) printf("Poor Yifenfei, he has to wait another ten thousand years.\n");else printf("Yes, Yifenfei will kill Lemon at %d sec.\n",s);}return 0;}

 

AC的代碼:【代碼來自http://blog.csdn.net/swm8023/article/details/6765219】

#include <cstdio>#include <string.h>#include <queue>using namespace std;struct pos{int r,c,p,s;pos(int w,int x,int y,int z){r=w,c=x,p=y,s=z;} };int n,m,p,t,sr,sc,er,ec;bool vis[82][82][82];int dr[]={0,0,1,-1},dc[]={1,-1,0,0};char maze[82][82];int bfs(){queue<pos> q;memset(vis,0,sizeof vis);q.push(pos(sr,sc,p,0));vis[sr][sc][p]=true;while(!q.empty()){pos tp=q.front();q.pop();//reach in timeif(tp.r==er&&tp.c==ec&&tp.s<=t)return tp.sfor(int i=0;i<4;i++){int nr=tp.r+dr[i],nc=tp.c+dc[i];if(nr<1||nr>n||nc<1||nc>m||maze[nr][nc]=='#')continue;//FLY  if(tp.p>0&&!vis[nr][nc][tp.p-1]){q.push(pos(nr,nc,tp.p-1,tp.s+1));vis[nr][nc][tp.p-1]=true;}//WALKif(maze[tp.r][tp.c]=='@'||maze[nr][nc]=='@')continue;//CAN`T WALKif(!vis[nr][nc][tp.p]){q.push(pos(nr,nc,tp.p,tp.s+2));vis[nr][nc][tp.p]=true;}}}return -1; }int main(){int cas=1;while(scanf("%d%d%d%d",&n,&m,&t,&p)!=EOF){for(int i=1;i<=n;i++){scanf("%s",maze[i]+1);for(int j=1;j<=m;j++){if(maze[i][j]=='Y')sr=i,sc=j;if(maze[i][j]=='L')er=i,ec=j;}}int r=bfs();printf("Case %d:\n",cas++);if(r==-1)printf("Poor Yifenfei, he has to wait another ten thousand years.\n");else printf("Yes, Yifenfei will kill Lemon at %d sec.\n",r);}return 0;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.