迷宫问题
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10936   Accepted: 6531

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,

	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)

思路:DFS题。找到每一条路取最短路径

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define INF 0x3fffffff
using namespace std; int mp[5][5]; int ans;
struct node {
int x;
int y;
}lu[30], pa[30]; bool vis[8][8]; const int dir[4][2] = {-1, 0, 1, 0, 0, 1, 0, -1}; void dfs(int x, int y, int deep) {
if(x == 4 && y == 4 && deep < ans) {
ans = deep;
for(int i = 0; i < deep; i ++) {
pa[i].x = lu[i].x;
pa[i].y = lu[i].y;
}
}
for(int i = 0; i < 4; i ++) {
int tx = x + dir[i][0];
int ty = y + dir[i][1];
if(tx < 0 || tx > 4 || ty < 0 || ty > 4) continue;
if(mp[tx][ty] == 0 && !vis[tx][ty]) {
vis[tx][ty] = true;
lu[deep].x = tx;
lu[deep].y = ty;
dfs(tx, ty, deep + 1);
vis[tx][ty] = false;
}
}
} int main() {
for(int i = 0; i < 5; i ++) {
for(int j = 0; j < 5; j ++) {
scanf("%d", &mp[i][j]);
}
}
memset(lu, 0, sizeof(lu));
memset(pa, 0, sizeof(pa));
memset(vis, false, sizeof(vis));
ans = INF;
lu[0].x = lu[0].y = 0;
vis[0][0] = true;
dfs(0, 0, 1);
for(int i = 0; i < ans; i ++) {
printf("(%d, %d)\n", pa[i].x, pa[i].y);
}
return 0;
}

POJ - 3984 - 迷宫问题 (DFS)的更多相关文章

  1. poj 3984 迷宫问题(dfs)

    题目链接:http://poj.org/problem?id=3984 思路:经典型的DFS题目.搜索时注意剪枝:越界处理,不能访问处理. 代码: #include <iostream> ...

  2. POJ - 3984 迷宫问题 dfs解法

    #include<stdio.h> #include<string.h> #include<stack> #include<algorithm> usi ...

  3. BFS(最短路+路径打印) POJ 3984 迷宫问题

    题目传送门 /* BFS:额,这题的数据范围太小了.但是重点是最短路的求法和输出路径的写法. dir数组记录是当前点的上一个点是从哪个方向过来的,搜索+,那么回溯- */ /************* ...

  4. POJ 3984 迷宫问题

    K - 迷宫问题 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  5. POJ 3984 迷宫问题(简单bfs+路径打印)

    传送门: http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  6. POJ - 3984迷宫问题(最短路径输出)

    题目链接:http://poj.org/problem?id=3984 题目: 迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  7. POJ 3984 - 迷宫问题 - [BFS水题]

    题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...

  8. POJ 3984 迷宫问题 bfs 难度:0

    http://poj.org/problem?id=3984 典型的迷宫问题,记录最快到达某个点的是哪个点即可 #include <cstdio> #include <cstring ...

  9. [POJ 3984] 迷宫问题(BFS最短路径的记录和打印问题)

    题目链接:http://poj.org/problem?id=3984 宽度优先搜索最短路径的记录和打印问题 #include<iostream> #include<queue> ...

随机推荐

  1. SpringCloud学习笔记(5)----Spring Cloud Netflix之Eureka的服务认证和集群

    1. Eureka服务认证 1. 引入依赖 <dependency> <groupId>org.springframework.boot</groupId> < ...

  2. SpringBoot学习笔记(10)-----SpringBoot中使用Redis/Mongodb和缓存Ehcache缓存和redis缓存

    1. 使用Redis 在使用redis之前,首先要保证安装或有redis的服务器,接下就是引入redis依赖. pom.xml文件如下 <dependency> <groupId&g ...

  3. MySQL的concat以及group_concat的用法

    一.concat()函数 1.功能:将多个字符串连接成一个字符串. 2.语法:concat(str1, str2,...) 返回结果为连接参数产生的字符串,如果有任何一个参数为null,则返回值为nu ...

  4. Quartz任务调度 服务日志+log4net打印日志+制作windows服务

    引言 现在许多的项目都需要定时的服务进行支撑,而我们经常用到的定时服务就是Quartz任务调度了.不过我们在使用定时Job进行获取的时候,有时候我们就需要记录一下自定义的日志,甚至我们还会对执行定时J ...

  5. HYSBZ-1040 骑士 基环树上的树状dp

    题目链接:https://cn.vjudge.net/problem/HYSBZ-1040 题意 Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英. 他们劫富济贫,惩恶扬善,受到社会各界的 ...

  6. Qt之字典划词

    简述 相信大家都用过词典吧!因为英语不太好...O(∩_∩)O~,所以经常进行划词翻译! 简述 实现 效果 源码 更多参考 实现 原理:鼠标移至某单词之上,获取鼠标位置,然后在对应位置进行取词,翻译! ...

  7. Hadoop - YARN NodeManager 剖析

    一 概述         NodeManager是执行在单个节点上的代理,它管理Hadoop集群中单个计算节点,功能包含与ResourceManager保持通信,管理Container的生命周期.监控 ...

  8. NUTCH2.3 hadoop2.7.1 hbase1.0.1.1 solr5.2.1部署(一)

     Precondition: hadoop 2.7.1 Nutch 2.3 hbase 1.0.1.1 / hbase 0.98.13 solr 4.8.1 Linux version 3.16. ...

  9. bzoj1801: [Ahoi2009]chess 中国象棋(DP)

    1801: [Ahoi2009]chess 中国象棋 题目:传送门 题解: 表示自己的DP菜的抠脚 %题解... 定义f[i][j][k]表示前i行 仅有一个棋子的有j列 有两个棋子的有k个 的方案数 ...

  10. MySql创建指定字符集的数据库

    以创建字符集为utf8的数据库为例: CREATE DATABASE db_name DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; CREAT ...