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. Nexus5 破解电信关键步骤

    5儿子终于摔坏了,送去保养之后,发现之前已破解的电信3G竟然无效了,心碎!!!!!!!!!!!!!!!!!! 尝试恢复efs --还好有备份,备份万岁!!! 不行!继续尝试恢复!还是不行!再试!... ...

  2. http 返回码 405 解决方案之一

    今天做网络请求数据的时候遇到返回码405,当时就傻了~~ 故事是这样的-- 我用post请求访问一个url,服务端数据是一个json的txt文件,理论上直接访问,返回json,然后解析就没事了,可是今 ...

  3. 浅谈iPhone OS(iOS)架构

    iPhone OS(现在叫iOS)是iPhone, iPod touch 和 iPad 设备的操作系统

  4. C 修改命令行文本颜色

    #include <Windows.h> #include <stdio.h> int main() { HANDLE h = GetStdHandle(STD_OUTPUT_ ...

  5. PHP代码审计笔记--命令执行漏洞

    命令执行漏洞,用户通过浏览器在远程服务器上执行任意系统命令,严格意义上,与代码执行漏洞还是有一定的区别. 0x01漏洞实例 例1: <?php $target=$_REQUEST['ip']; ...

  6. iOS开发-NSDictionary

    判断一个字典中是否存在某个key,有两种方法: 方法一: if ([dictionary allKeys] containsObject: key]){ // cotains key operatio ...

  7. Git 学习笔记--git 查看某个文件的修改历史

    1. git log -p filename 查看文件的每一个详细的历史修改,如果没有-p选项,只显示提交记录,不显示文件内容修改,git log -p -3 filename 显示最近的3次提交. ...

  8. Linux CentOS6.5上搭建环境遇到的问题

    1.卸载CentOS自带的JDK 查看centos上 安装的jdk:rpm -qa|grep jdk 出现如下: java-1.7.0-openjdk-1.7.0.45-2.4.3.3.el6.x86 ...

  9. Ansible Playbook handlers 语句

    handlers 用法如下,表示当 tasks 执行成功之后再执行 handlers,相当于 shell 中的 && 用法,如果 tasks 执行失败是不会执行 handlers 语句 ...

  10. 【Mac】WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!

    使用Mac 自带终端 链接服务器时候,报错如下 处理办法: 第一种:  直接删除:  /users/username/.ssh/known_hosts  文件 第二种: ssh-keygen -R   ...