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 ...
随机推荐
- 2017.2.28 activiti实战--第六章--任务表单(一)动态表单
学习资料:<Activiti实战> 第六章 任务表单(一)动态表单 内容概览:本章要完成一个OA(协同办公系统)的请假流程的设计,从实用的角度,讲解如何将activiti与业务紧密相连. ...
- 移植MonkeyRunner的图片对照和获取子图功能的实现-UiAutomator/Robotium篇
依据前一篇文章<移植MonkeyRunner的图片对照和获取子图功能的实现-Appium篇>所述,由于Appium和MonkeyRunner有一个共同点--代码控制流程都是在client实 ...
- 线程安全的概念和Synchronized(读书笔记)
并行程序开发的一大关注重点就是线程安全,一般来说,程序并行化为了获取更多的执行效率,但前提是,高效率不能以牺牲正确性为代价,线程安全就是并行程序的根本和根基.volatile并不能真正保证线 ...
- 一次JVM调优的笔记
1. JVM Tuning基础知识 1.1 Java堆结构 Java堆可以处于物理上不连续的内存空间上,只要逻辑上是连续的即可.Java堆就是各种对象分配和保存的内存空间,线程间共享.Java堆分为E ...
- 在 Linux 多节点安装配置 Apache Zookeeper 分布式集群
规划: 三台物理服务器就形成了(法定人数).对于高可用性集群,您可以使用高于3的任何奇数.例如,如果设置5台服务器,则集群可以处理两个故障节点等. 物理服务器需要开启的端口 2888 , 3888 和 ...
- SQL系列函数--字符串函数
1.charindex函数用来寻找一个指定的字符(串)在另一个字符串中的起始位置,返回一个整数,没找到就返回0 select CHARINDEX('SQL','Microsoft SQL SERVER ...
- Spring学习七----------Bean的配置之自动装配
© 版权声明:本文为博主原创文章,转载请注明出处 Bean的自动装配(Autowiring) no:不启用自动装配,此时需要手动注入.参考:Spring学习三----------注入方式 defaul ...
- less1.5中的减错误
(@bodywidth)-@leftwidth即在第一个变量外加括号---正确 @bodywidth-@leftwidth即直接使用不加括号--------错误
- 网络工具的瑞士军刀netcat
这是一个聒噪的夜晚,假设要给出个原因.可能是由于尽管我认为西班牙不纯粹,可是怎么也不至于干为人家搭台面自己不唱戏的角色吧..结束以后.我认为该玩一下素有网络瑞士军刀之称谓的netcat了. 尽管瑞士军 ...
- Android中BaseAdapter使用基础点
Android中要填充一些控件(如ListView)经常须要用到Adapter来实现,经常使用的有ArrayAdapter,SimpleAdapter, CursorAdapter,BaseAdapt ...