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个方格组成,有的 ...
随机推荐
- DEDECMS图片集上传图片出错302的解决办法
无忧主机(www.51php.com)小编今天在调试dede网站的时候发现了一个问题,因为小编想在网站上增加一个图片集的栏目,于是就到后台图片集栏目去添加内容,谁知在上传图片的时候给我弹出个错误信息框 ...
- Microsoft JET Database Engine(0x80004005)未指定错误的解决方法
今天在给一台新的电脑安装IIS,安装成功,建立虚目录后,运行一个已经在别的机器上的正确的asp文件,就是不成功,提示:Microsoft JET Database Engine (0x80004005 ...
- mac python 切换系统默认版本
1 找到所安装python路径/usr/local/Cellar/python/2.7.13/bin2 vim ~/.bash_profile 3 添加如下代码: PATH="/usr/lo ...
- DotnetBrowser入门教程-(3)启动与使用简单的WebSocket服务
websocket是个很好的通信协议,基本可以贯穿支持html5的所有设备.dotnetbrowser内置了对websocket服务端与客户端的支持.请看例子: 1.新建桌面项目,基于.net 4.0 ...
- POJ2405-Beavergnaw
Beavergnaw Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6204 Accepted: 4090 Descri ...
- antialiasing
Aliasing发生的原因在于,采样频率过低, 这样就无法重建更接近原来texture的图像, Temporal aliasing在时域上 采的texture 次数太少 不能重建真实 是帧与帧之间,s ...
- Maven引入Hadoop依赖报错:Missing artifact jdk.tools:jdk.tools:jar:1.6
Maven引入Hadoop依赖报错:Missing artifact jdk.tools:jdk.tools:jar:1.6 原因是缺少tools.jar的依赖,tools.jar在jdk的安装目录中 ...
- el表达式注意
如果action那边是String类型,el表达式进行判断的时候必须加引号,即使是数字也要加. 否则可能导致windows正常,linux出错.
- LeetCode Search in Rotated Sorted Array II -- 有重复的旋转序列搜索
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 使用struts2完成ckeditor和图片上传
代码地址如下:http://www.demodashi.com/demo/12427.html 使用struts2完成ckeditor和ckeditor图片上传 ckeditor版本ckeditor_ ...