poj3984
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)
解题思路
广搜问题:在正常基础上多定义一个父节点,可以根据结构体回溯
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int a[6][6],book[6][6];
struct node
{
int x;
int y;
int s;
int f;
}que[100];
int main()
{
int next[4][2]={1,0,0,-1,-1,0,0,1};
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
scanf("%d",&a[i][j]);
int head=1,tail=1;
que[head].x=0;
que[head].y=0;
que[head].s=0;
que[head].f=0;
book[1][1]=1;
tail++;
int flag=0,tx,ty;
while(head<tail)
{
for(int k=0;k<=3;k++)
{
tx=que[head].x+next[k][0];
ty=que[head].y+next[k][1];
if(tx<0||tx>4||ty<0||ty>4)
continue;
if(a[tx][ty]==0&&book[tx][ty]==0)
{
book[tx][ty]=1;
que[tail].x=tx;
que[tail].y=ty;
que[tail].s=que[head].s+1;
que[tail].f=head;
tail++;
}
if(tx==4&&ty==4)
{
flag=1;
break;
}
}
if(flag==1)
break;
head++;
}
int ax[30]={0},ay[30]={0};
int z=-1;
ax[++z]=que[tail-1].x;
ay[z]=que[tail-1].y;
int sum=que[tail-1].s;
tail=tail-1;
for(int i=1;i<sum;i++)
{
tail=que[tail].f;
ax[++z]=que[tail].x;
ay[z]=que[tail].y;
}
ax[++z]=0;
ay[z]=0;
for(int j=z;j>=0;j--)
printf("(%d, %d)\n",ax[j],ay[j]);
return 0;
}
poj3984的更多相关文章
- BFS算法入门--POJ3984
迷宫问题–POJ3984 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22008 Accepted: 12848 Descri ...
- poj3984迷宫问题 广搜+最短路径+模拟队列
转自:http://blog.csdn.net/no_retreats/article/details/8146585 定义一个二维数组: int maze[5][5] = { 0, 1, 0, ...
- 迷宫bfs POJ3984
#include<stdio.h> int map[5][5]={0,1,0,0,0, 0,1,0,1,0, 0,0,0,0,0, 0,1,1,1,0, ...
- 暑假集训(1)第八弹 -----简单迷宫(Poj3984)
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, ...
- POJ3984 迷宫问题
典型BFS. #include <iostream> #include <memory.h> #include <queue> #include <map&g ...
- poj3984(经典dfs)
题目链接:http://poj.org/problem?id=3984 分析:直接深搜从起点到终点,如何取最短路线,其实只要优先向下或向右走即可. #include <cstdio> #i ...
- poj3984迷宫问题
一个5 × 5的二维数组,表示一个迷宫.其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线. 很简单的一道题,迷宫问题,一般都选择两种优先搜索 ...
- [poj3984]迷宫问题_bfs
迷宫问题 题目大意:给你一个5*5的矩阵,求左上角到左下角的最短路径. 注释:0或1的矩阵,1表示不能走,0表示能走,保证有唯一最短路径. 想法:bfs爆搜练习题.通过其实点,定义方向数组,然后进行b ...
- poj3984迷宫问题(DFS广搜)
迷宫问题 Time Limit: 1000MSMemory Limit: 65536K Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, ...
随机推荐
- JavaWeb学习笔记——JavaBean的保存范围和删除
- thinkphp模板引擎
$this->assing('result'.$result) html页面代码 <foreach name='result item='vo'> <div>{$vo[' ...
- UI控件之ListView
一,一个简单的TextView列表 public class FirstActivity extends Activity { private String[] data = {"Apple ...
- Robot Framework--06 用户关键字User Keyword
转自:http://blog.csdn.net/tulituqi/article/details/7906130 在我们做自动化案例的时候,用的最多的主要是用户关键字.说到关键字,大体上可以分为测试库 ...
- curl 或 file_get_contents 获取需要授权页面的方法
原文:http://blog.csdn.net/fdipzone/article/details/44475801 红色字体部分是加上自己的注释,整理了一下. 今天因工作需要,需要用 curl / f ...
- imageserver
https://bitbucket.org/tamtam-nl/tamtam-nuget-imageserver/overview https://www.nuget.org/packages/Tam ...
- shutdown命令用法
首先我们先创建一个txt文件,添加shutdown -r -f -t 0 ,文件点击另存为,选择所有类型,保存格式为“重启.bat”文件. 说明:shutdown命令用法: /r 关闭 ...
- Mysql利用mysql_multi配置一台主机多个实例(转)
在Mysql官方帮助文档中,详细记录中Mysql的启动方式,有mysqld_safe.mysql.server.mysql_multi这三种.关于mysql_multi的介绍: Mysqld_mult ...
- php生成唯一随机码
最终使用: echo md5(time() . mt_rand(1,1000000)) //A:利用时间戳的方法 md5("admin"); // B:32位MD5加密 subst ...
- cmd /c和cmd /k 解释,附★CMD命令★ 大全
cmd /c和cmd /k http://leaning.javaeye.com/blog/380810 java的Runtime.getRuntime().exec(commandStr)可以调用执 ...