-->迷宫问题

Descriptions:

定义一个二维数组:

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)

题目链接:

https://vjudge.net/problem/POJ-3984

没有用到队列,想了一个染色+dfs的方法,直接就dfs走一遍地图,然后按步数染色,最后按步数从大到小输出即可,头脑简单的我直接就暴力搜了

AC代码:

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
using namespace std;
typedef long long ll;
const int maxx=;
const int n=;
int mp[maxx][maxx];//原始地图
int vis[maxx][maxx];//路径(染色)
int dx[maxx];//输出路径
int dy[maxx];
void dfs(int i,int j)
{
if(mp[i][j]== || i< || j>n || i>n || j<)
return;
mp[i][j]=;
if(!mp[i][j+] && j+>= && j+<=n)//向右
{
vis[i][j+]=vis[i][j]+;
dfs(i,j+);
}
if(!mp[i+][j] && i+>= && i+<=n)//向下
{
vis[i+][j]=vis[i][j]+;
dfs(i+,j);
}
if(!mp[i-][j] && i->= && i-<=n)//向上
{
vis[i-][j]=vis[i][j]+;
dfs(i-,j);
}
if(!mp[i][j-] && j->= && j-<=n)//向左
{
vis[i][j-]=vis[i][j]+;
dfs(i,j-);
}
}
int main()
{
memset(vis,,sizeof(vis));
memset(mp,,sizeof(mp));
for(int i=; i<=n; i++)
for(int j=; j<=n; j++)
cin >> mp[i][j];
vis[][]=;
dfs(,);
//路径直观图,可以看走的过程,需要看的把注释去掉(力荐!!!)
/* for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
printf("%4d ",vis[i][j]);
}
cout << endl;
}*/
int g=vis[n][n];
int x,y;
x=n,y=n;
dx[g]=n;
dy[g]=n;
while(g!=)//按步数从大到小输出
{
g--;
if(vis[x-][y]==g)
{
dx[g]=x-;
dy[g]=y;
x--;
}
else if(vis[x+][y]==g)
{
dx[g]=x+;
dy[g]=y;
x++;
}
else if(vis[x][y+]==g)
{
dx[g]=x;
dy[g]=y+;
y++;
}
else if(vis[x][y-]==g)
{
dx[g]=x;
dy[g]=y-;
y--;
}
}
for(int i=; i<=vis[n][n]; i++)
{
cout << "(" << dx[i]- << "," << " " <<dy[i]- << ")" << endl;
}
}

【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 - 迷宫问题 (DFS)

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

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

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

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

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

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

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

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

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

随机推荐

  1. nginx之"/"结尾

    一.配置127.0.0.1:8081的tomcat下的文件: 1.ROOT/a.html : this is ROOT page 2.ROOT/testa.html : nihao 3.ROOT/in ...

  2. PHP内置常量,和可变变量,常量的定义

    关键常量 可变变量----变量名是变量的变量 常量的定义

  3. List集合、泛型、装箱拆箱

    1.List集合 Vector:增删改查都慢 线程同步 线程安全 LlinkedList:以链表结构存储数据,查询慢.增删快 ArrayList:的运行速度比较快 连续数据空间存储数据,查询快(下标) ...

  4. zabbix企业微信告警配置教程

    前言: zabbix企业微信告警只需要配置一次就可以使用很久了,但是发现再次配置时,总会有遗忘,很麻烦又要去重新熟悉,所以,现在记录一份详细的配置过程,方便日后再次配置. 1.zabbix_serve ...

  5. jquery文章链接

    好文链接 1.jQuery是js的一个库,封装了js中常用的逻辑: 2.调用jQuery: (1).本地调用,在script标签的src属性里写上jQuery文件的地址. (2).使用CDN调用jQu ...

  6. 使用Python操作Excel文档(一)

    Python | 使用Python操作Excel文档(一) 0 前言 在阅读本文之前,请确保您已满足或可能满足以下条件: 请确保您具备基本的Python编程能力. 请确保您会使用Excel. 请确保您 ...

  7. jupyter工具

    国内源安装: pip install jupyter -i http://pypi.douban.com/simple --trusted-host pypi.douban.com pip --def ...

  8. 微信小程序设置全局变量

    为了提高程序的可用性我们在做项目的时候一定要设置全局变量 微信小程序里面有个app.js,我们可以在这个里面设置全局变量, globalData:{ userInfo:null, test:" ...

  9. Error:java: 错误: 不支持发行版本 5

    本文链接:https://blog.csdn.net/wo541075754/article/details/70154604 在Intellij idea中新建了一个Maven项目,运行时报错如下: ...

  10. VNC Viewer配置

    VNC概述 VNC (Virtual Network Computing)是虚拟网络计算机的缩写.VNC 是一款优秀的远程控制工具软件,由著名的 AT&T 的欧洲研究实验室开发的.VNC 是在 ...