uestc 一个简单的迷宫问题
2015-01-31 23:39
232 查看
我不知道化成网格线什么意思啊。。。 :(
如果我写一步一搜的话肯定很麻烦。。
大牛代码 理解以上两点之后再重新做。。sigh~
如果我写一步一搜的话肯定很麻烦。。
大牛代码 理解以上两点之后再重新做。。sigh~
#include <iostream> #include <stdio.h> #include <queue> #include <string.h> using namespace std; int n,m; int map[55][55]; bool vis[4][55][55]; struct node { int x,y; int dr; //方向 int step; bool operator<(const node &t)const { return t.step<step; } }S,E; bool judge(int x,int y) { if(x>=0 && x<n-1 && y>=0 && y<m-1) { return true; } return false; } int bfs() { priority_queue<node>q; q.push(S); node now,next,temp; memset(vis, false, sizeof(vis)); vis[S.dr][S.x][S.y]=true; while (!q.empty()) { next=now=temp=q.top(); q.pop(); if(now.x==E.x && now.y==E.y) { return now.step; } for (int i=-1; i<4; i++) { if(i<=0) { now.step=temp.step+1; if(i==-1) { now.dr=(1+temp.dr)%4; //右转 } if(i==0) { now.dr=(3+temp.dr)%4; //左转 } if(!vis[now.dr][now.x][now.y]) { vis[now.dr][now.x][now.y]=true; q.push(now); } } else { next.step=temp.step+1; if(next.dr==0) //向上走 { next.x=temp.x-i; } if(next.dr==1) //向右走 { next.y=temp.y+i; } if(next.dr==2) //向下走 { next.x=temp.x+i; } if(next.dr==3) //向左走 { next.y=temp.y-i; } //走i步过程中有一步走不通就不得行了 if(!judge(next.x, next.y) || map[next.x][next.y]==1) { break; } if(!vis[next.dr][next.x][next.y]) { q.push(next); vis[next.dr][next.x][next.y]=true; } } } } return -1; } int main() { while (scanf("%d%d",&n,&m) && (n||m)) { for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { scanf("%d",&map[i][j]); //把n*m的矩阵化成(n-1)*(m-1)的网格线矩阵。 if(map[i][j]==1) { map[i][j]=1; if(i>=1) { map[i-1][j]=1; if(j>=1) { map[i-1][j-1]=1; } } if(j>=1) { map[i][j-1]=1; if(i>=1) { map[i-1][j-1]=1; } } } } } scanf("%d%d%d%d%d",&S.x,&S.y,&E.x,&E.y,&S.dr); S.x--,S.y--,E.x--,E.y--; S.step=0; printf("%d\n",bfs()); } return 0; }
相关文章推荐
- UESTC 853 一个简单的走迷宫问题
- ~~把我搞晕了!一个简单的C#语法问题
- 又一个困扰我的简单问题。
- 数据结构中关于链表的一个简单问题
- 一个数独问题的算法(已更新,提供一个简单算法,欢迎拍砖)
- 《Flash Communication Server: 建立一个简单的聊天室》中引用问题的解决
- 一个简单但值得深思的问题!
- 一个简单的问题
- 一个简单的移植性的问题
- [转载]一个简单问题引起的“血案”(白云黄鹤)
- 今天在国外论坛上为别人解决一个简单问题
- 郁闷的跨域名问题(一个简单的高校bbs十大聚合页)
- 项目中一个简单SQL查询问题,已经解决了,留个纪念,非常感谢帮我的朋友们。
- 一个简单的怪问题
- 学习ejb并配置一个简单的helloEjb是遇到问题后总结的经验。
- 一个简单的初始化问题
- 一个简单的怪问题
- 今天调试一个WebPart的简单程序出现的问题
- 一个简单的怪问题
- 今天的问题:一个简单的例子,请帮我解开“接口实现Java‘隐藏实现细目’”的迷惑。