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 ...
随机推荐
- Nginx安装(官网翻译)
转载自:https://www.nginx.com/resources/wiki/start/topics/tutorials/install/ 二进制版本预包装的Linux和BSD大多数Linux发 ...
- 深入探析 Rational AppScan Standard Edition 多步骤操作
序言 IBM Rational AppScan Standard(下文简称 AppScan)作为面向 Web 应用安全黑盒检测的自动化工具,得到业界的广泛认可和应用.很多人使用 AppScan 时都采 ...
- oracle to_char格式数值
C:\Users\XXX>sqlplus / as sysdba SQL :: Copyright (c) , , Oracle. All Rights Reserved. 连接到: Oracl ...
- web.xml中出现<servlet-name>default</servlet-name>是什么意思?
转载自:http://blog.csdn.net/hello5orld/article/details/9407905 在web.xml文件中经常看到这样的配置<servlet-name> ...
- JS设计模式之装饰者模式
装饰者模式概述 在不改变原对象的基础上,通过对其进行包装拓展(添加属性或者方法)使原有对象可以满足用户更复杂的需求 实际需求 在已有的代码基础上,为每个表单中的input默认输入框上边显示一行提示文案 ...
- [LeetCode] 12. Integer to Roman ☆☆
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- 【CC2530入门教程-01】CC2530微控制器开发入门基础
[引言] 本系列教程就有关CC2530单片机应用入门基础的实训案例进行分析,主要包括以下6部分的内容:[1]CC2530微控制器开发入门基础.[2]通用I/O端口的输入和输出.[3]外部中断初步应用. ...
- 4、什么是事务?MySQL如何支持事务?
什么是事务? 事务是由一步或几步数据库操作序列组成逻辑执行单元,这系列操作要么全部执行,要么全部放弃执行.程序和事务是两个不同的概念.一般而言:一段程序中可能包含多个事务.(说白了就是几步的数据库操作 ...
- node启动服务
npm install http-server -g http-server ipconfig查看当前ip 手机可访问第一个网址.
- java===java基础学习(2)---运算符,三元操作符,数学函数
主要介绍运算符,和数学函数以及三元运算符: package testbotoo; public class test1 { public static void main(String[] args) ...