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个方格组成,有的 ...
随机推荐
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock'
原因:找不到mysql.sock文件 解决方法: 1 找到mysql.sock文件位置 echo "show variables" | mysql | grep "soc ...
- Linux下防火墙iptables用法规则详及其防火墙配置
转:http://www.linuxidc.com/Linux/2012-08/67952.htm iptables规则 规则--顾名思义就是规矩和原则,和现实生活中的事情是一样的,国有国法,家有家规 ...
- Java reference的种类及使用场景
Java 中一共有 4 种类型的引用 : StrongReference. SoftReference. WeakReference 以及 PhantomReference (传说中的幽灵引用).这 ...
- 日历插件js,jquery
常用的日历插件 DatePicker My97DatePicker 文章来源:刘俊涛的博客 地址:http://www.cnblogs.com/lovebing 欢迎关注,有问题一起学习欢迎留言. ...
- UNP学习笔记(第三章:套接字编程简介)
本章开始讲解套接字API. 套接字地址结构 IPv4套接字地址结构 它以sockaddr_in命名,下面给出它的POSIX定义 struct in_addr { in_addr_t s_addr; } ...
- hdfs笔记
Distributed File System 数据量越来越多,在一个操作系统管辖的范围存不下了,那么就分配到更多的操作系统管理的磁盘中,但是不方便管理和维护,因此迫切需要一种系统来管理多台机器上的文 ...
- linux下编译ffmpeg 引入外部库x264
Found no assembler Minimum version is nasm-2.13 If you really want to compile without asm, configure ...
- java 内存与内存溢出
学习自:http://www.codeceo.com/article/jvm-memory-overflow.html 讲的很清楚
- git 操作分支
1. git 查看本地分支:git branch 2. git 查看所有分支:git branch -a 3. git 新建本地分支:git branch branchName 4. git 新建分支 ...
- python thrift hbase安装连接
默认已装好 hbase,我的版本是hbase-0.98.24,并运行 python 2.7.x 步骤: sudo apt-get install automake bison flex g++ git ...