题目链接

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】的更多相关文章

  1. ZOJ - 4020 Traffic Light (BFS)

    [传送门]http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4020 [题目大意]从起点(sx, sy)出发,要到达(ex , ...

  2. [ZOJ 4020] Traffic Light

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4020 很简单的一个bfs题,是我想多了. 顺便学习一下C++的S ...

  3. 【bfs】抓住那头牛

    [题目] 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0≤N≤100000),牛位于点K(0≤K≤100000).农夫有两种移动方式: 1.从X移动到X-1或X+1,每次 ...

  4. 【bfs】拯救少林神棍(poj1011)

    Description 乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位.然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度.请你 ...

  5. 【bfs】Knight Moves

    [题目描述] 输入nn代表有个n×nn×n的棋盘,输入开始位置的坐标和结束位置的坐标,问一个骑士朝棋盘的八个方向走马字步,从开始坐标到结束坐标可以经过多少步. [输入] 首先输入一个nn,表示测试样例 ...

  6. 【bfs】1252 走迷宫

    [题目描述] 一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走. 给定一个迷宫,求从左上角走到右下角最少需要走多少步(数据保证一定能走到).只能在水平方向或垂直方向走,不 ...

  7. 【bfs】献给阿尔吉侬的花束

    [题目描述] 阿尔吉侬是一只聪明又慵懒的小白鼠,它最擅长的就是走各种各样的迷宫.今天它要挑战一个非常大的迷宫,研究员们为了鼓励阿尔吉侬尽快到达终点,就在终点放了一块阿尔吉侬最喜欢的奶酪.现在研究员们想 ...

  8. 【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, }; 它表示一个迷 ...

  9. 【bfs】仙岛求药

    [题目描述] 少年李逍遥的婶婶病了,王小虎介绍他去一趟仙灵岛,向仙女姐姐要仙丹救婶婶.叛逆但孝顺的李逍遥闯进了仙灵岛,克服了千险万难来到岛的中心,发现仙药摆在了迷阵的深处.迷阵由M×N个方格组成,有的 ...

随机推荐

  1. Cannot create JDBC driver of class '' for connect URL 'null'问题解决方法2

    1)启动Tomcat服务器,打开浏览器,输入http://localhost:8080/admin(其中localhost是名称服务器或称为主机),进入管理界面的登陆页面,这时候请输入原来安装时要求输 ...

  2. dedecms图片列表效果调用

    效果如图 代码如下: <div class="listbox"> <ul class="e1"> {dede:list pagesize ...

  3. GIS可视化

    作为一名GIS专业的学生,一晃也毕业三年了,在supermap也呆了三年多了,做的最多的就是浏览器端的GIS展示,最近也想分享一下我们团队在浏览器端GIS可视化的一些成果,算是做个宣传吧!有用的着的可 ...

  4. SparkStreaming和Drools结合的HelloWord版

    关于sparkStreaming的测试Drools框架结合版 package com.dinpay.bdp.rcp.service; import java.math.BigDecimal; impo ...

  5. 2016.3.23 集成新版activiti-modeler(5.17+)到项目中

    书:<activiti实战> 博客: http://www.kafeitu.me/activiti/2013/03/10/integrate-activiti-modeler.html h ...

  6. 2016.11.4 Injection of autowired dependencies failed

    运行项目时,提示错误: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ' ...

  7. C# Color颜色对照表

    Color命名空间  using System.Drawing; Color.AliceBlue 240,248,255 Color.LightSalmon 255,160,122 Color.Ant ...

  8. 重读金典------高质量C编程指南(林锐)-------第五章 常量

    5.1  为什么需要常量 1)为了便于用户理解,增加程序的可读性. 2)在程序的很多地方都用到同一个常量,用某一个宏常量来定义可以减少错误. 规则:尽可能的使用含义直观明确的常量来表示程序中多次出现的 ...

  9. View的滚动原理简单解析

    一直对View的滚动了解的不深,说明确了吧也能说出个所以然来,所以我就花了点时间做了一个小小的总结,言归正传,view的滑动分为下面三种: 1)View本身不滚动,指滚动View的内容,这也是View ...

  10. mysql 让一个存储过程定时作业的代码(转)

    1.在mysql 中建立一个数据库 test1 语句:create database test1 2.创建表examinfo create table examinfo( id int auto_in ...