LOJ#2632. 「BalticOI 2011 Day1」打开灯泡 Switch the Lamp On
题目描述
译自 BalticOI 2011 Day1 T3「Switch the Lamp On」
有一种正方形的电路元件,在它的两组相对顶点中,有一组会用导线连接起来,另一组则不会。
有 N×M 个这样的元件,你想将其排列成 N 行 M 列放在电路板上。电路板的左上角连接电源,右下角连接灯泡。
试求:至少要旋转多少个正方形元件才能让电源与灯泡连通,若无解则输出 NO SOLUTION。
Casper is designing an electronic circuit on a N×M rectangular grid plate. There are N×M square tiles that are aligned to the grid on the plate. Two (out of four) opposite corners of each tile are connected by a wire.
A power source is connected to the top left corner of the plate. A lamp is connected to the bottom right corner of the plate. The lamp is on only if there is a path of wires connecting power source to lamp. In order to switch the lamp on, any number of tiles can be turned by 90° (in both directions).
In the picture above the lamp is off. If any one of the tiles in the second column from the right is turned by 90° , power source and lamp get connected, and the lamp is on.
Write a program to find out the minimal number of tiles that have to be turned by 90° to switch the lamp on.
输入格式
第一行有两个整数 NN和 M。
在接下来的 N 行中,每行有 M 个字符。每个字符均为 \ 或 /,表示正方形元件上导线的连接方向。
The first line of input contains two integer numbers NNN and MMM, the dimensions of the plate. In each of the following NNN lines there are MMM symbols – either \ or / – which indicate the direction of the wire connecting the opposite vertices of the corresponding tile.
输出格式
输出共一行,若有解则输出一个整数,表示至少要旋转多少个正方形元件才能让电源与灯泡连通;若无解则输出 NO SOLUTION。
There must be exactly one line of output. If it is possible to switch the lamp on, this line must contain only one integer number: the minimal number of tiles that have to be turned to switch on the lamp. If it is not possible, output the string: NO SOLUTION
样例
样例输入
3 5
\\/\\
\\///
/\\\\
样例输出
1
数据范围与提示
对于 40% 的数据,1≤N≤4,1≤M≤5。
对于所有数据,1≤N,M≤500。
题解
唔...这题$spfa+SLF$跑的飞快
因为跑bfs要判的东西貌似很多的样子所以我直接连边跑spfa了
对于能够直接走的那就连一条0边,不能直接走就连1边
然后注意坐标位置的取值...
因为边权只有0和1所以SLF在这里很有用
最后就是空间要乘个8倍
#include <bits/stdc++.h> #define ll long long
#define inf 0x3f3f3f3f
#define il inline #define in(a) a=read()
#define out(a) printf( "%d" , a )
#define outn(a) out(a),putchar('\n') #define I_int int
inline I_int read() { I_int x = , f = ; char c = getchar() ;
while( c < '' || c > '' ) {
if( c == '-' ) f = - ;
c = getchar() ;
}
while( c >= '' && c <= '' ) {
x = (x << ) + (x << ) + c - ;
c = getchar() ;
}
return x * f ;
}
#undef I_int using namespace std ; const int N = ;
const int M = (*+)* ;
const int dx[] = {,,,-};
const int dy[] = {-,,,}; char a[ N ][ N ] ;
int n , m ;
int head[ M ] , d[ M ] , cnt , vis[ M ] ;
struct node {
int to , nxt , v ;
} e[ M << ] ; void ins( int u , int v , int w ) {
e[ ++ cnt ].to = v ;
e[ cnt ].nxt = head[ u ] ;
e[ cnt ].v = w ;
head[ u ] = cnt ;
} bool check( int x , int y ) {
if( x < || x > n || y < || y > m ) return ;
return ;
} int zb( int x , int y ) {
return (x-)*(m+)+y ;
} deque<int>q; void spfa() {
vis[ ] = ;
for( int i = ; i <= (n+)*(m+) ; i ++ ) d[ i ] = inf ;
d[ ] = ;
q.push_front();
while( !q.empty() ) {
int u = q.front() ; q.pop_front() ;
vis[ u ] = ;
for( int i = head[ u ] ; i ; i = e[ i ].nxt ) {
int v = e[ i ].to ;
if( d[ v ] > d[ u ] + e[ i ].v ) {
d[ v ] = d[ u ] + e[ i ].v ;
if( !vis[ v ] ) {
vis[ v ] = ;
if(!e[i].v) q.push_front(v);
else q.push_back(v) ;
}
}
}
}
if( d[zb(n,m)] == inf ) puts("NO SOLUTION") ;
else outn( d[ zb(n+,m+) ] ) ;
} int main() {
in( n ) ; in( m ) ;
for( int i = ; i <= n ; i ++ ) {
scanf( "%s" , a[ i ] + ) ;
for( int j = ; j <= m ; j ++ ) {
if( a[ i ][ j ] == '\\' ) {
ins( zb(i,j+) , zb(i+,j) , ) , ins( zb(i+,j) , zb(i,j+) , ) ;
ins( zb(i,j) , zb(i+,j+) , ) , ins( zb(i+,j+) , zb(i,j) , ) ;
}
if( a[ i ][ j ] == '/' ) {
ins( zb(i,j+) , zb(i+,j) , ) , ins( zb(i+,j) , zb(i,j+) , ) ;
ins( zb(i,j) , zb(i+,j+) , ) , ins( zb(i+,j+) , zb(i,j) , ) ;
}
}
}
spfa() ;
}
LOJ#2632. 「BalticOI 2011 Day1」打开灯泡 Switch the Lamp On的更多相关文章
- 【LOJ】#3032. 「JOISC 2019 Day1」馕
LOJ#3032. 「JOISC 2019 Day1」馕 处理出每个人把馕切成N段,每一段快乐度相同,我们选择第一个排在最前的人分给他的第一段,然后再在未选取的的人中选一个第二个排在最前的切一下,并把 ...
- 【LOJ】#3031. 「JOISC 2019 Day1」聚会
LOJ#3031. 「JOISC 2019 Day1」聚会 听说随机可过? 我想了很久想了一个不会被卡的做法,建出前\(u - 1\)个点的虚树,然后找第\(u\)个点的插入位置,就是每次找一条最长链 ...
- 【LOJ】#3030. 「JOISC 2019 Day1」考试
LOJ#3030. 「JOISC 2019 Day1」考试 看起来求一个奇怪图形(两条和坐标轴平行的线被切掉了一个角)内包括的点个数 too naive! 首先熟练的转化求不被这个图形包含的个数 -- ...
- [LOJ#2327]「清华集训 2017」福若格斯
[LOJ#2327]「清华集训 2017」福若格斯 试题描述 小d是4xx9小游戏高手. 有一天,小d发现了一个很经典的小游戏:跳青蛙. 游戏在一个 \(5\) 个格子的棋盘上进行.在游戏的一开始,最 ...
- Loj #2331. 「清华集训 2017」某位歌姬的故事
Loj #2331. 「清华集训 2017」某位歌姬的故事 IA 是一名会唱歌的女孩子. IOI2018 就要来了,IA 决定给参赛选手们写一首歌,以表达美好的祝愿.这首歌一共有 \(n\) 个音符, ...
- Loj #2324. 「清华集训 2017」小 Y 和二叉树
Loj #2324. 「清华集训 2017」小 Y 和二叉树 小Y是一个心灵手巧的OIer,她有许多二叉树模型. 小Y的二叉树模型中,每个结点都具有一个编号,小Y把她最喜欢的一个二叉树模型挂在了墙上, ...
- Loj #2321. 「清华集训 2017」无限之环
Loj #2321. 「清华集训 2017」无限之环 曾经有一款流行的游戏,叫做 *Infinity Loop***,先来简单的介绍一下这个游戏: 游戏在一个 \(n \times m\) 的网格状棋 ...
- Loj 2320.「清华集训 2017」生成树计数
Loj 2320.「清华集训 2017」生成树计数 题目描述 在一个 \(s\) 个点的图中,存在 \(s-n\) 条边,使图中形成了 \(n\) 个连通块,第 \(i\) 个连通块中有 \(a_i\ ...
- 【刷题】LOJ 6227 「网络流 24 题」最长k可重线段集问题
题目描述 给定平面 \(\text{xoy}\) 上 \(n\) 个开线段组成的集合 \(\text{I}\) ,和一个正整数 \(k\) ,试设计一个算法. 从开线段集合 \(\text{I}\) ...
随机推荐
- __getattr__,settr
__getattr__ 如果属性查找在实例以及对应的类中(通过__dict__)失败, 那么会调用到类的__getattr__函数, 如果没有定义这个函数,那么抛出AttributeError异常. ...
- getContextPath、getServletPath、getRequestURI、request.getRealPath的区别
1 区别 假定你的web application 名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 1.1 System.o ...
- 前端 HTML body标签相关内容 常用标签 换行标签 br
换行标签 <br> <br>标签用来将内容换行,其在HTML网页上的效果相当于我们平时使用word编辑文档时使用回车换行. 在第一行中间加上br <!DOCTYPE ht ...
- 解决idea工具下tomcat中文乱码问题
在运行/调试 配置对话框的Startup/Connection面板中, 勾选Pass environment variables. 并添加一个environment variable, Name填 J ...
- leadJS初构建
目录: 1. 面向对象篇 2. 数据结构篇 3. 全局函数篇 4. APICloud篇 1. 面向对象篇 JS原本无法进行程序员世界的面向对象编程,故此对JS封装成一种具有面向对象编程能力的JS. / ...
- CentOS6.5安装Twemproxy集群
Twemproxy,也叫Nutcraker.是一个Twtter开源的一个Redis和Memcache代理服务器. Redis作为一个高效的缓存服务器,非常具有应用价值.但是当使用比较多的时候,就希望可 ...
- DirectShow SDK下载
http://blog.csdn.net/zx3517288/article/details/50547243 Q : GRMSDK_EN_DVD.iso 5 67.3MBGRMSDKIAI_EN_D ...
- ftp命令行敲不了
最先安装了vsftpd,但是命令行敲ftp老是不行 解决方案:ftp命令是ftp客户端,vsftp是ftp服务器,两者不是一个概念.你需要安装ftp客户端 yum install ftp 可以自动安装 ...
- 转载 vsftpd安装
http://blog.csdn.net/shutfuckingup/article/details/8250290 1:安装vsftpd yum install vsftpd 2:关闭防火墙 ...
- 2:5 视图控制器result的配置
result: 1:其实底层还是使用原来servlet的转发和重定向的方法: 2:redirectAction:只能定位到 action (比如下面name属性为 *User 的Action ,但 ...