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表示状态。当所在格子为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 )的更多相关文章
- 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 题解: 题意 ...
- 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 ...
- 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 ...
- The 18th Zhejiang University Programming Contest Sponsored by TuSimple -C Mergeable Stack
题目链接 题意: 题意简单,就是一个简单的数据结构,对栈的模拟操作,可用链表实现,也可以用C++的模板类来实现,但是要注意不能用cin cout,卡时间!!! 代码: #include <std ...
- 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 ...
- 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 ...
- The 19th Zhejiang University Programming Contest Sponsored by TuSimple (Mirror)
http://acm.zju.edu.cn/onlinejudge/showContestProblems.do?contestId=391 A Thanks, TuSimple! Time ...
- 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 ...
- 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 ...
随机推荐
- Python 扩展知识
Python 练习题 Python 编程习惯 Python 转义字符 Python 格式化输出 Python 列表表达式 Python 生成器表达式 Python 序列化 Python2 与 Pyth ...
- LVS+NGINX+TOMCAT_集群实施操作记录.docx
LVS IP: Eth0:192.168.100.115 Eth1:192.168.100.215 Vi /etc/init.d./lvs #!/bin/sh # # lvs Start ...
- 深入浅出MFC——MFC骨干程序(四)
1. 熟记MFC类层次结构: 2. AppWizard可以为我们制作出MFC程序骨干: 3. Document/View支撑你的应用程序:Document/View的价值在于,这些MFC类已经把一个应 ...
- 在 Core Data 中存取 transformable 类型的数据
本文转载至 http://imenjoe.com/2015/04/10/CoreData-transformable-20150410/ 在开发过程中有一个需要在 Core Data 中存取 NSDi ...
- Java读取maven目录下的*.properties配置文件
public class ReadProperties{ private static String proFileName = "/config/MQSubjectId.propertie ...
- IDEA试用期结束激活问题
1.试用期结束,出现IDEA License Activation界面 2.IntelliJ Idea 2017 免费激活方法 方法1. 到网站 http://idea.lanyus.com/ 获取注 ...
- IOS设计模式第四篇之装饰设计模式的类别设计模式
装饰设计模式 装饰设计模式动态的添加行为和责任向一个对象而不修改他的任何代码.他是你子类化修改类的行为用通过另一个对象的包装的代替方法. 在Objective-c里面有很多这种设计模式的实现,像cat ...
- 异常:Servlet class X is not a javax.servlet.Servlet
使用Maven命令 mvn archetype:create 创建了一个简单的web项目.导入Eclipse运行时,报这样的异常信息: Servlet class X is not a javax.s ...
- create-react-app项目中的eslint
"no-multi-spaces": 1, //禁止多个空格 "jsx-quotes": 1, //此规则强制在JSX属性中一致使用双引号或单引号 " ...
- css零零散散的笔记
1.div根据内容自适应大小 效果图: html: <body> <div class="parent"> <div class="chil ...