题目链接

http://poj.org/problem?id=3984

思路

因为要找最短路 用BFS

而且 每一次 往下一层搜 要记录当前状态 之前走的步的坐标

最后 找到最短路后 输出坐标就可以了

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> #define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss; const double PI = acos(-1);
const double E = exp(1);
const double eps = 1e-30; const int INF = 0x3f3f3f3f;
const int maxn = 5e4 + 5;
const int MOD = 1e9 + 7; int G[5][5];
int v[5][5]; int Move[4][2]
{
-1, 0,
1, 0,
0,-1,
0, 1,
}; struct Node
{
int x, y;
vector <pii> ans;
}tmp; vector <pii> ans; queue <Node> q; bool ok(int x, int y)
{
if (x < 0 || x >= 5 || y < 0 || y >= 5 || v[x][y] || G[x][y])
return false;
return true;
} void bfs()
{
tmp.x = 0;
tmp.y = 0;
tmp.ans.pb(pii(0, 0));
v[tmp.x][tmp.y] = 1;
q.push(tmp);
while (!q.empty())
{
int x = q.front().x;
int y = q.front().y;
ans = q.front().ans;
q.pop();
if (x == 4 && y == 4)
return;
for (int i = 0; i < 4; i++)
{
tmp.x = x + Move[i][0];
tmp.y = y + Move[i][1];
if (ok(tmp.x, tmp.y))
{
tmp.ans = ans;
tmp.ans.pb(pii(tmp.x, tmp.y));
q.push(tmp);
tmp.ans.pop_back();
v[tmp.x][tmp.y] = 1;
}
}
} } int main()
{
CLR(v);
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
scanf("%d", &G[i][j]);
}
bfs();
vector <pii>::iterator it;
for (it = ans.begin(); it != ans.end(); it++)
{
printf("(%d, %d)\n", (*it).first, (*it).second);
}
}

POJ - 3984 迷宫问题 【BFS】的更多相关文章

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

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

  2. POJ 3984 迷宫问题 bfs 难度:0

    http://poj.org/problem?id=3984 典型的迷宫问题,记录最快到达某个点的是哪个点即可 #include <cstdio> #include <cstring ...

  3. [POJ 3984] 迷宫问题(BFS最短路径的记录和打印问题)

    题目链接:http://poj.org/problem?id=3984 宽度优先搜索最短路径的记录和打印问题 #include<iostream> #include<queue> ...

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

  5. poj 3984 迷宫问题 bfs

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

  6. POJ - 3984 迷宫问题 bfs解法

    #include<stdio.h> #include<string.h> #include<algorithm> #include<stack> usi ...

  7. POJ 3984 迷宫问题 (BFS + Stack)

    链接 : Here! 思路 : BFS一下, 然后记录下每个孩子的父亲用于找到一条路径, 因为寻找这条路径只能从后向前找, 这符合栈的特点, 因此在输出路径的时候先把目标节点压入栈中, 然后不断的向前 ...

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

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

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

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

  10. POJ 3984 迷宫问题

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

随机推荐

  1. eos wasm虚拟机相关接口定义实现

    wasm虚拟机相关接口定义实现 执行流程 controller::push_transaction()  // 事务 -> transaction_context::exec()  // 事务 ...

  2. NVIDIA® Quadro® 四路缓冲 3D立体方案

    http://www.nvidia.cn/object/quadro_pro_graphics_boards_cn.html NVIDIA® Quadro® 专业显卡让地球学家以及时装设计师等许多专业 ...

  3. linux中xargs用法

    参数代换: xargs xargs 是在做什么的呢?就以字面上的意义来看, x 是加减乘除的乘号,args 则是 arguments (参数) 的意思,所以说,这个玩意儿就是在产生某个命令的参数的意思 ...

  4. Node.js学习入门手册

    Node.js 安装 1.下载http://nodejs.org/dist/v0.12.1/node-v0.12.1-x86.msi并完成安装 2.下载https://www.python.org/f ...

  5. Android开发人员不得不收集的代码(转)

    App相关→AppUtils.java 安装App installApp 卸载指定包名的App uninstallApp 获取当前App信息 getAppInfo 获取所有已安装App信息 getAl ...

  6. 利用 LeakCanary 来检查 Android 内存泄漏

    前言 你被概率性的 OOM 困扰么?有时候,OOM 像幽灵一样,挥之不去,可真想把它揪出来时,又捉之不着.或许,是时候用 LeakCanary 来诊断一下了.它是一个用来检查 Android 下内存泄 ...

  7. Java reference的种类及使用场景

    Java 中一共有 4 种类型的引用 : StrongReference. SoftReference. WeakReference 以及 PhantomReference (传说中的幽灵引用).这  ...

  8. maven springmvc-hibernate搭建以及源码下载

    见:https://blog.csdn.net/fengshizty/article/details/43635305 下载源码参见:http://www.cnblogs.com/ljy2013/p/ ...

  9. C# 读取Excel中的数据

    #region 读取Excel中的数据 /// <summary> /// 读取Excel中的数据 /// </summary> /// <param name=&quo ...

  10. Xutils的get请求后,总是返回同样数据的问题解决方式

    原因: XUtils中的HttpUtils框架採用的时,HttpUtils对于GET请求採用了LRU缓存处理.默认60秒内提交返回上次成功的结果. 解决方法: HttpUtils http = new ...