[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 ...
随机推荐
- 严重:one or more listeners failed. Full details will be found in the appropriate container log file
one or more listeners failed. Full details will be found in the appropriate container log file 这句话 ...
- 隐藏input光标和输入内容方法
text-indent: -999em; // 隐藏input文字margin-left: -100%;// 隐藏input光标
- CAS单点登录入门
一.单点登录简介 SOO是现在企业比较流行的业务整合解决方案之一,定义解决登录,可以应用在不同系统中,用户只需要登录一次,就可以访问所有相互信任的应用系统(模块开发.同家公司不同产品等等),例如百度, ...
- numpy(三)
广播: x= np.arange(12).reshape((3,4)) a= np.arange(3) b=np.arange(3)[;,np.newaxis] c=a+b a,b会扩散成公共的形状进 ...
- mysql中的concat,concat_ws(),group_concat()
mysql中的concat,concat_ws(),group_concat() 说明: 本文中使用的例子均在下面的数据库表tt2下执行: 一.concat()函数 1.功能:将多个字符串连接 ...
- #20175201 实验二:Java面向对象程序设计
20175201 实验二:Java面向对象程序设计 实验二 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L ...
- Unity3D 代码加密保护工具
加密方式 对于Unity3D的保护方式,主要是通过Virbox Protector Standalone对Unity3D程序的整个生成目录进行加密,可以保护Unity的主要代码逻辑不被反编译,最大 ...
- Git常用功能记录
1. git查看某个文件的修改历史 git log --pretty 然后使用下面的命令可列出文件的所有改动历史,注意,这里着眼于具体的一个文件,而不是git库,如果是库,那改动可多了去了- git ...
- MVC中ajax调用API版信息分页显示
效果图: API端采用三层架构控制器显示界面: UserBll bll = new UserBll(); //获取用户所有信息 [HttpGet] ,) { List<UserInfo> ...
- 关于delete请求,后台接收不到数据
在前端用axios需要这样写 /** * 删除数据 */export function del(url, data = {}) { return axios.delete(url, { data: q ...