【POJ - 3984】迷宫问题(dfs)
-->迷宫问题
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
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)的更多相关文章
- 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 - 迷宫问题 (DFS)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10936 Accepted: 6531 Description ...
- 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> ...
随机推荐
- vue项目搭建和开发流程 vue项目配置ElementUI、jQuery和Bootstrap环境
目录 一.VUE项目的搭建 1. 环境搭建 2. 项目的创建和启动 二. 开发项目 1. 配置vue项目启动功能 2. 开发vue项目 (1)项目文件的作用 (2)vue项目开发流程 (3)vue项目 ...
- 如何获取到一个form中的所有子控件?
使用yield关键字,非常的方便 private static IEnumerable<Control> GetChildren(Control frmRootDock) { if (fr ...
- MessagePack Java Jackson Dataformat - POJO 的序列化和反序列化
在本测试代码中,我们定义了一个 POJO 类,名字为 MessageData,你可以访问下面的链接找到有关这个类的定义. https://github.com/cwiki-us-demo/serial ...
- Codevs 4019 想越狱的小明
4019 想越狱的小明 时间限制: 1 s 空间限制: 1000 KB 题目等级 : 钻石 Diamond 题目描述 Description 这次小明来到了经典美剧<越狱>的场景里-- 它 ...
- AtCoder AGC019E Shuffle and Swap (DP、FFT、多项式求逆、多项式快速幂)
题目链接 https://atcoder.jp/contests/agc019/tasks/agc019_e 题解 tourist的神仙E题啊做不来做不来--这题我好像想歪了啊= =-- 首先我们可以 ...
- python3爬取拉钩招聘数据
使用python爬去拉钩数据 第一步:下载所需模块 requests 进入cmd命令 :pip install requests 回车 联网自动下载 xlwt 进入cmd命令 :pip install ...
- Inter IPP+ VS + opencv 在 Windows下的环境搭建
首先Inter官网申请和下载:https://software.intel.com/en-us/intel-ipp 需要VS2013或更高版本(先装vs再装IPP,我的版本是VS2015社区版,IPP ...
- 震惊!文科生如何三个月转行成为Java工程师?
点击上方“程序员江湖”,选择“置顶或者星标” 你关注的就是我关心的! 作者:以大橘为重链接:https://www.nowcoder.com/discuss/156087 楼主是19届应届生,去年在牛 ...
- onReachBottom 注意事项
onReachBottom使用注意 可在pages.json里定义具体页面底部的触发距离onReachBottomDistance,比如设为50,那么滚动页面到距离底部50px时,就会触发onReac ...
- 2、记录代码----Ajax
$.ajax({ url:'/content-engine/index.php/tracker/confirmSendEmail', async: false, //默认为true,同意异步传输 da ...