题目链接

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. Can''t find the channel handler for deviceType 工行 个人网银 错误

    背景描述:系统Win7,浏览器IE8.登录工商银行个人网银的时候,输入帐号密码和验证码后,出现空白页面,上面一句话  Can''t find the channel handler for devic ...

  2. http://preshing.com/

    http://preshing.com/ http://mechanical-sympathy.blogspot.com/

  3. Linux学习之十五-Linux文件特殊权限和附加权限

    Linux文件特殊权限和附加权限 1.特殊权限suid 范围:只能针对二进制命令文件 作用:让普通用户拥有二进制命令文件所有者的权限 举例1:普通用户使用passwd命令修改密码 cat /etc/s ...

  4. JAVA基础针对自己薄弱环节总结01(循环之前的知识)

    java中的标识符 组成:数字.字母.下划线.美元$符号组成.  规则:不能由数字开头.  类名:每一个单词的首字母大写  包名:所有小写  变量名.方法名:第一个单词首字母小写.后面首字母大写  常 ...

  5. Direct-X学习笔记--纹理映射

    一.介绍 之前学习了如何绘制物体,还画了个DX自带的茶壶,然而这个东东并不怎么好看....离我们现实的物体简直相隔千里. 仅仅能说像美术他们用来写生的模型...那么要怎么样才干让我们的东西看起来更像真 ...

  6. EasyUI这个框架用了好久了,总结一下遇到的问题和解决方法

    1. jQuery EasyUI动态添加控件或者ajax加载页面后不能自动渲染问题的解决方法: 我们之所以在页面中,只要书写相应easyui的class,Easyui就能成功渲染页面,这是因为解析器在 ...

  7. shell脚本实现定时重启进程

    ##############################Deploy crontab for yechang ad*******eta restart ###################### ...

  8. UBUNTU : Destination Host Unreachable

    介绍我的系统的搭建的方式: WIN7 64 + VMWARE STATION,方式是进行桥接的方式.最近突然出现了问题,Ubuntu ping 外网或者 PING WIN 7 的时候,出现 Desti ...

  9. (六)jQuery选择器

    jQuery 的选择器可谓之强大无比,这里简单地总结一下常用的元素查找方法: $("#myELement") 选择id值等于myElement的元素,id值不能重复在文档中只能有一 ...

  10. SpringBoot启动流程分析(二):SpringApplication的run方法

    SpringBoot系列文章简介 SpringBoot源码阅读辅助篇: Spring IoC容器与应用上下文的设计与实现 SpringBoot启动流程源码分析: SpringBoot启动流程分析(一) ...