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 ...
随机推荐
- 「CF1313C Skyscrapers」
题目大意 给出一个长度为 \(N\) 的序列 \(a\) 需要构造出一个长度为 \(N\) 的序列 \(h\) 使得 \(\forall i \in \{1,2,\ldots ,N\} h_i \le ...
- 一种ui app写法
方法一: $(function(){ // 定义一个对象 var qx={}; qx.ui={}; qx.ui.getleft=function(){ alert('qx.ui.getleft'); ...
- html js中的引号
content = "<a class='btn' href='javascript:void(0)' onclick='change('orders/orderTail.do?ode ...
- 【K-means算法】matlab代码实例学习
1. MATLAB函数Kmeans 使用方法:Idx=Kmeans(X,K)[Idx,C]=Kmeans(X,K) [Idx,C,sumD]=Kmeans(X,K) [Idx,C,sumD,D]=Km ...
- linux 镜像备份工具rsnyc
1.本地拷贝文件nohup rsync -avzh /data/wwwroot/xhprof/* /mnt/xhprof/ &2.更改文件夹名称mv /data/wwwroot/xhprof ...
- 4-form表单的双向绑定
概念:表单中的input框等其他标签,值变化时会触发函数,改变state中的值,反过来修改state中的值也会改变input框中值的展现 实现:利用类组件里的state属性来实现(setState会再 ...
- centos7的网络管理(参考使用)
How to Setup network on centos 7 Posted krizna Centos, Centos 7 After installing Centos 7, You may ...
- Win10子系统Ubuntu安装nginx (win10 安装 nginx)
更新仓库,下载nginx: sudo apt update sudo apt install nginx 检查版本: nginx –v 启动服务: sudo nginx sudo service ...
- TensorFlow样例一
假设原函数为 f(x) = 5x^2 + 3,为了估计出这个函数,定义参数未知的函数g(x, w) = w0 x^2 + w1 x + w2,现要找出适合的w使g(x, w) ≍ f(x).将这个问题 ...
- Python学习 —— 爬虫入门 - 爬取Pixiv每日排行中的图片
更新于 2019-01-30 16:30:55 我另外写了一个面向 pixiv 的库:pixiver 支持通过作品 ID 获取相关信息.下载等,支持通过日期浏览各种排行榜(包括R-18),支持通过 p ...