POJ - 3984 - 迷宫问题 (DFS)
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
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)的更多相关文章
- poj 3984 迷宫问题(dfs)
题目链接:http://poj.org/problem?id=3984 思路:经典型的DFS题目.搜索时注意剪枝:越界处理,不能访问处理. 代码: #include <iostream> ...
- POJ - 3984 迷宫问题 dfs解法
#include<stdio.h> #include<string.h> #include<stack> #include<algorithm> usi ...
- BFS(最短路+路径打印) POJ 3984 迷宫问题
题目传送门 /* BFS:额,这题的数据范围太小了.但是重点是最短路的求法和输出路径的写法. dir数组记录是当前点的上一个点是从哪个方向过来的,搜索+,那么回溯- */ /************* ...
- POJ 3984 迷宫问题
K - 迷宫问题 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- POJ 3984 迷宫问题(简单bfs+路径打印)
传送门: http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- POJ - 3984迷宫问题(最短路径输出)
题目链接:http://poj.org/problem?id=3984 题目: 迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...
- POJ 3984 - 迷宫问题 - [BFS水题]
题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...
- POJ 3984 迷宫问题 bfs 难度:0
http://poj.org/problem?id=3984 典型的迷宫问题,记录最快到达某个点的是哪个点即可 #include <cstdio> #include <cstring ...
- [POJ 3984] 迷宫问题(BFS最短路径的记录和打印问题)
题目链接:http://poj.org/problem?id=3984 宽度优先搜索最短路径的记录和打印问题 #include<iostream> #include<queue> ...
随机推荐
- SpringCloud学习笔记(2)----Spring Cloud Netflix之Eureka的使用
1. Spring Cloud Netflix Spring Cloud Netflix 是Spring Cloud 的核心子项目,是对Netflix公司一系列开源产品的封装.它为Spring Bo ...
- SpringBoot学习笔记(12)----SpringBoot实现多个 账号轮询发送邮件
首先,引入发送邮件的依赖,由于freemarker自定义模板,所以也需要把freemarker的依赖引入 pom.xml文件 <dependency> <groupId>org ...
- servlet中Session的用法
## (1)什么是Session? 服务器端为了保存用户的状态而创建的一个特殊的对象(即session对象). 当浏览器第一次访问服务器时,服务器会创建session对象(该 ...
- [SCOI2009]windy数 数位dp
Code: #include<cmath> #include<iostream> #include<cstdio> using namespace std; con ...
- Linux下编译,安装Apache httpd服务器
环境:ubuntu 16.0.4 Apache官网下载Apache httpd压缩包:httpd-2.4.27.tar.gz,安装之前请确定安装了make工具,我安装的是GNU make 解压文件 s ...
- TP5防sql注入、防xss攻击
框架默认没有设置任何过滤规则 可以配置文件中设置全局的过滤规则 config.php 配置选项 default_filter 添加以下代码即可 // 默认全局过滤方法 用逗号分隔多个 'default ...
- pyinstall 常见错误
字符编码错误: https://blog.csdn.net/weixin_42426496/article/details/81102665 https://blog.csdn.net/qq_4206 ...
- 小程序QQ版表情解析组件
代码片段: [https://developers.weixin.qq.com/s/KLaD5MmD7V45) GitHub: https://github.com/WozHuang/Miniprog ...
- ubuntu安装和使用
1.查看ubuntu是32位还是64位 教程:jingyan.baidu.com/article/db55b609ab531f4ba30a2f13.html 2.安装maven 教程:www.linu ...
- glm编译错误问题解决 formal parameter with __declspec(align('16')) won't be aligned
參考:http://stackoverflow.com/questions/25300116/directxxmmatrix-error-c2719-declspecalign16-wont-be-a ...