题目描述

译自 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的更多相关文章

  1. 【LOJ】#3032. 「JOISC 2019 Day1」馕

    LOJ#3032. 「JOISC 2019 Day1」馕 处理出每个人把馕切成N段,每一段快乐度相同,我们选择第一个排在最前的人分给他的第一段,然后再在未选取的的人中选一个第二个排在最前的切一下,并把 ...

  2. 【LOJ】#3031. 「JOISC 2019 Day1」聚会

    LOJ#3031. 「JOISC 2019 Day1」聚会 听说随机可过? 我想了很久想了一个不会被卡的做法,建出前\(u - 1\)个点的虚树,然后找第\(u\)个点的插入位置,就是每次找一条最长链 ...

  3. 【LOJ】#3030. 「JOISC 2019 Day1」考试

    LOJ#3030. 「JOISC 2019 Day1」考试 看起来求一个奇怪图形(两条和坐标轴平行的线被切掉了一个角)内包括的点个数 too naive! 首先熟练的转化求不被这个图形包含的个数 -- ...

  4. [LOJ#2327]「清华集训 2017」福若格斯

    [LOJ#2327]「清华集训 2017」福若格斯 试题描述 小d是4xx9小游戏高手. 有一天,小d发现了一个很经典的小游戏:跳青蛙. 游戏在一个 \(5\) 个格子的棋盘上进行.在游戏的一开始,最 ...

  5. Loj #2331. 「清华集训 2017」某位歌姬的故事

    Loj #2331. 「清华集训 2017」某位歌姬的故事 IA 是一名会唱歌的女孩子. IOI2018 就要来了,IA 决定给参赛选手们写一首歌,以表达美好的祝愿.这首歌一共有 \(n\) 个音符, ...

  6. Loj #2324. 「清华集训 2017」小 Y 和二叉树

    Loj #2324. 「清华集训 2017」小 Y 和二叉树 小Y是一个心灵手巧的OIer,她有许多二叉树模型. 小Y的二叉树模型中,每个结点都具有一个编号,小Y把她最喜欢的一个二叉树模型挂在了墙上, ...

  7. Loj #2321. 「清华集训 2017」无限之环

    Loj #2321. 「清华集训 2017」无限之环 曾经有一款流行的游戏,叫做 *Infinity Loop***,先来简单的介绍一下这个游戏: 游戏在一个 \(n \times m\) 的网格状棋 ...

  8. Loj 2320.「清华集训 2017」生成树计数

    Loj 2320.「清华集训 2017」生成树计数 题目描述 在一个 \(s\) 个点的图中,存在 \(s-n\) 条边,使图中形成了 \(n\) 个连通块,第 \(i\) 个连通块中有 \(a_i\ ...

  9. 【刷题】LOJ 6227 「网络流 24 题」最长k可重线段集问题

    题目描述 给定平面 \(\text{xoy}\) 上 \(n\) 个开线段组成的集合 \(\text{I}\) ,和一个正整数 \(k\) ,试设计一个算法. 从开线段集合 \(\text{I}\) ...

随机推荐

  1. 利用compass制作雪碧图

    compass是什么?是sass一款神奇插件,具体教程,我还是推荐阮一峰sass,compass教程,简单清晰明了. 用ps制作雪碧图,工作效率太低了.用compass来制作,方便很多.下图的用com ...

  2. MYSQL中的BlackHole引擎

    MYSQL中的BlackHole引擎 http://blog.csdn.net/ylspirit/article/details/7234021 http://blog.chinaunix.net/u ...

  3. [LeetCode] 193. Valid Phone Numbers_Easy tag: Bash

    Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...

  4. SQL Expression Language Tutorial 学习笔记二

    11. Using Textual SQL 直接使用 SQL 如果实在玩不转, 还是可以通过 test() 直接写 SQL. In [51]: s = text( ...: "SELECT ...

  5. PIMPL(二)

    文档下载 上一篇文档,PIMPL(一) 1 如何使用PIMPL 有多种方式实现PIMPL,这里按照<Effective C++>中介绍的方式. 1.1 基本步骤 假设原有Person如下: ...

  6. MDX导航结构层次:《Microsoft SQL Server 2008 MDX Step by Step》学习笔记九

    <Microsoft SQL Server 2008 MDX Step by Step>学习笔记九:导航结构层次   SQL Server 2008中SQL应用系列及BI笔记系列--目录索 ...

  7. Integer类之成员变量

    一.一共11个成员变量. 二.详情介绍. 1.value值.这个是Integer类的唯一标志.最重要的实例属性. 2.最小值和最大值常量.注意,计算机里面是以补码形式保存的,因此用十六进制时,给的数据 ...

  8. 014-配置SSH免密钥登录

    问题:client端需要免密钥登录服务器server如何配置?1.前提:客户端已安装openssh-client;服务端已安装openssh-server;服务器端22号端口已经打开2.需要密钥登录时 ...

  9. 教你玩转产品管理系统iClap(基础功能篇)

    距iClap这款宇宙级产品的推出已经有一段时间了,相信不少小伙伴们都已经开始使用上了,多好用多方便,就不用说了,可不想违反广告法呢!不过还是有用户反映说某些功能不太了解,或者还有一些不清楚的操作方式, ...

  10. org.apache.catalina.core.StandardWrapperValve invoke的解决办法

    org.apache.catalina.core.StandardWrapperValve invoke的解决办法 比较容易错的地方是页面带参数进行跳转,由于跳转之后的页面本身也要执行一部分sql语句 ...