http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5738

题意 给你一个map 每个格子里有一个红绿灯,用0,1表示状态。当所在格子为0时只能上下移动,为1时左右移动。人一秒动一次,并且每一秒必须移动,灯每秒改变依次状态。问从起点到终点最短时间。

题解:   就看成一道墙壁会按时间周期改变的走迷宫。

    只是墙壁的作用是限制走的方向而不是不能通过。

    关于如何判定迷宫无法走通,按套路设了一个vis数组,试了一发就ac了,莫名奇妙。

    关于处理状态随时间改变,在node里加一个时间参数,然后对它mod2。

我用的BFS代码:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<cstring>
#include<set>
#include<algorithm>
#include<stack>
#include<string>
#include<cstdio>
#include<list>
#include<cstdlib>
#include<queue>
#define _for(i, a, b) for (int i = (a); i<(b); ++i)
using namespace std;
const int N = 1e5 + ;
const int INF = 1e6; int t, n, m,x;
vector<int> map[N], vis[N];
int dir[][] = { , ,-,, ,, ,- };
struct node {
int x, y, t;
node(int x, int y, int t) :x(x), y(y), t(t) {}
};
int main()
{ cin >> t;
while (t--) { scanf("%d%d", &n, &m);
_for(i, , n) map[i].clear(), vis[i].clear();
_for(i, , n)_for(j, , m) { scanf("%d", &x); map[i].push_back(x); vis[i].push_back(); }
int sr, sc, fr, fc;
scanf("%d%d%d%d", &sr, &sc, &fr, &fc);
fr--, fc--,sr--,sc--;
//_for(i, 0, n)_for(j, 0, m)cout << map[i][j];
queue<node>Q;
Q.push(node(sr, sc,));
vis[sr][sc] = ;
int ok = ;
while (!Q.empty()) {
if (ok) break;
node now = Q.front();
Q.pop();
if (now.x == fr&&now.y == fc) { ok = ; cout << << endl; break; }
int state = (now.t+map[now.x][now.y]) % ;
if (state) {
_for(i, , ) {
int dx = now.x;
int dy;
dy = i ? now.y + : now.y - ;
if (dx < || dx >= n || dy < || dy >= m||vis[ dx][dy])continue;
if (dx == fr&&dy == fc) {
cout << now.t + << endl; ok = ; break;
}
Q.push(node(dx, dy, now.t + )); vis[dx][dy] = ;
}
}
else {
_for(i, ,) {
int dx;
dx = i ? now.x + : now.x - ;
int dy = now.y ;
if (dx < || dx >= n || dy < || dy >= m||vis[dx][dy])continue;
if (dx == fr&&dy == fc) {
cout << now.t + << endl; ok = ; break;
}
Q.push(node(dx, dy, now.t + )); vis[dx][dy] = ;
}
} }
if (!ok)cout << - << endl;
}
//system("pause");
return ;
}

152 - - G Traffic Light 搜索(The 18th Zhejiang University Programming Contest Sponsored by TuSimple )的更多相关文章

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

  2. The 18th Zhejiang University Programming Contest Sponsored by TuSimple

    Pretty Matrix Time Limit: 1 Second      Memory Limit: 65536 KB DreamGrid's birthday is coming. As hi ...

  3. Mergeable Stack 直接list内置函数。(152 - The 18th Zhejiang University Programming Contest Sponsored by TuSimple)

    题意:模拟栈,正常pop,push,多一个merge A B 形象地说就是就是将栈B堆到栈A上. 题解:直接用list 的pop_back,push_back,splice 模拟, 坑:用splice ...

  4. The 18th Zhejiang University Programming Contest Sponsored by TuSimple -C Mergeable Stack

    题目链接 题意: 题意简单,就是一个简单的数据结构,对栈的模拟操作,可用链表实现,也可以用C++的模板类来实现,但是要注意不能用cin cout,卡时间!!! 代码: #include <std ...

  5. ZOJ 4016 Mergeable Stack(from The 18th Zhejiang University Programming Contest Sponsored by TuSimple)

    模拟题,用链表来进行模拟 # include <stdio.h> # include <stdlib.h> typedef struct node { int num; str ...

  6. ZOJ 4019 Schrödinger's Knapsack (from The 18th Zhejiang University Programming Contest Sponsored by TuSimple)

    题意: 第一类物品的价值为k1,第二类物品价值为k2,背包的体积是 c ,第一类物品有n 个,每个体积为S11,S12,S13,S14.....S1n ; 第二类物品有 m 个,每个体积为 S21,S ...

  7. The 19th Zhejiang University Programming Contest Sponsored by TuSimple (Mirror)

    http://acm.zju.edu.cn/onlinejudge/showContestProblems.do?contestId=391 A     Thanks, TuSimple! Time ...

  8. The 19th Zhejiang University Programming Contest Sponsored by TuSimple (Mirror) B"Even Number Theory"(找规律???)

    传送门 题意: 给出了三个新定义: E-prime : ∀ num ∈ E,不存在两个偶数a,b,使得 num=a*b;(简言之,num的一对因子不能全为偶数) E-prime factorizati ...

  9. The 17th Zhejiang University Programming Contest Sponsored by TuSimple J

    Knuth-Morris-Pratt Algorithm Time Limit: 1 Second      Memory Limit: 65536 KB In computer science, t ...

随机推荐

  1. Dubbo -- 系统学习 笔记 -- 示例 -- 直连提供者

    Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 直连提供者 在开发及测试环境下,经常需要绕过注册中心,只测试指定服务提供者,这时候 ...

  2. NetBpm 组织架构(4)

    大牛的杰作,赞一个 转自:NetBPM工作流的架构设计及实现浅析 读前的话:由于本文涉及内容颇多,若有地方读来不很明白,建议先跳过,整体上有个认识后,再回过头来理解.作者认识有限,若有错误,欢迎斧正: ...

  3. zabbix加入TCP连接数及状态的监控

    一 监控原理: [root@ nginx]# /bin/netstat -an|awk '/^tcp/{++S[$NF]}END{for(a in S) print a,S[a]}' TIME_WAI ...

  4. 流程控制与数组——Java疯狂讲义

    顺序结构 if分支语句      if{} 可以有多个else if{} else{} 可以省略 switch分支语句 while循环 do while循环 for循环 嵌套循环 控制循环结构 理解数 ...

  5. Swift - static和class的使用

    Swift中表示 “类型范围作用域” 这一概念有两个不同的关键字,它们分别是static和class.这两个关键字确实都表达了这个意思,但是在其他一些语言,包括Objective-C中,我们并不会特别 ...

  6. Objective-C官方文档 值和集合

    版权声明:原创作品,谢绝转载!否则将追究法律责任. 尽管Objective-c是一个面向对象的语言,是C语言的超集,这意味着你可以用任何标准的C标量(非对象)像int,float,和char在Obje ...

  7. WP8.1学习系列(第二十四章)——Json解析

    .net已经集成了json解析,类名叫DataContractJsonSerializer DataContractJsonSerializer 类型公开以下成员. 构造函数     名称 说明 Da ...

  8. 使用IBM SVC构建vSphere存储间集群

    使用IBM SVC构建vSphere存储间集群 本文目的 本文描述利用IBM SVC来构建Vsphere 存储间集群 解决方案 什么是vMSC? vShpere存储间集群是一个针对VmwarevSpe ...

  9. 使用 CSS MARK 改变 SVG 背景色

    CSS masks -webkit-mask 这个属性是相当强大的,详细的介绍请到这里查看,它非常值得深入研究. -webkit-mask 让为一个元素添加蒙板成为可能,从而你可以创建任意形状的花样. ...

  10. jQuery的回调管理机制(二)

    jQuery.extend({ /*  * deferred对象的一大好处,就是它允许你自由添加多个回调函数. * $.ajax("test.html")   .done(func ...