题目大意

题目不需要大意,poj居然还有中文题

鸣谢

特别鸣谢ljc大佬提供的方法!!!

解法

我们可能输出个最短路径的长度比较简单,但是输出最短路径真的是没有做过,这里有一种简单的方法

因为我们的dfs是把所有的情况都遍历了一遍,那么是不是我们的答案的路径就在这其中呢?

这样我们在判断条件的时候把老生常谈的那些什么路径数记录的变成如果sum<minn更新minn,然后记录这些路径

回溯的时候,这里dfs用了三个参数x,y,cur。把cur回溯顺便就回溯了路径

代码(为啥没有高亮。。。)

#include <iostream>
#include <cstdio>
using namespace std;
int mp[5][5],bk[5][5];
int dx[6]={-1,1,0,0};
int dy[6]={0,0,-1,1};
int minn=9999,sum,p,pp;
struct node
{
int x,y;
}ans[25],now[25];
void dfs(int x,int y,int cur)
{
if(x==4&&y==4)
{
if(cur<minn)
{
minn=cur;
for(int i=0;i<cur;i++)
{
ans[i].x=now[i].x;
ans[i].y=now[i].y;
}
}
}
else
{
for(int i=0;i<4;i++)
{
int xx=x+dx[i];
int yy=y+dy[i];
if(!bk[xx][yy]&&mp[xx][yy]==0&&xx>=0&&yy>=0&&xx<5&&yy<5)
{
bk[xx][yy]=1;
now[cur].x=x;
now[cur].y=y;
dfs(xx,yy,cur+1);
bk[xx][yy]=0;
now[cur].x=0;
now[cur].y=0;
}
}
}
}
int main()
{
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
cin>>mp[i][j];
bk[0][0]=1;
dfs(0,0,0);
for(int i=0;i<minn;i++)
printf("(%d, %d)\n",ans[i].x,ans[i].y);
printf("(4, 4)");
}

迷宫问题 POJ - 3984 (搜索输出路径)的更多相关文章

  1. - 迷宫问题 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, 0, 1, ...

  2. Q - 迷宫问题 POJ - 3984(BFS / DFS + 记录路径)

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

  3. 迷宫问题-POJ 3984

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

  4. POJ 3414 Pot (输出路径)【BFS】

    <题目链接> 题目大意: 有两个容量的空杯子,能够对这两个空杯子进行三种操作: 分别是fill(a),装满a杯子: drop(a),倒空a杯子: pour(a,b),将a杯子中的水倒入b杯 ...

  5. kuangbin专题 专题一 简单搜索 迷宫问题 POJ - 3984

    题目链接:https://vjudge.net/problem/POJ-3984 这个题目,emm,上代码,看的估计应该是刚开始接触搜索的,我带点注释,你能慢慢理解. #include <ios ...

  6. 迷宫问题 POJ - 3984 [kuangbin带你飞]专题一 简单搜索

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

  7. hdu 1026 Ignatius and the Princess I 搜索,输出路径

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  8. poj3984 迷宫问题(简单的输出路径的bfs)

    题目链接 http://poj.org/problem?id=3984 中文题题意不解释了 反正就是简单的结构体套结构体存一下路径就行了 #include <iostream> #incl ...

  9. K - 迷宫问题 POJ - 3984

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

随机推荐

  1. Resharper 实现接口的方式

  2. PDO连接mysql8.0报PDO::__construct(): Server sent charset (255) unknown to the client. Please, report to the developers错误

    安装mysql8.0之后,尝试使用php连接mysql,总是报PDO::__construct(): Server sent charset (255) unknown to the client. ...

  3. luogu 1726 上白泽惠音

    题目大意: 给一个有向图 求一个最大的强连通分量,输出这个强连通分量里的所有元素 若两个联通分量内点数相同 则输出字典序小的那个 思路: 直接tarjan 对每个连通分量,求一下最小点,然后判断字典序 ...

  4. bzoj1999 (洛谷1099) 树网的核——dfs

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1999  https://www.luogu.org/problemnew/show/P109 ...

  5. 洛谷P1291 [SHOI2002]百事世界杯之旅——期望DP

    题目:https://www.luogu.org/problemnew/show/P1291 水水的经典期望DP: 输出有毒.(其实也很简单啦) 代码如下: #include<iostream& ...

  6. 打开mat文件

    点击file目录,选择import data 然后选择所需.mat文件,就可以打开了

  7. 近年来火热的人工智能,其实是IT业界的一个障眼法

    近年来火热的人工智能,其实是IT业界的一个障眼法,仗着现在的计算机的计算能力牛B,把一个类仿生统计算法,宣传成了人工智能,不得不感叹一些营销人士的牛逼,说大话不腰疼.当然谎言重复一千遍也许自己也就信了 ...

  8. 下载tortoisegit

    https://download.tortoisegit.org/tgit/ 藏经阁技术资料分享群二维码

  9. Y-C

    1.asp.net服务控件生命周期 11个生命阶段 (1)初始化: 初始化在传入Web请求生命周期内所需的设置,.跟踪视图状态.页面框架通过默认方式引发Init事件,并调用OnInit()方法,控件开 ...

  10. 转 awr自动收集脚本

    1. remote get awr report #!/usr/bin/ksh ####sample: sh awr.sh 20170515 20170516 AWR ### default it w ...