博客
关于我
迷宫寻路 (20 分)
阅读量:670 次
发布时间:2019-03-15

本文共 1555 字,大约阅读时间需要 5 分钟。

给定一个M行N列的迷宫图,其中 "0"表示可通路,"1"表示障碍物,无法通行。在迷宫中只允许在水平或上下四个方向的通路上行走,走过的位置不能重复走。**

**5行8列的迷宫如下:

0 1 1 1 0 0 0 00 0 0 1 0 0 0 00 1 0 0 0 1 0 00 1 1 1 0 1 1 01 0 0 0 0 0 0 0

则从左上角(1,1)至右下角(5,8)的最短路径为:

1,1–》2,1–》2,2–》2,3–》3,3–》3,4–》3,5–》4,5–》5,5–》5,6–》5,7–》5,8

题目保证每个迷宫最多只有一条最短路径。

请输出该条最短路径,如果不存在任何通路,则输出"NO FOUND".

输入格式:

第一行,输入M和N值,表示迷宫行数和列数。

接着输入M行数值,其中,0表示通路,1表示障碍物。每列数值用空格符间隔。

接下来可能输入多组迷宫数据。

当输入M的值为-1时结束输入。

输出格式:

按行顺序输出路径的每个位置的行数和列数,如 x,y

如果不存在任何路径,则输出"NO FOUND".

每组迷宫寻路结果用换行符间隔。

输入样例:

在这里给出一组迷宫。例如:

8 8    0 0 1 0 0 0 1 00 0 1 0 0 0 1 00 0 0 0 1 1 0 00 1 1 1 0 0 0 00 0 0 1 0 0 0 00 1 0 0 0 1 0 00 1 1 1 0 1 1 01 0 0 0 0 0 0 04 4    0 0 1 00 0 0 00 0 1 1 0 1 0 0-1 -1

输出样例:

在这里给出相应的输出。例如:

1,12,13,14,15,15,25,36,36,46,57,58,58,68,78,8NO FOUND
#include
#include
using namespace std;const int N=110,inf=0x3f3f3f3f;int dist[N][2],path[N][2],m,n,minn;//dist存的是最短路径的x,y path存的是当前路径的x,yint dx[4]={ 0,1,0,-1},dy[4]={ 1,0,-1,0};bool st[N][N],g[N][N],success;void dfs(int step,int x,int y){ if(step>minn)return;//如果当前步骤已经大于最小步数,结束搜索 if(x==n&&y==m) { memcpy(dist,path,sizeof dist); success=1; minn=step; return ; } for(int i=0;i<4;i++) { int tx=x+dx[i],ty=y+dy[i]; if(tx>=1&&tx<=n&&ty<=m&&ty>=1&&!g[tx][ty]&&!st[tx][ty]) { st[tx][ty]=1; path[step][0]=tx,path[step][1]=ty; dfs(step+1,tx,ty); st[tx][ty]=0; } }}int main(){ while(cin>>n>>m,~n) { for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>g[i][j]; success=0; minn=inf; st[1][1]=1; path[0][0]=1,path[0][1]=1; dfs(1,1,1); if(success) for(int i=0;i

转载地址:http://pvzqz.baihongyu.com/

你可能感兴趣的文章
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
no such file or directory AndroidManifest.xml
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>