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> ...
随机推荐
- 优动漫PAINT绘制紫阳花教程
紫阳花是插画.漫画很常见的绘画画材.这个教程非常好懂.而且很方便就能绘制出漂亮的效果.因为这种花一个月内能变化三种颜色,故而人们赋予它的花语是善变.背叛. 教程是简单,呃.... 没有优动漫PAINT ...
- 文件流转base64字符串
public static string GetBase64Data() { string path = @"C: \txt.jpg"; FileStream filestream ...
- java上传文件工具类
这个是之前整理之前所学时与使用java向邮箱发送邮件一块找到的,一起贴出来供大家参考: import java.awt.image.BufferedImage; import java.io.File ...
- 小程序自定义tabbar
代码片段: wechatide://minicode/IUoCyemJ7D3d GitHub: https://github.com/WozHuang/Miniprogram-Demo/tree/ma ...
- Mean, Median, Mode, Range, and Standard Deviation
Descriptive statistics tell you about the distribution of data points in data set. The most common m ...
- Tomcat跨域资源共享
1.下载Jar包 cors-filter-1.7.jar java-property-utils-1.9.jar 下载完成后将Jar拷贝到tomcat下lib目录中 2.修改web.xml配置 在29 ...
- 关于数组array_diff(array1, array2)求差集来比较数组是否相等的问题细究
无意中发现很多朋友都喜欢使用array_diff(array1, array2)来判断两个数组是否相等, 我自己也偶尔会这么使用 但是今天我在写代码的过程中无意发现这么做是不准确的. 首先我们来看一下 ...
- POJ 1975 Median Weight Bead
Median Weight Bead Time Limit: 1000ms Memory Limit: 30000KB This problem will be judged on PKU. Orig ...
- C - The C Answer (2nd Edition) - Exercise 1-12
/* Write a program that prints its input one word per line. */ #include <stdio.h> #define IN 1 ...
- ITOO右击菜单实现
ITOO做了持续了这么长时间,client使用MVC+EF+EasyUI框架,服务端在三层基础上增加WCF服务,后来增加容器,AOP(还没怎么接触),封装了在我们刚開始看来神奇的底层方法,克服了非常多 ...