Day2-C-迷宫问题 -POJ3984
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) 分析:简单的求路径BFS问题,用数组处理更好找路径,直接上代码:
const int dx[] = {, -, , };
const int dy[] = {, , , -};
int G[][], vis[][];
struct Node {
int x, y, pre;
Node(int _x = -, int _y = -, int _pre = -):x(_x),y(_y),pre(_pre){}
} Nodes[];
bool inside(int x, int y) {
return x >= && x < && y >= && y < ;
}
void print(Node p) {
if(p.pre != -)
print(Nodes[p.pre]);
printf("(%d, %d)\n", p.x, p.y);
}
int main() {
for (int i = ; i < ; ++i) { // read in
for (int j = ; j < ; ++j)
scanf("%d", &G[i][j]);
}
int head = , rear = ;
Nodes[rear++] = Node(, );
while(head < rear) { //bfs
Node tmp = Nodes[head++];
if(vis[tmp.x][tmp.y])
continue;
vis[tmp.x][tmp.y] = ;
if(tmp.x == && tmp.y == )
print(Nodes[head - ]);
for (int i = ; i < ; ++i) {
int nx = tmp.x + dx[i], ny = tmp.y + dy[i];
if(inside(nx,ny) && !G[nx][ny] && !vis[nx][ny]) { //prevent multiple entries
Nodes[rear++] = Node(nx, ny, head - );
}
}
}
return ;
}
Day2-C-迷宫问题 -POJ3984的更多相关文章
- 暑假集训(1)第八弹 -----简单迷宫(Poj3984)
Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, ...
- bfs—迷宫问题—poj3984
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20591 Accepted: 12050 http://poj ...
- 迷宫bfs POJ3984
#include<stdio.h> int map[5][5]={0,1,0,0,0, 0,1,0,1,0, 0,0,0,0,0, 0,1,1,1,0, ...
- 迷宫问题---poj3984(bfs,输出路径问题)
题目链接 主要就是输出路径问题: pre[x][y]表示到达(x,y)是由点(pre[x][y].x, pre[x][y].y)而来: #include<stdio.h> #includ ...
- 简单广搜,迷宫问题(POJ3984)
题目链接:http://poj.org/problem?id=3984 解题报告: 1.设置node结构体,成员pre记录该点的前驱. 2.递归输出: void print(int i) { ) { ...
- BFS算法入门--POJ3984
迷宫问题–POJ3984 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22008 Accepted: 12848 Descri ...
- DFS系列 POJ(自认为的讲解)
C - Sum It Up POJ1564 题意: 给你一个N,然后给你一堆数The numbers in each list appear in nonincreasing order, and t ...
- 1.1.1最短路(Floyd、Dijstra、BellmanFord)
转载自hr_whisper大佬的博客 [ 一.Dijkstra 比较详细的迪杰斯特拉算法讲解传送门 Dijkstra单源最短路算法,即计算从起点出发到每个点的最短路.所以Dijkstra常常作为其他算 ...
- 最短路算法详解(Dijkstra/SPFA/Floyd)
新的整理版本版的地址见我新博客 http://www.hrwhisper.me/?p=1952 一.Dijkstra Dijkstra单源最短路算法,即计算从起点出发到每个点的最短路.所以Dijkst ...
- codingame
无聊登了一下condingame,通知说有本周谜题,正好刚撸完bfs,想尝试下. 题目链接:https://www.codingame.com/ide/17558874463b39b9ce6d4207 ...
随机推荐
- day46_Webservice学习笔记_02
一.回顾昨天所学 什么是webservice? 什么是远程调用技术?答:系统和系统之间的调用,从远程系统当中获取业务数据. Webservice是web服务,他是用http传输SOAP协议 ...
- POJ-2891 Strange Way to Express Integers(拓展中国剩余定理)
放一个写的不错的博客:https://www.cnblogs.com/zwfymqz/p/8425731.html POJ好像不能用__int128. #include <iostream> ...
- 【代码总结】PHP之会话跟踪技术
有些时候感觉自己学习的效率好低,一套教程一本书,反复听,反复看,反复敲代码.自学这条路真的是很艰难,在任何事情都没有成功之前都很枯燥乏味.自学的过程中总会遇到很多问题,有时候连错误的原因都找不出来,那 ...
- PyQt5四大布局方式
1.绝对布局方式'''绝对布局方式,通过move的XY坐标方式来控制控件的位置'''from PyQt5.QtWidgets import *import sys,math class absolut ...
- SpringBoot与Mybatis整合(包含generate自动生成代码工具,数据库表一对一,一对多,关联关系中间表的查询)
链接:https://blog.csdn.net/YonJarLuo/article/details/81187239 自动生成工具只是生成很单纯的表,复杂的一对多,多对多的情况则是在建表的时候就建立 ...
- CSS创意与视觉表现
视觉效果 CSS代码: .cover { padding: 36% 50%; background: linear-gradient(to right, white 50%, black calc(5 ...
- Java语言特性、加载与执行
[开源.免费.纯面向对象.跨平台] 简单性: 相对而言,例如,Java是不支持多继承的,C++是支持多继承的,多继承比较复杂:C++ 有指针,Java屏蔽了指针的概念.所以相对来说Java是简单的. ...
- webpack原理类型问题
1.webpack底层原理 (实现一个webpack) 步骤:1.拿到入口文件的代码并读出来转化为js对象(抽象语法术parser)2.拿到所有模块的依赖 ‘./message.js’,放进数组中 引 ...
- C# Lambda排序
1.按照多个字段进行排序:xxxList.OrderBy(c => c.RoadCode).ThenBy(c => c.Qdzh),表示先按照RoadCode字段进行排序再按照Qdzh字段 ...
- 吴裕雄--天生自然ORACLE数据库学习笔记:数据导出与导入
create directory dump_dir as 'd:\dump'; grant read,write on directory dump_dir to scott; --在cmd下 exp ...