定义一个二维数组:

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

一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

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. 暑假集训(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, ...

  2. bfs—迷宫问题—poj3984

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20591   Accepted: 12050 http://poj ...

  3. 迷宫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, ...

  4. 迷宫问题---poj3984(bfs,输出路径问题)

    题目链接 主要就是输出路径问题: pre[x][y]表示到达(x,y)是由点(pre[x][y].x,  pre[x][y].y)而来: #include<stdio.h> #includ ...

  5. 简单广搜,迷宫问题(POJ3984)

    题目链接:http://poj.org/problem?id=3984 解题报告: 1.设置node结构体,成员pre记录该点的前驱. 2.递归输出: void print(int i) { ) { ...

  6. BFS算法入门--POJ3984

    迷宫问题–POJ3984 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22008 Accepted: 12848 Descri ...

  7. DFS系列 POJ(自认为的讲解)

    C - Sum It Up POJ1564 题意: 给你一个N,然后给你一堆数The numbers in each list appear in nonincreasing order, and t ...

  8. 1.1.1最短路(Floyd、Dijstra、BellmanFord)

    转载自hr_whisper大佬的博客 [ 一.Dijkstra 比较详细的迪杰斯特拉算法讲解传送门 Dijkstra单源最短路算法,即计算从起点出发到每个点的最短路.所以Dijkstra常常作为其他算 ...

  9. 最短路算法详解(Dijkstra/SPFA/Floyd)

    新的整理版本版的地址见我新博客 http://www.hrwhisper.me/?p=1952 一.Dijkstra Dijkstra单源最短路算法,即计算从起点出发到每个点的最短路.所以Dijkst ...

  10. codingame

    无聊登了一下condingame,通知说有本周谜题,正好刚撸完bfs,想尝试下. 题目链接:https://www.codingame.com/ide/17558874463b39b9ce6d4207 ...

随机推荐

  1. 在CDN不能使用的时候加载自己服务器的资源

    <script src="http://wlib.sinaapp.com/js/jquery/1.7.2/jquery.min.js"></script> ...

  2. CCF认证 2019-12-3

    分析 后面的数据,坐标分布太离散,不能用一个二位数组来模拟垃圾分布.因此,考虑用一个数组记录每个垃圾点的位置. 先根据x坐标.再根据y坐标进行排序. 再遍历数组中的每一处垃圾点,判断其是否能建回收站( ...

  3. pom.xml文件中properties有什么用

    properties标签的作用: 在标签内可以把版本号作为变量进行声明,后面dependency中用到版本号时可以用${变量名}的形式代替,这样做的好处是:当版本号发生改变时,只有更新properti ...

  4. matplotlib学习(2)

    1.legend的学习(图例)1.1 代码 import matplotlib.pyplot as plt import numpy as np x=np.linspace(-3,3,50) #从-1 ...

  5. 吴裕雄--天生自然TensorFlow2教程:链式法则

    import tensorflow as tf x = tf.constant(1.) w1 = tf.constant(2.) b1 = tf.constant(1.) w2 = tf.consta ...

  6. 吴裕雄 python 神经网络——TensorFlow 队列操作

    import tensorflow as tf q = tf.FIFOQueue(2, "int32") init = q.enqueue_many(([0, 10],)) x = ...

  7. JS 上传图片压缩,原比例压缩

    复制 粘贴 改吧改吧就可用,原生js var fileObj = file.file;//原文件 file是我用vue-vant里的组件,里边有file(原文件)和content(base64) 其它 ...

  8. Python学习第二十课——自定property and classmethod

    自定制property class Lazyproperty: def __init__(self,func): # print('==========>',func) self.func=fu ...

  9. 谈一谈并查集QAQ(上)

    最近几日理了理学过的很多oi知识...发现不知不觉就有很多的知识忘记了... 在聊聊并查集的时候顺便当作巩固吧.... 什么是并查集呢? ( Union Find Set ) 是一种用于处理分离集合的 ...

  10. 【PAT甲级】1065 A+B and C (64bit) (20 分)(大数溢出)

    题意: 输入三个整数A,B,C(long long范围内),输出是否A+B>C. trick: 测试点2包括溢出的数据,判断一下是否溢出即可. AAAAAccepted code: #defin ...