ZOJ - 4020 Traffic Light 【BFS】
题目链接
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4020
题意
给出一张地图 以及起点和终点 求是否能从起点走到终点 如果能 求出最小步数 如果不能 输出 -1
然后地图上的0表示在这个点 只能 上下走,,1 只能 左右走
没走一步 地图上 每个1 都变成 0 每个0 都变成 1
思路
那么地图变化 可以用 步数 % 2 来求得 如果 步数 % 2 是 1 那么此时
1 就是 0 0 就是 1 如果 步数 % 2 是 0 那么就按照原地图来算
因为 n 或者 m 特别大 所以用 vector 来保存 地图 用一个 pair(int, int) 前者保存地图 后者保存标记
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 = 1e3 + 5;
const int MOD = 1e9 + 7;
int Move[2][2][2]
{
-1, 0,
1, 0,
0,-1,
0, 1,
};
vector < vector <pii> > G;
vector <pii> tmp;
int sx, sy, ex, ey;
int ans;
int n, m;
struct Node
{
int x, y, step;
}temp;
bool ok(int x, int y)
{
if (x < 0 || x >= n || y < 0 || y >= m)
return false;
return true;
}
queue <Node> q;
void bfs()
{
while (!q.empty())
{
int x = q.front().x;
int y = q.front().y;
int step = q.front().step;
q.pop();
if (x == ex && y == ey)
{
ans = step;
return;
}
int d;
if (step % 2)
d = !G[x][y].first;
else
d = G[x][y].first;
for (int i = 0; i < 2; i++)
{
temp.x = x + Move[d][i][0];
temp.y = y + Move[d][i][1];
if (ok(temp.x, temp.y) && G[temp.x][temp.y].second == 0)
{
temp.step = step + 1;
q.push(temp);
G[temp.x][temp.y].second = 1;
}
}
}
}
int main()
{
int t;
cin >> t;
while (t--)
{
G.clear();
int num;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++)
{
tmp.clear();
for (int j = 0; j < m; j++)
{
scanf("%d", &num);
tmp.pb(pii(num, 0));
}
G.pb(tmp);
}
scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
sx--, sy--, ex--, ey--;
while (!q.empty())
q.pop();
temp.x = sx;
temp.y = sy;
temp.step = 0;
q.push(temp);
ans = -1;
bfs();
cout << ans << endl;
}
}
ZOJ - 4020 Traffic Light 【BFS】的更多相关文章
- ZOJ - 4020 Traffic Light (BFS)
[传送门]http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4020 [题目大意]从起点(sx, sy)出发,要到达(ex , ...
- [ZOJ 4020] Traffic Light
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4020 很简单的一个bfs题,是我想多了. 顺便学习一下C++的S ...
- 【bfs】抓住那头牛
[题目] 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0≤N≤100000),牛位于点K(0≤K≤100000).农夫有两种移动方式: 1.从X移动到X-1或X+1,每次 ...
- 【bfs】拯救少林神棍(poj1011)
Description 乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位.然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度.请你 ...
- 【bfs】Knight Moves
[题目描述] 输入nn代表有个n×nn×n的棋盘,输入开始位置的坐标和结束位置的坐标,问一个骑士朝棋盘的八个方向走马字步,从开始坐标到结束坐标可以经过多少步. [输入] 首先输入一个nn,表示测试样例 ...
- 【bfs】1252 走迷宫
[题目描述] 一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走. 给定一个迷宫,求从左上角走到右下角最少需要走多少步(数据保证一定能走到).只能在水平方向或垂直方向走,不 ...
- 【bfs】献给阿尔吉侬的花束
[题目描述] 阿尔吉侬是一只聪明又慵懒的小白鼠,它最擅长的就是走各种各样的迷宫.今天它要挑战一个非常大的迷宫,研究员们为了鼓励阿尔吉侬尽快到达终点,就在终点放了一块阿尔吉侬最喜欢的奶酪.现在研究员们想 ...
- 【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,0, }; 它表示一个迷 ...
- 【bfs】仙岛求药
[题目描述] 少年李逍遥的婶婶病了,王小虎介绍他去一趟仙灵岛,向仙女姐姐要仙丹救婶婶.叛逆但孝顺的李逍遥闯进了仙灵岛,克服了千险万难来到岛的中心,发现仙药摆在了迷阵的深处.迷阵由M×N个方格组成,有的 ...
随机推荐
- SVN环境搭建详解
SVN服务器搭建和使用(一) Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. 首先来下载和搭建SVN服务器. 现在Subversion已经迁移到apache网 ...
- [置顶]
python字典和nametuple互相转换例子
如果tuple中的元素很多的时候操作起来就比较麻烦,有可能会由于索引错误导致出错. namedtuple对象给tuple命名. 下面的例子可以字典和nametuple互相转换 aa={'verbosi ...
- Linux执行命令unable to create new native thread问题
对于系统的Linux的问题 主要是线程数有限制max user processes 参数限制 修改这个参数涉及到修改两个文件 vi /etc/security/limits.conf 增加如下内容: ...
- mongodb配置副本集(多台服务器间的副本集搭建) replica[ˈrɛplɪkə]
副本集具有多个副本保证了容错性,就算一个副本挂掉了还有很多副本存在,并且解决了“主节点挂掉了,整个集群内会自动切换”的问题.我们来看看mongoDB副本集的架构图: 由图可以看到客户端连接到整个副本集 ...
- struts2获取文件真实路径
CreateTime--2017年8月25日15:59:33 Author:Marydon struts2获取文件真实路径 需要导入: import java.io.FileNotFoundExc ...
- WebRTC for android ios开发官方指南
The WebRTC native code package can be found at: https://chromium.googlesource.com/external/webrtc ht ...
- 基于Apache POI 向xlsx写入数据
[0]写在前面 0.1) these codes are from 基于Apache POI 的向xlsx写入数据 0.2) this idea is from http://cwind.iteye. ...
- 多媒体开发之rtp打包---打包中的FU-A分包方式说明
继上篇rtp中的时间戳和负载类型之后,升入到了nalu的分片打包问题,这里做下笔记 (1)fu-a的打包格式 1.基于RTP协议的打包及解包 (1)单个NAL打包 H.264NALU单元常由[star ...
- C#使用for循环移除HTML标记
public static string StripTagsCharArray(string source) { char[] array = new char[source.Length]; int ...
- freopen - C/C++文件输入输出利器
freopen以前经常使用,比较方便,可以当作模板,在中间替换为自己的代码即可使用. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <stdio.h&g ...