[ZOJ 4020] Traffic Light
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4020
很简单的一个bfs题,是我想多了。
顺便学习一下C++的STL中的vector的用法:https://www.cnblogs.com/youpeng/p/10779019.html
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 300005;
vector<int> vec[maxn];
vector<bool> vis[maxn];
//前两个是横着走,后两个是竖着走
int gox[4] = {0, 0, -1, 1};
int goy[4] = {-1, 1, 0, 0};
int test;
int n, m;
int sx, sy, ex, ey;
struct node {
int x, y;
int dis;
};
bool judge(node nex) {
if (nex.x < 1 || nex.x > n || nex.y < 1 || nex.y > m || vis[nex.x][nex.y])
return false;
else
return true;
}
int bfs() {
node cu, ne;
cu.x = sx, cu.y = sy;
cu.dis = 0;
vis[cu.x][cu.y] = true;
queue<node> q;
q.push(cu);
while (!q.empty()) {
cu = q.front();
q.pop();
//判断是否满足条件
if (cu.x == ex && cu.y == ey) {
return cu.dis;
}
int status = vec[cu.x][cu.y];
if (cu.dis % 2) {
if (status)
status = 0;
else
status = 1;
}
if (status) { //横着走
for (int i = 0; i < 2; i++) {
ne.x = cu.x + gox[i];
ne.y = cu.y + goy[i];
if (judge(ne)) {
vis[ne.x][ne.y] = true;
ne.dis = cu.dis + 1;
q.push(ne);
}
}
} else { //竖着走
for (int i = 2; i < 4; i++) {
ne.x = cu.x + gox[i];
ne.y = cu.y + goy[i];
if (judge(ne)) {
vis[ne.x][ne.y] = true;
ne.dis = cu.dis + 1;
q.push(ne);
}
}
}
}
return -1;
}
int main() {
scanf("%d", &test);
while (test--) {
scanf("%d%d", &n, &m);
for (int i = 0; i <= n; i++) {
vec[i].clear();
vis[i].clear();
}
int x;
for (int i = 1; i <= n; i++) {
vec[i].push_back(0);
vis[i].push_back(false);
for (int j = 1; j <= m; j++) {
scanf("%d", &x);
vec[i].push_back(x);
vis[i].push_back(false);
}
}
scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
int ans = bfs();
if (ans >= 0) {
printf("%d\n", ans);
} else {
printf("-1\n");
}
}
return 0;
}
[ZOJ 4020] Traffic Light的更多相关文章
- ZOJ - 4020 Traffic Light 【BFS】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4020 题意 给出一张地图 以及起点和终点 求是否能从起点走到终点 ...
- ZOJ - 4020 Traffic Light (BFS)
[传送门]http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4020 [题目大意]从起点(sx, sy)出发,要到达(ex , ...
- zoj 4020 The 18th Zhejiang University Programming Contest Sponsored by TuSimple - G Traffic Light(广搜)
题目链接:The 18th Zhejiang University Programming Contest Sponsored by TuSimple - G Traffic Light 题解: 题意 ...
- Gym 101775C - Traffic Light - [思维题]
题目链接:http://codeforces.com/gym/101775/problem/C 题意: 给出 $N$ 个红绿灯,又给出 $N+1$ 个距离 $S_i = S_0,S_1, \cdots ...
- ZOJ2018/4月月赛G题Traffic Light(广搜)
题意:首先T组数据,每组数据包括:第一行:一个n,m,然后下面有一个n行m列的01矩阵. 最后一行输入四个数字,分别是起点的横纵坐标,终点的横纵坐标.询问从起点到终点,最少要几步,如果到不了输出-1 ...
- 152 - - G Traffic Light 搜索(The 18th Zhejiang University Programming Contest Sponsored by TuSimple )
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5738 题意 给你一个map 每个格子里有一个红绿灯,用0,1表示 ...
- zoj4020 Traffic Light(bfs+状态压缩)
题意:每个点有两种状态,0/1,0表示只能上下方向走,1表示只能左右方向走.每走一步整个图的状态改变一次(即0->1,1->0). 数据范围:n,m<=1e15 开始迷之因为数组太大 ...
- 快速切题 sgu103. Traffic Lights 最短路 难度:1
103. Traffic Lights Time limit per test: 0.25 second(s)Memory limit: 4096 kilobytes input: standardo ...
- The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online
A Live Love DreamGrid is playing the music game Live Love. He has just finished a song consisting of ...
随机推荐
- 一种JNI混淆方案
转载自:http://www.liuling123.com/2016/06/so_method_mix.html 感谢原作者 侵删 默认情况下,使用JNI时与native对应的JNI函数名都是Java ...
- Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/maven/cli/MavenCli : Unsupported major.minor version 51.0
原因:JDK版本不兼容.运行的Java Class文件采用高版本的JDK编译,然后在低版本的JRE环境中运行,有时会报此错误. 解决:1)升级JDK 2)调整Eclipse中Java Compiler ...
- js初级练习
知识点:1.for循环的运用:2.this的使用.索引值:3自定义属性 说明:获取到的元素下文用element代替 学习涉及到的知识: 清除元素类名: element.className=" ...
- 代码混淆工具——Virbox Protector Standalone
VirboxProtector Standalone 加壳工具可对代码加密的技术有:代码混淆.代码虚拟化.代码加密. 代码混淆:利用花指令和代码非等价变形等技术,将程序的代码,转换成一种功能上等价,但 ...
- monent API详解
参考链接:https://www.jianshu.com/p/9c10543420de
- (转)Pycharm在Ubuntu 16.04 系统中tensorflow导入报错
今天下午在配置pycharm时一直出现tensorflow不能导入的错误,如下图所示: 通过以下步骤可以解决问题: 步骤一: 1)在pycharm安装目录下,进入bin目录(一般情况下我们下载解压py ...
- PHP知识点
目录 1. PHP函数前面添加@的作用 2. PHP连接MySQL数据库字符集设置 1. 通过PDO扩展连接MySQL数据库 2. 通过mysql扩展连接 3. php查询数据库出现中文乱码 3. 参 ...
- Linux系统常用升级的基础包
Linux系统常用升级的基础包 yum -y install lrzsz gcc gcc-c++ make flex autoconf automake vixie-cron libjpeg libj ...
- java面试题 wait和sleep区别
sleep() 方法是线程类(Thread)的静态方法,让调用线程进入睡眠状态,让出执行机会给其他线程,等到休眠时间结束后,线程进入就绪状态和其他线程一起竞争cpu的执行时间 wait()是Objec ...
- windows server 2008 R2服务器无法通过ShellClass获取mp3音乐时长
我们先看一段代码,获取mp3播放时长: #region GetMediaDetailInfo 获取媒体文件属性信息 /// <summary> /// 获取媒体文件属性信息 /// < ...