[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 ...
随机推荐
- btcpool之总架构
一.架构图 二.模块划分 整个btcpool分成GbtMaker.BlockMaker.JobMaker.StratumServer.PoolWatcher.statshttpd.sharelogge ...
- .Net Core:Middleware中间件管道
.NetCore中的Middleware是装配到管道处理请求和响应的组件:每个组件都可以决定是否继续进入下一个管道.并且可以在进入下一个管道前后执行逻辑: 最后一个管道或者中断管道的中间件叫终端中间件 ...
- HTMLTESTRunner自动化测试报告增加截图功能
我们都知道HTMLTESTRunner自动化测试报告,是Unittest单元测试框架报告,那么在做ui测试的时候就有点不适用了. 我们需要出错截图功能. 以下是我改的,增加了截图功能,先展示界面,再展 ...
- python虚拟环境迁移
python虚拟环境迁移: 注意事项:直接将虚拟环境复制到另一台机器,直接执行是会有问题的. 那么可以采用以下办法: 思路:将机器1虚拟环境下的包信息打包,之后到机器2上进行安装: (有两种情况要考虑 ...
- 《linux就该这么学》第十二节课:第10章,Apache网站服务
第十章 10.1.网站服务程序 (让用户能够通过网站访问服务器上的资源) 目前提供的网站服务有IIS,Nginx,Apache等,IIS是windows中默认的web服务程序. Nginx是后起之秀, ...
- Elasticsearch.安装(单节点)
Elasticsearch.安装(单节点) 环境Linux 7.x jdk 1.8 elasticsearch 5.x 环境目录结构(根目录多了两个文件夹): /resources /** 存放 ...
- React组件绑定this的三种方法
我们在使用React组件时,调用方法常常用到this和event对象,默认情况是不会绑定到组件上的,需要特殊处理. 节点上使用bind绑定 特点:该方法会在每次渲染组件时都会重新绑定一次,消耗一定的性 ...
- zabbix监控实战<3> 之自定义监控实例
第一章 自定义监控tcp状态 命令可以选择ss 或者 netstat ss打印基于socket的统计信息,实际运行下来,ss的速度要比netstat要快得多 1.1 tcp的十一种状态 ...
- Java -- 基于JDK1.8的LinkedList源码分析
1,上周末我们一起分析了ArrayList的源码并进行了一些总结,因为最近在看Collection这一块的东西,下面的图也是大致的总结了Collection里面重要的接口和类,如果没有意外的话后面基本 ...
- SV-assertion
断言(assert)是一种描述性语言,通过描述的期望结果来进行仿真验证. 断言有一个更加基础的信息,我们称为属性(property),属性可以作为断言结果,功能覆盖点,形式检查和约束随机激励生成. 断 ...