POJ - 3984 迷宫问题 【BFS】
题目链接
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】的更多相关文章
- POJ 3984 - 迷宫问题 - [BFS水题]
		题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ... 
- POJ 3984 迷宫问题 bfs 难度:0
		http://poj.org/problem?id=3984 典型的迷宫问题,记录最快到达某个点的是哪个点即可 #include <cstdio> #include <cstring ... 
- [POJ 3984] 迷宫问题(BFS最短路径的记录和打印问题)
		题目链接:http://poj.org/problem?id=3984 宽度优先搜索最短路径的记录和打印问题 #include<iostream> #include<queue> ... 
- 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, ... 
- poj 3984 迷宫问题 bfs
		学会这道水题之后我懂得了不少哈,首先水题也能学到不少知识,尤其像我这样刚入门的小菜鸟,能学到一些小技巧. 然后就是可以从别人的代码里学到不一样的思路和想法. 这题就是求最短的路径,首先想到就是用bfs ... 
- POJ - 3984   迷宫问题 bfs解法
		#include<stdio.h> #include<string.h> #include<algorithm> #include<stack> usi ... 
- POJ 3984 迷宫问题 (BFS + Stack)
		链接 : Here! 思路 : BFS一下, 然后记录下每个孩子的父亲用于找到一条路径, 因为寻找这条路径只能从后向前找, 这符合栈的特点, 因此在输出路径的时候先把目标节点压入栈中, 然后不断的向前 ... 
- BFS(最短路+路径打印) POJ 3984 迷宫问题
		题目传送门 /* BFS:额,这题的数据范围太小了.但是重点是最短路的求法和输出路径的写法. dir数组记录是当前点的上一个点是从哪个方向过来的,搜索+,那么回溯- */ /************* ... 
- POJ 3984 迷宫问题(简单bfs+路径打印)
		传送门: http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions ... 
- POJ 3984 迷宫问题
		K - 迷宫问题 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Sta ... 
随机推荐
- Linux执行命令unable to create new native thread问题
			对于系统的Linux的问题 主要是线程数有限制max user processes 参数限制 修改这个参数涉及到修改两个文件 vi /etc/security/limits.conf 增加如下内容: ... 
- 2017.2.21 activiti实战--第十三章--流量数据查询与跟踪(一)查询接口介绍及运行时数据查询
			学习资料:<Activiti实战> 第十三章 流量数据查询与跟踪 本章讲解运行时与历史数据的查询方法.主要包含三种:标准查询,Native查询,CustomSql查询. 13.1 Quer ... 
- Arduino MEGA 2560找不到驱动怎么办
			刚买了Arduino MEGA 2560(比Arduino UNO稍微高级一点的板子),按照视频一步一步操作(似乎插板子也不太一样,不管他,能插上去就完事了),但是到了代码烧录的时候,点击Tools- ... 
- 微信小程序 - 关闭当前页面无法再通过左上角返回
			考试的时候不可能答完以后,得到成绩后再通过左上角返回再重新答吧? 可以通过:open-type='redirectTo'实现 
- 手把手教你画AndroidK线分时图及指标
			先废话一下:来到公司之前.项目是由外包公司做的,面试初,没有接触过分时图k线这块,认为好难,我能搞定不.可是一段时间之后,发现之前做的那是一片稀烂,可是这货是主功能啊.迟早的自己操刀,痛下决心,开搞, ... 
- Node.js学习笔记(2)——关于异步编程风格
			Node.js的异步编程风格是它的一大特点,在代码中就是体现在回调中. 首先是代码的顺序执行: function heavyCompute(n, callback) { var count = 0, ... 
- layui-字体图标
			layui官网下载:GitHub:https://github.com/sentsin/layui/ layui官网首页-下载:http://www.layui.com/ layui-字体图标-官方网 ... 
- leetcode_Multiply Strings
			描写叙述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ... 
- 如何使用CocoaPods安装使用及配置私有库以及管理依赖库 【原创】
			CocoaPods是什么 在iOS开发中势必会用到一些第三方依赖库,比如大家都熟悉的ASIHttpRequest.AFNetworking.JSONKit等.使用这些第三方类库能极大的方便项目的开发, ... 
- items" does not support runtime expression
			<%@taglib prefix="c" uri="http://java.sun.com/jstl/core"%> 更改为 <%@tagl ... 
