hdu 1026 Ignatius and the Princess I(简单的bfs+输出路径)(WA的代码)
来源:互联网 发布:互联网行业工资 知乎 编辑:程序博客网 时间:2024/06/03 20:17
肯跌呀,过不了,自己想的样例和题目给的样例都能过,求大神 给指出wa在那里?
题目分析:Ignatius 在(0,0),princess在(N-1,M-1),每个格子可能是'X','.'或者是数字time(1-9),如果是数字则说明要消耗time时间在这个格子,每走一个格子需要多长时间
思路:bfs,用一个二维数组vis[i][j]记录走到(i,j)需要的时间,如果以前已经走到,再次走到时比vis[i][j]小,那么吧vis[i][j]置为当前值,并加入队列,否则不加入队列
输出路径的话,从vis[N-1][M-1]到vis[0][0]用一个栈或者数组记录路径 就可以了,
********看网上的代码,有人用 优先队列做的,不知道我是不是这里有问题???
#include<iostream>#include<cstdio>#include<memory.h>#include<queue>using namespace std;char maze[1100][1100];int vis[1100][1100];int N,M;int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};struct node{ int x,y; int time;}arr[11000];queue<node>q;bool IN(int x,int y){ if(0<=x&&x<N && 0<=y&&y<M) return true; else return false;}int BFS(){ while(!q.empty()) {node t=q.front();q.pop();node cur;int ans=-1;for(int i=0;i<4;i++){cur.x=t.x+dir[i][0];cur.y=t.y+dir[i][1];cur.time=t.time+1;if(IN(cur.x,cur.y) && maze[cur.x][cur.y]!='X'){if(vis[cur.x][cur.y]==-1){if(maze[cur.x][cur.y]!='.'){vis[cur.x][cur.y]=cur.time+(maze[cur.x][cur.y]-'0');cur.time+=(maze[cur.x][cur.y]-'0');}elsevis[cur.x][cur.y]=cur.time; q.push(cur);}else{if(maze[cur.x][cur.y]!='.'){if(cur.time+(maze[cur.x][cur.y]-'0')<vis[cur.x][cur.y]) {vis[cur.x][cur.y]=cur.time+(maze[cur.x][cur.y]-'0');cur.time+=(maze[cur.x][cur.y]);q.push(cur); }}else{if(cur.time<vis[cur.x][cur.y]) {vis[cur.x][cur.y]=cur.time;q.push(cur); }}}}} } return vis[N-1][M-1];}void print(){ int ans=vis[N-1][M-1]; node t; t.x=N-1; t.y=M-1; int len=0; arr[len++]=t; while(ans>vis[0][0]) { if('1'<=maze[t.x][t.y] && maze[t.x][t.y]<='9') { ans=ans-(maze[t.x][t.y]-'0'); } ans-=1; node cur; for(int i=0;i<4;i++) { cur.x=t.x+dir[i][0]; cur.y=t.y+dir[i][1]; if(IN(cur.x,cur.y)&&vis[cur.x][cur.y]==ans) { t=cur; arr[len].x=t.x; arr[len++].y=t.y; break; } } } int cnt=1; if('1'<=maze[0][0] &&maze[0][0]<='9') { int temp=maze[0][0]-'0'; for(int i=1;i<=temp;i++) printf("%ds:FIGHT AT (0,0)\n",cnt++); } for(int i=len-2;i>=0;i--) { if(i<len-1) { printf("%ds:(%d,%d)->(%d,%d)\n",cnt++,arr[i+1].x,arr[i+1].y,arr[i].x,arr[i].y); } if('1'<=maze[arr[i].x][arr[i].y] && maze[arr[i].x][arr[i].y]<='9') { int temp=(maze[arr[i].x][arr[i].y]-'0'); for(int j=1;j<=temp;j++) { printf("%ds:FIGHT AT (%d,%d)\n",cnt++,arr[i].x,arr[i].y); } } }}int main(){ while(scanf("%d %d",&N,&M)!=EOF) { getchar(); for(int i=0;i<N;i++) gets(maze[i]); memset(vis,-1,sizeof(vis)); vis[0][0]=0; while(!q.empty()) q.pop(); node start; start.x=0,start.y=0; if('1'<=maze[0][0]&&maze[0][0]<='9') start.time=maze[0][0]-'0'; else start.time=0; vis[0][0]=start.time; q.push(start); int ans=BFS(); if(ans==-1) printf("God please help our poor hero.\n"); /*else if(ans==1) { printf("It takes %d second to reach the target position, let me show you the way.\n",ans); print(); }*/ else { printf("It takes %d seconds to reach the target position, let me show you the way.\n",ans); print(); } printf("FINISH\n"); } return 0;}/***5 65XX.1...X.2.2...X....XX.XXXXX.*/
- hdu 1026 Ignatius and the Princess I(简单的bfs+输出路径)(WA的代码)
- HDU-1026 Ignatius and the Princess I(BFS) 带路径的广搜
- 杭电 hdu 1026 Ignatius and the Princess I(BFS+优先队列+墨迹人的输出)
- hdoj 1026 Ignatius and the Princess I【bfs&&路径输出】
- HDU-1026 Ignatius and the Princess I (BFS)
- hdu 1026 Ignatius and the Princess I(BFS)
- hdu 1026 Ignatius and the Princess I(BFS)
- hdu 1026 Ignatius and the Princess I(bfs)
- HDU 1026 Ignatius and the Princess I (BFS)
- HDU - 1026 - Ignatius and the Princess I(bfs)
- HDU 1026 Ignatius and the Princess I(BFS)
- HDOJ 1026 Ignatius and the Princess I (BFS + 优先队列 + 输出路径)
- HDU 1026 Ignatius and the Princess I && BFS+记录路径
- HDU 1026 Ignatius and the Princess I BFS打印路径
- HDU 1026 Ignatius and the Princess I(BFS+记录路径)
- HDU 1026Ignatius and the Princess I(bfs+记录路径)
- HDU 1026 Ignatius and the Princess I (BFS+记录路径)
- HDU 1026 Ignatius and the Princess I 【BFS(优先队列)+路径存储】
- ubuntu tomcat
- Vim+ctags
- 2007 Office system 檔案格式 MIME 類型
- Knockout绑定语法
- 曲径通幽
- hdu 1026 Ignatius and the Princess I(简单的bfs+输出路径)(WA的代码)
- 文件服务器之一:NFS 服务器
- C++建议
- CityEngine Web Scene如何在Tomcat下部署
- 关于图片存储格式的整理(JPEG)
- 理解BSTR数据类型 神奇的BSTR
- 托管代码遇到的问题暂无解决办法
- 网页ActiveX插件开发可能遇到的几个问题
- [易飞9.0]任务中心无法通过1000+压力测试?