- 迷宫问题 POJ - 3984 bfs记录路径并输出最短路径
int maze[5][5] = {
0, 1, 0, 0, 0,
0, 1, 0, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
Input
Output
Sample Input
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
Sample Output
(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)
记录路径并输出
代码:
1 #include<stdio.h>
2 #include<string.h>
3 #include<algorithm>
4 #include<iostream>
5 #include<queue>
6 using namespace std;
7 struct shu
8 {
9 int y,x,step;
10 }str1,str2;
11 int a,s,d,f,g,q[10][10],v[10][10],w[10][10],z[25],c[25];
12 int p[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
13 void bfs()
14 {
15 int xx,yy,x,y;
16 queue<shu>r;
17 r.push(str1);
18 while(!r.empty())
19 {
20 str1=r.front();
21 r.pop();
22 if(str1.y==4 && str1.x==4)
23 {
24 xx=q[str1.y][str1.x]%10;
25 yy=(q[str1.y][str1.x]-xx)/10;
26 while(1) //while循环找寻路径
27 {
28 z[g]=yy;c[g]=xx;
29 g++;
30 if(q[yy][xx]==0)
31 {
32 break;
33 }
34 x=q[yy][xx]%10; //找到这一个点的前一个点的坐标(因为我们之前维护过,所以用同样方式找寻点坐标)
35 y=(q[yy][xx]%100-x)/10;
36 yy=y;
37 xx=x;
38 }
39 while(!r.empty())
40 r.pop();
41 return;
42 }
43 str2.step=str1.step+1;
44 for(int i=0;i<4;++i)
45 {
46
47 str2.y=str1.y+p[i][1];
48 str2.x=str1.x+p[i][0];
49 if(str2.y<0 || str2.x<0 || str2.y>=5 || str2.x>=5) continue;
50 if(v[str2.y][str2.x]==0 && w[str2.y][str2.x]==0)
51 {
52 q[str2.y][str2.x]=str1.y*10+str1.x; //把它的前一个点的坐标维护成一个唯一值
53 w[str2.y][str2.x]=1;
54 r.push(str2);
55 /*
56 除了这一种方式之外,我们还可以定义一个结构体专门保存上一个点的坐标
57
58 */
59 }
60 }
61 }
62 }
63 int main()
64 {
65 g=0;
66 for(int i=0; i<5;++i)
67 {
68 for(int j=0; j<5 ;++j)
69 {
70 scanf("%d",&v[i][j]);
71 }
72 }
73 q[0][0]=0;
74 str1.x=0;
75 str1.y=0;
76 str1.step=0;
77 w[0][0]=1;
78 bfs();
79 printf("(0, 0)\n");
80 for(int i=g-1;i>=0;--i)
81 {
82 printf("(%d, %d)\n",z[i],c[i]);
83 }
84 printf("(4, 4)\n");
85 return 0;
86 }
- 迷宫问题 POJ - 3984 bfs记录路径并输出最短路径的更多相关文章
- Q - 迷宫问题 POJ - 3984(BFS / DFS + 记录路径)
Q - 迷宫问题 POJ - 3984 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, ...
- poj 2395 bfs/记录路径
http://poj.org/problem?id=2935 Basic Wall Maze Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ.3894 迷宫问题 (BFS+记录路径)
POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...
- Codeforces-A. Shortest path of the king(简单bfs记录路径)
A. Shortest path of the king time limit per test 1 second memory limit per test 64 megabytes input s ...
- HDU1026--Ignatius and the Princess I(BFS记录路径)
Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...
- 迷宫问题(bfs+记录路径)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105278#problem/K K - 迷宫问题 Time Limit:1000 ...
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- (简单) POJ 3414 Pots,BFS+记录路径。
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- 迷宫问题 POJ - 3984 (搜索输出路径)
题目大意 题目不需要大意,poj居然还有中文题 鸣谢 特别鸣谢ljc大佬提供的方法!!! 解法 我们可能输出个最短路径的长度比较简单,但是输出最短路径真的是没有做过,这里有一种简单的方法 因为我们的d ...
随机推荐
- windows打包脚本出现 /bin/sh^M: 坏的解释器: 没有那个文件或目录 错误
1.错误描述 我在Windows 10 系统下打包dolphinscheduler,上传到centos7解压之后,执行脚本报如下错误: -bash: ./dolphinscheduler-daemon ...
- 【EXP】exp-00091解决办法
如果遇到exp的话一般都是因为字符集的问题 解决办法: 1.在oracle中查看数据库的字符集 SQL> select userenv('language') from dual; USEREN ...
- SAP ERP中权限参数和角色相关表
SAP版本:S/4 HANA 1809
- sap alv grid 中的delete按键问题
今天发先一个问题,在使用ALV输出的时候,如果有字段设置为可编辑状态,则会在前面出现选择条,并且,当我们选择一行或者多行的时候,可以用键盘上的DELETE键将行删除!呵呵
- 安装macosx10.13high serria
本教程所需资源下载链接: 链接:https://pan.baidu.com/s/1wGTezXz6zGvtlwpv6mMoSg 提取码:r6n9 安装VMware workstation 16.0,安 ...
- atlas读写分离
Atlas是由 Qihoo 360公司Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目.它在MySQL官方推出的MySQL-Proxy 0.8.2版本的基础上,修改了大量bug ...
- poj 1038 Bugs Integrated, Inc. 题解
提供一种代码难度比较简单的做法(可能) 状态表示: 设置状态$ f[i][j] $,表示第 \(i\) 行状态为 \(j\) 的最大放置数,因为这是个阴间题,因为题目内存设置很小,所以要用滚动数组,存 ...
- java虚拟机入门(四)-垃圾回收的故事
谈到垃圾回收器,java程序员骄傲了起来,c语言你是够快,但是你有管家帮你打扫吗,还不是得靠自己的一双手,有钱就是任性.既然如此令java程序员骄傲的垃圾回收器,怎能让人不想去一探究竟呢! 垃圾回收器 ...
- Golang拼接字符串的5种方法及其效率_Chrispink-CSDN博客_golang 字符串拼接效率 https://blog.csdn.net/m0_37422289/article/details/103362740
Different ways to concatenate two strings in Golang - GeeksforGeeks https://www.geeksforgeeks.org/di ...
- 在Centos7上安装Python+Selenium+Firefox+Geckodriver
1.事先准备好Centos7的系统 Centos系统是CentOS Linux release 7.4.1708 (Core) 查看Centos内核版本命令cat /etc/centos-releas ...