迷宫问题
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7635   Accepted: 4474

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


  广搜,入门题

  题意

  给你一个5*5的迷宫,0代表通路,1代表墙,找到从迷宫左上角到达右下角的最短路径,并输出路径。

  思路

  这道题是一道比较简单的广搜题目,为什么是广搜?因为题意是要找最短路径,这类题基本上就是用广搜。但是与其他直接输出最短路径的步数的不同,这道题要输出的是最短路径,是要输出这个路径,所以就要考虑状态了,每一个状态都应该存储到达这个状态的路径。其他就没什么好说的了,总体上是一道较为简单的广搜入门题。

  代码

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std; bool isw[][];
int a[][];
int dx[] = {,,,-};
int dy[] = {,,-,}; struct Node{
int x;
int y;
int s;
short l[];
}; bool judge(int x,int y)
{
if(x< || x>= || y< || y>=)
return true;
if(isw[x][y])
return true;
if(a[x][y]==)
return true;
return false;
} Node bfs()
{
queue <Node> q;
Node cur,next;
cur.x = ;
cur.y = ;
cur.s = ;
isw[cur.x][cur.y] = true;
q.push(cur);
while(!q.empty()){
cur = q.front();
q.pop();
if(cur.x== && cur.y==)
return cur;
int i,nx,ny;
for(i=;i<;i++){
nx = cur.x + dx[i];
ny = cur.y + dy[i];
if(judge(nx,ny))
continue;
//可以走
next = cur;
next.x = nx;
next.y = ny;
next.s = cur.s + ;
next.l[cur.s] = i;
q.push(next);
}
}
return cur;
} int main()
{
int i,j;
for(i=;i<;i++){ //读入迷宫
for(j=;j<;j++){
scanf("%d",&a[i][j]);
}
}
memset(isw,,sizeof(isw));
Node ans = bfs();
int x,y;
x = ,y = ;
for(i=;i<=ans.s;i++){
printf("(%d, %d)\n",x,y);
x+=dx[ans.l[i]];
y+=dy[ans.l[i]];
}
return ;
}

Freecode : www.cnblogs.com/yym2013

poj 3984:迷宫问题(广搜,入门题)的更多相关文章

  1. POJ3984 BFS广搜--入门题

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

  2. poj 3984 -- 迷宫问题 深搜

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

  3. BFS(最短路+路径打印) POJ 3984 迷宫问题

    题目传送门 /* BFS:额,这题的数据范围太小了.但是重点是最短路的求法和输出路径的写法. dir数组记录是当前点的上一个点是从哪个方向过来的,搜索+,那么回溯- */ /************* ...

  4. POJ 3984 迷宫问题 记录路径的广搜

    主要是学一下如何在广搜中记录路径:每找到一个点我就记录下这个点是由那个点得来的,这样我找到最后个点后,我就可以通过回溯得到我走过的路径,具体看代码吧~ #include<cstdio> # ...

  5. POJ 3984 迷宫问题

    K - 迷宫问题 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  6. poj 3984 迷宫问题 bfs

    学会这道水题之后我懂得了不少哈,首先水题也能学到不少知识,尤其像我这样刚入门的小菜鸟,能学到一些小技巧. 然后就是可以从别人的代码里学到不一样的思路和想法. 这题就是求最短的路径,首先想到就是用bfs ...

  7. POJ 3984 迷宫问题(简单bfs+路径打印)

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

  8. POJ - 1469 COURSES (匈牙利算法入门题)

    题意: P门课程,N个学生.给出每门课程的选课学生,求是否可以给每门课程选出一个课代表.课代表必须是选了该课的学生且每个学生只能当一门课程的. 题解: 匈牙利算法的入门题. #include < ...

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

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

随机推荐

  1. HDU 4707 DFS

    Problem Description One day, Lin Ji wake up in the morning and found that his pethamster escaped. He ...

  2. Webclent基本操作

    /** * @Title: webclientTest.java * @Package webclient * @Description: TODO(用一句话描述该文件做什么) * @author A ...

  3. zaqar项目介绍

    Zaqar is a multi-tenant cloud messaging and notification service for web and mobile developers. It c ...

  4. git config proxy

    $ export http_proxy=http://proxy.ip.ad.ress:portnumber/ $ export https_proxy=http://proxy.ip.ad.ress ...

  5. 【GoLang】GoLang 中 make 与 new的区别

    make.new操作 make用于内建类型(map.slice 和channel)的内存分配.new用于各种类型的内存分配. 内建函数new本质上说跟其它语言中的同名函数功能一样:new(T)分配了零 ...

  6. emmet 太 hackble 了 。。。

    http://www.open-open.com/lib/view/open1451954899292.html

  7. strcpy C++实现

    #include <iostream> #include <assert.h> using namespace std; char *strcpy(char *strDest, ...

  8. Python 包管理工具解惑

    Python 包管理工具解惑 本文链接:http://zengrong.net/post/2169.htm python packaging 一.困惑 作为一个 Python 初学者,我在包管理上感到 ...

  9. selenium web driver 配合使用testng

    首先为eclipse添加testng插件 步骤如下:help->Install New SoftWare... 2. 添加testng链接,该链接可以在这里找到 For the Eclipse ...

  10. 【网络】VPN和代理服务器的区别

    来自:http://www.zhihujingxuan.com/19311.html [scotttony的回答(41票)]: VPN和ssh哪个比较好, 要看你怎么定义是“好”. ssh作为一个创建 ...