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 ...
随机推荐
- VC++6.0相对路径无效的解决办法
我们在开发程序时,常常需要操作相关的文件.操作文件一般有两种方法:绝对路径和相对路径.绝对路径是从盘符开始的,相对路径则是相对于当前目录. 绝对路径很简单,一般也不会出错,但是在实际开发过程中要慎用绝 ...
- 浅谈PostgreSQL的索引
1. 索引的特性 1.1 加快条件的检索的特性 当表数据量越来越大时查询速度会下降,在表的条件字段上使用索引,快速定位到可能满足条件的记录,不需要遍历所有记录. create table t(id i ...
- IIS URL Rewrite – Installation and Use
IIS URL Rewrite – Installation and Use Posted by Nick LeFevre | Leave a reply IIS URL Rewrite Instal ...
- Kubernetes 相关概念
Node: (1) Node(节点)也就是宿主机,宿主机可以是物理机 .云主机 .虚拟机等等,我们可以在一个宿主机上跑多个容器(container)(2) Node 上会被分配一些工作负载(即 doc ...
- 如何使用 SSH 连接 VMWare 虚拟机中的 Ubuntu
环境:VMWare Player 5.0.2,Ubuntu 13.10 VMWare共有3种网络连接模式,分别是: 1. bridged(桥接模式):虚拟机将直接连接到物理局域网,使自身独立于宿主机 ...
- 【sql进阶】查询每天、每个设备的第一条数据
需求如下 每个设备(不同DeviceID).每天会向数据库插入多条数据,求每天.每个设备插入的第一条数据. 下面SQL中的 ShareRecommendID 类比不同设备的DeviceID. ROW_ ...
- 【大数据系列】hadoop上传文件报错_COPYING_ could only be replicated to 0 nodes
使用hadoop上传文件 hdfs dfs -put XXX 17/12/08 17:00:39 WARN hdfs.DFSClient: DataStreamer Exception org.ap ...
- React组件设计(转)
React组件设计 组件分类 展示组件和容器组件 展示组件 容器组件 关注事物的展示 关注事物如何工作 可能包含展示和容器组件,并且一般会有DOM标签和css样式 可能包含展示和容器组件,并且不会有D ...
- smarty assign变量赋值
1.变量赋值的两种写法 <%assign var="name" value="cl"%> <%assign "name" ...
- SharpGL学习笔记(三) 投影变换和视点变换
从本节开始,我们使用SharpGL带的VS2010扩展,来直接生成SharpGL工程. 如果你新建项目时,没有看到下面的SharpGL项目,那么请事先在SharpGL源代码中找到一个叫 ”SharpG ...