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. Java使用泛型实现栈结构

    泛型是Java SE5.0的重要特性,使用泛型编程可以使代码获得最大的重用.由于在使用泛型时要指明泛型的具体类型,这样就避免了类型转换.本实例将使用泛型来实现一个栈结构,并对其进行测试. 思路分析:既 ...

  2. 随笔 -- NIO -- 相关 -- 系统概述

    .打开Selector .打开ServerSocketChannel .获取与此Channel关联的ServerSocket并绑定地址 .设置Channel为非阻塞 .将Channel注册到Selec ...

  3. Kubernetes kubectl 命令

    kubectl 命令用来操作 Kubernetes 集群中的资源对象,包括对资源的创建.删除.查看.修改.配置.运行等 命令语法:kubectl [command] [TYPE] [NAME] [fl ...

  4. MHL相关资源链接

    http://www.mhlconsortium.org/ 消费者网站: www.meetmhl.com采用者网站:www.mhltech.org 博客http://blog.sina.com.cn/ ...

  5. linux C 调用shell程序执行

    #include<stdio.h> #include <unistd.h> #include <sys/types.h> #include <stdlib.h ...

  6. 《Lua程序设计》第3章 表达式 学习笔记

    3.1 算术操作符“+”(加法).“-”(减法).“*”(乘法).“/”(除法).“^”(指数).“%”(取模).3.2 关系运算符< > <= >= == ~=3.3 逻辑操 ...

  7. Matlab 矩阵函数

    clear; clc; A = rand() cond(A) %求矩阵A的条件数 Det(A) %求方阵A的行列式 Dot(A,B) %矩阵A与B的点积 Eig(A) %方阵A的特征值和特征向量 No ...

  8. free -m 下的含义

    如下显示free是显示的当前内存的使用,-m的意思是M字节来显示内容.我们来一起看看. $ free -mtotal used free shared buffers cachedMem: 1002 ...

  9. thinkphp5.0 输入变量

    可以通过Request对象完成全局输入变量的检测.获取和安全过滤,支持包括$_GET.$_POST.$_REQUEST.$_SERVER.$_SESSION.$_COOKIE.$_ENV等系统变量,以 ...

  10. Sqlserver Sequence操作

    USE [database_test] GO --创建SEQUENCE CREATE SEQUENCE defaultSequence AS INT --设置开始行 START --自增量 INCRE ...