迷宫问题
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 31616   Accepted: 18100

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)

Source

1.简单DFS

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<vector>
#include<cmath>
#include<cstring>
#include<string> #define N 100010 using namespace std; void in(int &x){
register char c=getchar();x=0;int f=1;
while(!isdigit(c)){if(c=='-') f=-1;c=getchar();}
while(isdigit(c)){x=x*10+c-'0';c=getchar();}
x*=f;
} int a[6][6],tot;
struct node{int x,y;}e[30],ans[30];
int mx[4]={0,0,1,-1},
my[4]={1,-1,0,0};
bool vis[6][6]; void print(int k){
if(k==2) for(int i=1;i<=tot;i++) printf("(%d, %d)\n",ans[i].x,ans[i].y);
else for(int i=1;i<=tot;i++) ans[i].x=e[i].x,ans[i].y=e[i].y;
} void DFS(int x,int y,int tim){
e[tim].x=x;e[tim].y=y;
if(tim>tot) return;
if(x==4&&y==4){
if(tim<tot){
tot=tim;print(1);
}return;
}for(int i=0;i<4;i++){
int tx=x+mx[i],ty=y+my[i];
if(tx>=0&&tx<=4&&ty>=0&&ty<=4&&vis[tx][ty]==0&&a[tx][ty]==0){
vis[tx][ty]=1;
DFS(tx,ty,tim+1);
vis[tx][ty]=0;
}
}
} int main()
{
tot=0x7fffffff;
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
in(a[i][j]);
DFS(0,0,1);
print(2);
return 0;
}

  2.简单BFS(考虑如何更新)

POJ3984——迷宫问题的更多相关文章

  1. poj3984迷宫问题 广搜+最短路径+模拟队列

    转自:http://blog.csdn.net/no_retreats/article/details/8146585   定义一个二维数组: int maze[5][5] = { 0, 1, 0, ...

  2. poj3984迷宫问题

    一个5 × 5的二维数组,表示一个迷宫.其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线. 很简单的一道题,迷宫问题,一般都选择两种优先搜索 ...

  3. [poj3984]迷宫问题_bfs

    迷宫问题 题目大意:给你一个5*5的矩阵,求左上角到左下角的最短路径. 注释:0或1的矩阵,1表示不能走,0表示能走,保证有唯一最短路径. 想法:bfs爆搜练习题.通过其实点,定义方向数组,然后进行b ...

  4. poj3984迷宫问题(DFS广搜)

    迷宫问题 Time Limit: 1000MSMemory Limit: 65536K Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, ...

  5. Poj3984 迷宫问题 (BFS + 路径还原)

    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, ...

  6. POJ-3984.迷宫问题(BFS + 路径输出)

    昨天中午做的这道题,结果蛙了一整天,就因为一行代码困住了,今天算是见识到自己有多菜了.流泪.jpg 本题大意:给一个5 * 5的迷宫,1表示墙壁,0表示通路,从左上角走到右下角并输出路径. 本题思路: ...

  7. poj3984迷宫问题(dfs+stack)

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35426   Accepted: 20088 Descriptio ...

  8. POJ3984 迷宫问题 —— BFS

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

  9. POJ-3984 迷宫问题(BFS找最短路径并保存)

    问题: 定义一个二维数组:  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, ...

随机推荐

  1. struts2 一个CRUD的BaseAction

    在struts2 in action中所见,这样封装后省去了大部分crud反复代码.尽管还不能理悟.先记下来. abstract class BaseAction extends ActionSupp ...

  2. Longest Increasing Subsequence HDU - 6284

    /* 首先预处理好f g数组 fi :以a[i]为结尾的 最长上升子序列的长度 gi :以a[i]为开始的 最长上升子序列的长度 mxx : 最长上升子序列的长度 线段树优化 nlogn (不包含a[ ...

  3. luogu 3834 【模板】可持久化线段树 1(主席树)

    我这种菜鸡还是%一下棒神比较好 #include<iostream> #include<cstdio> #include<cmath> #include<cs ...

  4. luogu 3808 【模板】AC自动机(简单版)

    我太菜了 棒神%%% #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib ...

  5. Kaka's Matrix Travels

    http://poj.org/problem?id=3422 #include <stdio.h> #include <algorithm> #include <stri ...

  6. [Swift通天遁地]七、数据与安全-(4)CoreData数据的增、删、改、查

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  7. Akka源码分析-故障恢复

    Actor故障恢复是akka中非常重要的内容,在之前的博客中虽然有介绍,但都是杂糅在其他知识点的细节中,本博客将单独介绍这一部分的故障恢复.为了简化分析的单独,本文只研究用户的actor故障恢复的步骤 ...

  8. Kafka详解与总结(三)

    Kafka分片存储机制 几个kafka重要概念: Broker:消息中间件处理结点,一个Kafka节点就是一个broker,多个broker可以组成一个Kafka集群. Topic:一类消息,例如pa ...

  9. 发布 Windows 服务

    1. 如何新建 Windows 服务 打开VS,“新建项目”-->“windows 桌面”-->“windows 服务”: http://www.cnblogs.com/sorex/arc ...

  10. 洛谷1002 容斥原理+dfs OR DP

    //By SiriusRen #include <bits/stdc++.h> using namespace std; #define int long long ,,,,-,-,-,- ...