首页 > 其他分享 >poj 3984 迷宫问题(BFS+输出路径)

poj 3984 迷宫问题(BFS+输出路径)

时间:2023-02-02 11:34:02浏览次数:44  
标签:map arr bfs int mark BFS poj 3984 include




迷宫问题


Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 11323

 

Accepted: 6776


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


题目分析:

此题的移动方向已经很明确要么下要么右







<span style="font-size:24px;">#include<cstdio>
#include<cstring>
#include<vector>
#include<stack>
#include<iostream>
using namespace std;
int mark[7][7],map[7][7];
struct node
{
int a;
int b;
};
stack<node> q;
void bfs(int x,int y)
{

if(!mark[x][y])
{
node arr;
arr.a=x;
arr.b=y;
mark[x][y]=1;
q.push(arr);
}
if(x==5&&y==5) return ;
if(!mark[x+1][y]&&!map[x+1][y]) bfs(x+1,y);
else if(!mark[x][y+1]&&!map[x][y+1]) bfs(x,y+1);
else
{
node arr;
q.pop();
arr=q.top();
bfs(arr.a,arr.b);
}
}
int main()
{
for(int i=1;i<=5;i++)
for(int j=1;j<=5;j++)
//cin>>map[i][j];
scanf("%d",&map[i][j]);
memset(mark,0,sizeof(mark));
for(int i=0;i<7;i++)
{
map[0][i]=1;
map[i][0]=1;
map[i][6]=1;
map[6][i]=1;
}
bfs(1,1);
vector<node> v;
while(!q.empty())
{
v.push_back(q.top());
q.pop();
}
for(int i=v.size()-1;i>=0;i--)
printf("(%d, %d)\n",v[i].a-1,v[i].b-1);
return 0;
}</span>



























标签:map,arr,bfs,int,mark,BFS,poj,3984,include
From: https://blog.51cto.com/u_14235050/6033424

相关文章

  • POJ-2752-Seek the Name, Seek the Fame
    SeektheName,SeektheFameTimeLimit:4000/2000ms(Java/Other)   MemoryLimit:131072/65536K(Java/Other)TotalSubmission(s):43   AcceptedSubmis......
  • DO、DTO、BO、AO、VO、POJO定义和转换的正确姿势
    一、引言DO、DTO、BO、AO、VO、POJO的概念看似简单,但是想区分好或者理解好也不容易,本文简单梳理一下。通过各层POJO的使用,有助于提高代码的可读性和可维护性。----------......
  • 「POJ3076」Sudoku TJ
    「POJ3076」SudokuTJ为什么要用DLX呢?DFS他不香吗!题意:略去不讲(数独还没玩过?滚回去玩吧!)思路:DFS,判断方案是否可行。\(9\times9\)的数独问题中,我们采用的策略是:优......
  • 广度优先搜索算法 BFS 实战 All In One
    广度优先搜索算法BFS实战AllInOneBreadth-FirstSearch/BFSBFS/广度优先搜索/广度优先遍历/按层次遍历树demosLeetCode"usestrict";/****......
  • 倍增+bfs 清楚姐姐逛街
    题目https://ac.nowcoder.com/acm/contest/46812/H题意地图大小N*M,障碍物为“#”,地图上其他所有点有一个字母(“LRUD”之一,表示走的方向;“.”表示A停止)有两个人A和B,A......
  • POJ--3169 Layout(最短路)
    记录12:362023-1-30http://poj.org/problem?id=3169reference:《挑战程序设计竞赛(第2版)》2.5.6p111DescriptionLikeeveryoneelse,cowsliketostandcloseto......
  • Knight Moves POJ-1915 <bfs>
    KnightMovesTimeLimit: 1000MS MemoryLimit: 30000KTotalSubmissions: 37011 Accepted: 17105DescriptionBackgroundMrSomurolov,fabulous......
  • POJ--3723 Conscription(最小生成树)
    记录18:302023-1-29http://poj.org/problem?id=3723reference:《挑战程序设计竞赛(第2版)》2.5.6p109DescriptionWindyhasacountry,andhewantstobuildana......
  • <Web Navigation> stack-POJ1028
    WebNavigationTimeLimit: 1000MS MemoryLimit: 10000KTotalSubmissions: 41995 Accepted: 18687DescriptionStandardwebbrowserscontainf......
  • 图文:深度优先遍历(DFS)和广度优先遍历(BFS)
    /***注意:left/right值若没有显示设置为null,值即为undefined*在调用二叉树前、中、后序遍历方法时,由于参数设置了默认值(tree)*所以进入了死循环*/consttree={......