BZOJ4290 传送门
昨天考试考了这道题,学校评测不开O2被卡的一愣一愣的。
这种题线性复杂度就线性复杂度,为什么要卡常数。
顺便提一句,GRH大爷O(m*n*ans)的算法有90分,我的O(m*n)算法75。(万恶的STL)
这是什么烂数据(只是吐槽我们学校的数据,与BZOJ无关)
那我们来讲一讲做法:(首先,这是一道SB题)
我们考虑传送的行为:
1.如果一个传送门被穿越,那么从此以后进的那个门一定不用回去,出的那个门可以在出去以后立即打一枪替换。所以从此这两个门都不用进了
2.如果我们从一个传送门出来,那么我们一定有一个时刻枪可以打到传送门所在位置。
3.我们考虑这个位置:打完这一枪以后,在哪里开传送门并不重要,都可以到达。那么我们就贪心地走到最近的一面墙就可以了(这一步等价于建立一条从这个点到所有枪可以到达的点建立了一条长度为t+1的边(t为当前点到最近的贴墙空地的距离)
下面是代码:
/**************************************************************
Problem: 4290
User: CYZ
Language: C++
Result: Accepted
Time:2444 ms
Memory:81668 kb
****************************************************************/ #include<cstdio>
#include<queue>
#include<list>
using namespace std ; const int MAXR = + ;
int C , R ;
char M [ MAXR ] [ MAXR ] ;
int dis1 [ MAXR ] [ MAXR ] ;
int dis2 [ MAXR ] [ MAXR ] ;
bool vis [ MAXR ] [ MAXR ] ;
int s1 [ MAXR ] [ MAXR ] ;
int s2 [ MAXR ] [ MAXR ] ;
int s3 [ MAXR ] [ MAXR ] ;
int s4 [ MAXR ] [ MAXR ] ; queue < pair < int , int > > q ;
queue < pair < int , int > , list < pair < int , int > > > Q [ * + ] ;
int cx , cy ;
int ans = - ; void P ( const int x , const int y ) {
if ( x < || x > C + || y < || y > R + || dis1 [ x ] [ y ] != ) return ;
dis1 [ x ] [ y ] = ;
q . push ( make_pair ( x , y ) ) ;
} template < class T1 , class T2 >
void min_equal ( T1 & a , const T2 & b ) {
if ( a > b ) a = b ;
} template < class T1 , class T2 >
void max_equal ( T1 & a , const T2 & b ) {
if ( a < b ) a = b ;
} int main () {
/*
freopen ( "portals.in" , "r" , stdin ) ;
freopen ( "portals.out" , "w" , stdout ) ;
*/
scanf ( "%d%d" , & R , & C ) ;
for ( int x = ; x <= R ; ++ x ) scanf ( "%s" , M [ x ] + ) ;
for ( int x = ; x <= R + ; ++ x ) M [ x ] [ ] = M [ x ] [ C + ] = '#' ;
for ( int y = ; y <= C + ; ++ y ) M [ ] [ y ] = M [ R + ] [ y ] = '#' ;
for ( int x = ; x <= R ; ++ x )
for ( int y = ; y <= C ; ++ y ) {
dis2 [ x ] [ y ] = ;
if ( M [ x ] [ y ] == 'S' ) {
Q [ ] . push ( make_pair ( x , y ) ) ;
} else if ( M [ x ] [ y ] == 'C' ) {
cx = x ;
cy = y ;
}
}
for ( int x = ; x <= R + ; ++ x )
for ( int y = ; y <= C + ; ++ y )
dis1 [ x ] [ y ] = M [ x ] [ y ] == '#' ;
for ( int x = ; x <= R + ; ++ x )
for ( int y = ; y <= C + ; ++ y )
if ( M [ x ] [ y ] == '#' ) {
P ( x + , y ) ;
P ( x - , y ) ;
P ( x , y - ) ;
P ( x , y + ) ;
}
while ( ! q . empty () ) {
const int x = q . front () . first ;
const int y = q . front () . second ;
q . pop () ;
if ( dis1 [ x + ] [ y ] == ) {
dis1 [ x + ] [ y ] = dis1 [ x ] [ y ] + ;
q . push ( make_pair ( x + , y ) ) ;
}
if ( dis1 [ x - ] [ y ] == ) {
dis1 [ x - ] [ y ] = dis1 [ x ] [ y ] + ;
q . push ( make_pair ( x - , y ) ) ;
}
if ( dis1 [ x ] [ y + ] == ) {
dis1 [ x ] [ y + ] = dis1 [ x ] [ y ] + ;
q . push ( make_pair ( x , y + ) ) ;
}
if ( dis1 [ x ] [ y - ] == ) {
dis1 [ x ] [ y - ] = dis1 [ x ] [ y ] + ;
q . push ( make_pair ( x , y - ) ) ;
}
}
for ( int x = ; x <= R ; ++ x )
for ( int y = ; y <= C ; ++ y ) {
dis2 [ x ] [ y ] = - ;
if ( s1 [ x ] [ y ] == )
for ( int x2 = x ; M [ x2 ] [ y ] != '#' ; ++ x2 )
s1 [ x2 ] [ y ] = x ;
if ( s3 [ x ] [ y ] == )
for ( int y2 = y ; M [ x ] [ y2 ] != '#' ; ++ y2 )
s3 [ x ] [ y2 ] = y ;
}
for ( int x = R ; x >= ; -- x )
for ( int y = C ; y >= ; -- y ) {
if ( s2 [ x ] [ y ] == )
for ( int x2 = x ; M [ x2 ] [ y ] != '#' ; -- x2 )
s2 [ x2 ] [ y ] = x ;
if ( s4 [ x ] [ y ] == )
for ( int y2 = y ; M [ x ] [ y2 ] != '#' ; -- y2 )
s4 [ x ] [ y2 ] = y ;
}
for ( int t = ; ans == - ; ++ t ) {
queue < pair < int , int > ,
list < pair < int , int > > > & q = Q [ t ] ;
while ( ! q . empty () ) {
const int x = q . front () . first ;
const int y = q . front () . second ;
q . pop () ;
if ( vis [ x ] [ y ] ) continue ;
vis [ x ] [ y ] = true ;
dis2 [ x ] [ y ] = t ;
if ( x == cx && y == cy ) {
ans = t ;
break ;
}
if ( M [ x + ] [ y ] != '#' && ! vis [ x + ] [ y ] )
Q [ t + ] . push ( make_pair ( x + , y ) ) ;
if ( M [ x - ] [ y ] != '#' && ! vis [ x - ] [ y ] )
Q [ t + ] . push ( make_pair ( x - , y ) ) ;
if ( M [ x ] [ y + ] != '#' && ! vis [ x ] [ y + ] )
Q [ t + ] . push ( make_pair ( x , y + ) ) ;
if ( M [ x ] [ y - ] != '#' && ! vis [ x ] [ y - ] )
Q [ t + ] . push ( make_pair ( x , y - ) ) ;
if ( ! vis [ s1 [ x ] [ y ] ] [ y ] ) Q [ t + dis1 [ x ] [ y ] ] . push
( make_pair ( s1 [ x ] [ y ] , y ) ) ;
if ( ! vis [ s2 [ x ] [ y ] ] [ y ] ) Q [ t + dis1 [ x ] [ y ] ] . push
( make_pair ( s2 [ x ] [ y ] , y ) ) ;
if ( ! vis [ x ] [ s3 [ x ] [ y ] ] ) Q [ t + dis1 [ x ] [ y ] ] .
push ( make_pair ( x , s3 [ x ] [ y ] ) ) ;
Q [ t + dis1 [ x ] [ y ] ] .
push ( make_pair ( x , s4 [ x ] [ y ] ) ) ;
}
}
/*
for ( int x = 1 ; x <= R ; ++ x ) {
for ( int y = 1 ; y <= C ; ++ y ) printf ( "%2d " , dis2 [ x ] [ y ] ) ;
putchar ( '\n' ) ;
}*/
printf ( "%d\n" , ans ) ;
return ;
}
BZOJ4290 传送门的更多相关文章
- 【hrbust2294】修建传送门
题意 哈理工2016级新生程序设计全国邀请赛B题 n个点1~n,i到i+1的距离为a[i],现在可以在两个点之间建一个传送门,则两点之间距离为0,求建传送门后1号出发的最远距离最小是多少? 题解 a[ ...
- [openwrt 项目开发笔记]: 传送门
“Openwrt 项目开发笔记”系列传送门: [Openwrt 项目开发笔记]:Openwrt平台搭建(一) (2014-07-11 00:11) [Openwrt 项目开发笔记]:Openwrt平台 ...
- codevs2059逃出克隆岛(传送门bfs)
/* 和普通的迷宫问题类似只是多了一个叫传送门的东西 对于传送门的处理: 每当跑到传送门就把其余所有传送门周围的点都入队 传送门之间不花费时间并且从不是传送门的点走到传送门 也不花费时间花费时间的(好 ...
- unity传送门类似效果实现
简述 在传送门中,核心的玩法是在地上或者墙上打开2个可以联通的洞来实现传送的效果.以此扩展加入解谜要素构成游戏的核心. 这里尝试使用unity来实现传送门的核心功能,具体功能分析如下: 1.传送门的模 ...
- Java编译过程(传送门)
我不是要做一门编程语言,了解这个对我现在的工作也没什么帮助,纯粹好奇而已. 传送门
- Test传送门(更新中)
一.Codeforces传送门: Avito Code Challenge 2018 题解传送门 Codeforces Round #485 (Div. 2) 题解传送门 二.hihocode ...
- 【bzoj题解】题解传送门
如题,题解传送门: 1001 1008 1012
- HDU 2102 A计划(两层地图加时间限制加传送门的bfs)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2102 A计划 Time Limit: 3000/1000 MS (Java/Others) Me ...
- JZOJ 5906. 传送门
Description 8102年,Normalgod在GLaDOS的帮助下,研制出了传送枪.但GLaDOS想把传送枪据为己有,于是把Normalgod扔进了一间实验室.这间实 ...
随机推荐
- 序列化serialize()与反序列化unserialize()的实例
在写序列化serialize与反序列化unserialize()时,我们先来看看: serialize - 产生一个可存储的值的表示 描述 string serialize ( mixed $valu ...
- MySql指令的执行顺序
1:From 2:On 3:Join 4:Where 5:Group by 5.1:函数 6:Having 7:Select 8:Distinct 9:Order by
- android发布帖子类技术
最近练习一些关于发布帖子的技术,说来也简单,就学了一点皮毛吧!好了,下面就上代码吧! 首先设计服务器的访问类,大家都知道现在东西都要联网的嘛! JSONParser的类: public class J ...
- count_char
import java.util.Scanner; public class count_char { public static void main(String args[]) { int cou ...
- SocketServer模块中的几种类
BaseServer:包括服务器的核心功能与混合类的一些功能. TCPServer:基本的网络同步TCP服务器. UDPServer:基本的网络同步UDP服务器. ForkingMixIn:实现了核心 ...
- BZOJ:2038: [2009国家集训队]小Z的袜子(hose)(莫队算法模板)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2038 解题心得: 第一次接触莫队算法,很神奇,很巧妙.莫队算法主要就是用来解决多次询问时 ...
- 在WPF中自定义控件(3) CustomControl (上)
原文:在WPF中自定义控件(3) CustomControl (上) 在WPF中自定义控件(3) CustomControl (上) 周银辉 ...
- 初步学习pg_control文件之五
接前文 初步学习pg_control文件之四,继续看何时出现 DB_IN_CRASH_RECOVERY: 看下面代码就比较清楚了:如果对 InArchiveRecovery 判断值为假,而且 读取出 ...
- 配置ORACLE的PRO*C环境
1.访问数据库的方法 在ORACLE数据库管理和系统中,有三种访问数据库的方法: ⑴.用SQL*Plus, 它有SQL命令以交互的应用程序访问数据库: ⑵.用第四代语言应用开发工具开 ...
- Android PopupWindow 疑难杂症之宽度WRAP_CONTENT
一直以来都觉得 Android 中的 PopupWindow 不好用.主要有以下两点:1.宽度不好控制2.位置不好控制 今天单说第1点. 由于应用有好几种国家的语言,加上各设备宣染效果不完全一样,对p ...