定义一个二维数组:

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. C:sizeof 运算符

    sizeof不是函数,所以不需要包含任何头文件,它的功能是计算一个数据类型的大小,单位为字节 sizeof的返回值为size_t size_t类型在32位操作系统下是unsigned int,是一个无 ...

  2. Linux学习:进入与退出系统

    进入Linux系统:必须要输入用户的账号,在系统安装过程中可以创建以下两种帐号: 1.root--超级用户帐号(系统管理员),使用这个帐号可以在系统中做任何事情. 2.普通用户--这个帐号供普通用户使 ...

  3. 基于scikitlearn的深度学习环境安装(三)(完整版)

    OS Linux  Ubuntu14.04 安装 pip (python2.7.9或以上自带pip) sudo apt-get install python-pip pip是python环境下安装包的 ...

  4. matplotlib学习(2)

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

  5. 我的B站主页:https://space.bilibili.com/611212 内有视频题解

    我的B站主页:https://space.bilibili.com/611212 内有视频题解

  6. Caused by: java.lang.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfig

    Caused by: java.lang.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfig at ...

  7. Spring中@MapperScan注解

    之前是,直接在Mapper类上面添加注解@Mapper,这种方式要求每一个mapper类都需要添加此注解,麻烦. 通过使用@MapperScan可以指定要扫描的Mapper类的包的路径,比如: @Sp ...

  8. Centos 下安装php

    1 从php 官网下载源安装包 http://php.net/downloads.php // 安装php 扩展 2 yum install libxml2 libxml2-devel openssl ...

  9. ZkApi的方法跨域访问ZkResource的静态资源文件出现的问题

    问题:ZkApi的方法跨域访问ZkResource的静态资源文件出现下面的情况 解决方法: cd /usr/local/apache/conf/vhost vim .conf 将上面的文件php_ad ...

  10. 前端学习 之 HTML

    一.HTML 介绍 超文本标记语言(英语:HyperText Markup Language,简称:HTML)是一种用于创建网页的标准标记语言,它不是一种编程语言. HTML使用标签来描述网页.不像p ...