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
题解:
题意自己翻译,此题首先肯定是要广搜的,不过要开一个1e5*1e5的数组好像有点困难,
所以用结构体来存每个点的下标,然后从源点开始广搜。定义一个pair<node,int>,第一个存节点信息,第二个存到当前节点的步数,因为还要处理到达每个节点的状态。
状态:走奇数步并且状态为1与走偶数步状态为0的结果是一样的,都是向左或向右移动。走偶数步并且状态为1与走奇数步状态为0的结果也是一样,可以向上或向下移动。
最后定义一个pair的队列。
代码:
#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <string.h>
#include <utility>
#define IO ios::sync_with_stdio(0);\
cin.tie();cout.tie();
using namespace std;
;
struct node
{
int i,j,value;
int flag;
} res[N];
pair <node,int> p;
queue <pair<node,int > > q;
int main()
{
int T;
cin >> T;
while(T--)
{
memset(res,,sizeof(res));
;
cin >> n >> m;
; i <= n; i++)
; j <= m; j++)
cin >> x,res[++num].value = x,res[num].i = i,res[num].j=j;
int x1,y1,x2,y2;
cin >>x1>>y1>>x2>>y2;
;
res[(x1-)*m + y1].flag = ;
p.first = res[(x1-)*m + y1];
p.second = sum;
q.push(p);
,ff = ;
while(!q.empty())
{
pair <node,int> pp;
pp = q.front();
q.pop();
int i = pp.first.i,j = pp.first.j,value = pp.first.value,sum = pp.second;
if(i==x2&&j==y2)
{
ans = sum;
ff = ;
break;
}
)&&value==)||(!(sum&)&&value==))
{
<=n&&res[i*m+j].flag==)
{
pp.first.i = i+,pp.first.j = j,pp.first.value = res[(i)*m+j].value;
pp.first.flag = ;
res[(i)*m+j].flag = ;
pp.second = sum+;
q.push(pp);
}
>&&res[(i-)*m+j].flag==)
{
pp.first.i = i-,pp.first.j = j,pp.first.value = res[(i-)*m+j].value;
pp.first.flag = ;
res[(i-)*m+j].flag = ;
pp.second = sum+;
q.push(pp);
}
}
else
{
<=m&&res[(i-)*m+j+].flag==)
{
pp.first.i = i,pp.first.j = j+,pp.first.value = res[(i-)*m+j+].value;
pp.first.flag = ;
res[(i-)*m+j+].flag = ;
pp.second = sum+;
q.push(pp);
}
>&&res[(i-)*m+j-].flag==)
{
pp.first.i = i,pp.first.j = j-,pp.first.value = res[(i-)*m+j-].value;
pp.first.flag = ;
res[(i-)*m+j-].flag = ;
pp.second = sum+;
q.push(pp);
}
}
}
printf();
while(!q.empty())q.pop();
}
;
}
/*
4
2 3
1 1 0
0 1 0
1 3 2 1
2 3
1 0 0
1 1 0
1 3 1 2
2 2
1 0
1 0
1 1 2 2
1 2
0 1
1 1 1 1
*/
zoj 4020 The 18th Zhejiang University Programming Contest Sponsored by TuSimple - G Traffic Light(广搜)的更多相关文章
- 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 ...
- 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 ...
- 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表示 ...
- The 18th Zhejiang University Programming Contest Sponsored by TuSimple -C Mergeable Stack
题目链接 题意: 题意简单,就是一个简单的数据结构,对栈的模拟操作,可用链表实现,也可以用C++的模板类来实现,但是要注意不能用cin cout,卡时间!!! 代码: #include <std ...
- 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 ...
- 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 19th Zhejiang University Programming Contest Sponsored by TuSimple (Mirror)
http://acm.zju.edu.cn/onlinejudge/showContestProblems.do?contestId=391 A Thanks, TuSimple! Time ...
- 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 ...
随机推荐
- bzoj 2038 莫队入门
http://www.lydsy.com/JudgeOnline/problem.php?id=2038 题意:多次询问区间内取出两个相同颜色的种类数 思路:由于不是在线更新,那么可以进行离线查询,而 ...
- js高阶函数--判断数据类型、函数胡柯里化;
一.判断数据类型: 常见的判断有typeof.instanceof. constructor. prototype,先来看typeof: var a = "hello world" ...
- ECMAScript5中新增的Array方法实例详解
ECMAScript5标准发布于2009年12月3日,它带来了一些新的,改善现有的Array数组操作的方法.(注意兼容性) 在ES5中,一共有9个Array方法:http://kangax.githu ...
- 天气预报service
https://weather.com/ https://api.weather.com/v2/turbo/vt1dailyForecast?apiKey=c1ea9f47f6a88b9acb43ab ...
- 【Dream Counting, 2006 Dec-数数的梦】数位dp
题意:给定两个数,问区间[A,B]中0~9分别出现了多少次.A,B<=10^18 题解:应该是最裸的数位dp吧..一开始没有记忆化tle了TAT 我们可以求出区间[0,B]的,再减去区间[0,A ...
- UOJ#179. 线性规划[模板]
传送门 http://uoj.ac/problem/179 震惊,博主竟然还不会线性规划! 单纯形实在学不会啊……背个板子当黑盒用…… 学(chao)了NanoApe dalao的板子 #includ ...
- 【LibreOJ】#539. 「LibreOJ NOIP Round #1」旅游路线
[题意]给定正边权有向图,车油量上限C,每个点可以花费pi加油至min(C,ci),走一条边油-1,T次询问s点出发带钱q,旅行路程至少为d的最多剩余钱数. n<=100,m<=1000, ...
- pinctrl框架【转】
转自:http://www.cnblogs.com/kevinhwang/p/5703192.html pinctrl框架是linux系统为统一各SOC厂家pin管理,目的是为了减少SOC厂家系统移植 ...
- python基础===创建大量对象是节省内存方法
问题: 你的程序要创建大量(可能上百万) 的对象,导致占用很大的内存. 解决方案: 对于主要是用来当成简单的数据结构的类而言,你可以通过给类添加__slots__属性来极大的减少实例所占的内存.比如: ...
- ARM linux的启动部分源代码简略分析【转】
转自:http://www.cnblogs.com/armlinux/archive/2011/11/07/2396784.html ARM linux的启动部分源代码简略分析 以友善之臂的mini2 ...