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 ...
随机推荐
- TCP网络调试助手上提示错误:“1035 未知错误”的有效解决方法,本人实测确实可行
转:https://blog.csdn.net/jacket_/article/details/97415651 图片转载:https://blog.csdn.net/Alice_YCR/articl ...
- 吴裕雄 PYTHON 神经网络——TENSORFLOW 正则化
import tensorflow as tf import matplotlib.pyplot as plt import numpy as np data = [] label = [] np.r ...
- Cisco AP-ROMMON升级AP镜像
Rommon is Cisco bootloader for their Router devices >>>ROMMON是思科设备的引导加载程序while U-boot is a ...
- 【剑指Offer面试编程题】题目1509:树中两个结点的最低公共祖先--九度OJ
题目描述: 给定一棵树,同时给出树中的两个结点,求它们的最低公共祖先. 输入: 输入可能包含多个测试样例. 对于每个测试案例,输入的第一行为一个数n(0<n<1000),代表测试样例的个数 ...
- ResultSet指针回到初始位置的方法及ResultSet介绍
原文地址: https://blog.csdn.net/walkerjong/article/details/7023872 仅供学习参考使用. 结果集(ResultSet)是数据中查询结果返回的一种 ...
- 区分git ,github,github桌面版,gitbash、gitshell
推荐链接:https://www.runoob.com/git/git-tutorial.html https://www.zhihu.com/question/34582452?sort=c ...
- 转载--php函数使用--var_export
var_export用于将数组转换成字符串 <?php $arr = [ 'key1'=>'val1', 'key2'=>'val2', 'key3'=>'val3', 'ke ...
- UIKit框架使用总结--看看你掌握了多少
一.经常使用的,基本就是每次项目迭代都需要使用的 UIView.UILabel.UIImage.UIColor.UIFont.UIImageView.UITextField.UIButton. UIS ...
- VSCode 出现错误 System.IO.IOException: The configured user limit (128) on the number of inotify instances has been reached.
方案一: sudo vim /etc/sysctl.conf 增加下面内容(环境变量) fs.inotify.max_user_watches = 1638400 fs.inotify.max_use ...
- 【转】ERP系统测试方法
问题: 1.如何进行ERP系统测试用例设计? 2.ERP系统测试用例设计过程? 3.ERP系统测试用例设计的方法? ERP系统本身是一种业务流程很复杂,单据报表众多,逻辑性很强的系统,质量保证方 ...