题目链接: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的更多相关文章

  1. ZOJ - 4020 Traffic Light 【BFS】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4020 题意 给出一张地图 以及起点和终点 求是否能从起点走到终点 ...

  2. ZOJ - 4020 Traffic Light (BFS)

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

  3. 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 题解: 题意 ...

  4. Gym 101775C - Traffic Light - [思维题]

    题目链接:http://codeforces.com/gym/101775/problem/C 题意: 给出 $N$ 个红绿灯,又给出 $N+1$ 个距离 $S_i = S_0,S_1, \cdots ...

  5. ZOJ2018/4月月赛G题Traffic Light(广搜)

    题意:首先T组数据,每组数据包括:第一行:一个n,m,然后下面有一个n行m列的01矩阵. 最后一行输入四个数字,分别是起点的横纵坐标,终点的横纵坐标.询问从起点到终点,最少要几步,如果到不了输出-1 ...

  6. 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表示 ...

  7. zoj4020 Traffic Light(bfs+状态压缩)

    题意:每个点有两种状态,0/1,0表示只能上下方向走,1表示只能左右方向走.每走一步整个图的状态改变一次(即0->1,1->0). 数据范围:n,m<=1e15 开始迷之因为数组太大 ...

  8. 快速切题 sgu103. Traffic Lights 最短路 难度:1

    103. Traffic Lights Time limit per test: 0.25 second(s)Memory limit: 4096 kilobytes input: standardo ...

  9. 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 ...

随机推荐

  1. springcloud学习第一坑

    我是根据周立先生所写的<Spring+Cloud与Docker微服务架构实战>来学习SpringCloud的,我会记录下来我日常遇到的所有问题,包括但不仅只包括SpringCloud的问题 ...

  2. Log print(Android)

        Log.v("MyTag", "This is Verbose.");    Log.d("MyTag", "This i ...

  3. table-cell width:1% 深入理解

    问题描述 今天在使用Bootstrap给页面添加底部导航栏时,需要在手机下也使导航栏呈现水平排列的效果.最后在网上查找解决方法是,看到这样一个解决方法: .nav-justified > li ...

  4. canal mysql slave

    [mysqld] log-bin=mysql-bin #添加这一行就ok binlog-format=ROW #选择row模式 server_id=1 #配置mysql replaction需要定义, ...

  5. go 语言环境搭建(Ubuntu + Sublime Text3 + GoSublime + gocode)

    1. 首先安装go http://code.google.com/p/golang-china/wiki/Install 或者http://golang.org/doc/install这里有详细介绍. ...

  6. C++ 赋值构造函数的返回值到底有什么用?且返回值是否为引用类型有什么区别吗?

    首先定义类Person class Person{ public: string name; Person()=default; //默认构造函数 Person(string nam):name(na ...

  7. 18.12.09-C语言练习:兔子繁衍问题 / Fibonacci 数列

    题目: 问题解析: 这是典型的/Fibonacci 数列问题.具体这里不赘述. 问题中不论是初始的第1对兔子还是以后出生的小兔子都是从第3个月龄起每个月各生一对兔子. 设n1,n2,n3分别是每个月1 ...

  8. HVP plan

    HVP,hier verification plan,建立整个验证的plan,在验证后期,通过vcs的coverage db可以直接进行反标, 包括反标code coverage,function c ...

  9. CentOS7 源码编译安装Tengine

    简介 Tengine是由淘宝网发起的Web服务器项目.它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性.它的目的是打造一个高效.安全的Web平台. 发展 Tengine的性能和 ...

  10. day21 python之模块和包

    一 模块 1 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编 ...