UESTC 853 一个简单的走迷宫问题
来源:互联网 发布:江苏破获网络诈骗团伙 编辑:程序博客网 时间:2024/05/17 00:10
这是一个网格迷宫问题,地图的左上角为
Input
题目有多组测试数据。每组包含以下内容:
第一行包含两个整数0 0
时表示结束。
接下来
接下来一行包含
题目保证输入合法。
Output
输出仅包含一个整数,如果无解输出-1
,否则输出最短的时间(单位:秒)。
Sample input and output
9 100 0 0 0 0 0 1 0 0 00 0 0 0 0 0 0 0 1 00 0 0 1 0 0 0 0 0 00 0 1 0 0 0 0 0 0 00 0 0 0 0 0 1 0 0 00 0 0 0 0 1 0 0 0 00 0 0 1 1 0 0 0 0 00 0 0 0 0 0 0 0 0 01 0 0 0 0 0 0 0 1 07 2 2 7 29 100 0 0 0 0 0 1 0 0 00 0 0 0 0 0 0 0 1 00 0 0 1 0 0 0 0 0 00 0 1 0 0 0 0 0 0 00 0 0 0 0 0 1 0 0 00 0 0 0 0 1 0 0 0 00 0 0 1 1 0 0 0 0 00 0 0 0 0 0 0 0 0 01 0 0 0 0 0 0 0 1 07 2 2 7 20 0
1212
Hint
样例如图所示:(蓝色点为出发点,红色点为目标点,红线为路径)
状态:
1.向左转
2.向右转
3.向此时的方向走1步
4.向此时的方向走2步
5.向此时的方向走3步
bfs
#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;}
0 0
- UESTC 853 一个简单的走迷宫问题
- 走迷宫的问题
- 一个走迷宫的程序
- C++实现简单的走迷宫
- 【算法】老鼠走迷宫问题的解答
- 老鼠走迷宫问题
- 机器人走迷宫问题
- 老鼠走迷宫问题
- 走迷宫问题
- 走迷宫问题
- dfs-----走迷宫问题
- 一个经典Delphi算法(老鼠走迷宫问题)
- 一个简单的迷宫算法
- 走迷宫的题目
- 简单的小游戏 ——走迷宫 哈哈。
- 老鼠走迷宫----------递归问题
- 穷举解决走迷宫问题
- 走迷宫最短路问题
- STM32电源管理
- C# 生成自定义长度的0-z随机字符串
- android(Google公司开发的操作系统)
- 文件处理函数
- ios何时使用self
- UESTC 853 一个简单的走迷宫问题
- TreeList获取节点中的值
- 用ATL开发ActiveX网页控件
- linux下一次性fork多个子进程方法
- AutoScrollViewPager与PageIndicator无限循环混合使用
- HDU2054_A == B ?【模拟题】【大数】【水题】
- VC ++ & sqlite
- C# 轻松获取路径中文件名、目录、扩展名等
- FileStream文件读写