题目描述

译自 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. Linux新建用户 useradd&groupadd

    建立一个新组,并设置组ID加入系统:#groupadd -g 1000 sparkgroup #useradd -u 2000 -g sparkgroup sparkuser #mkdir -p /a ...

  2. mysql清空有外键关联的表

    第一种:(不要外键约束) 手动删除外键约束: 删除表数据 第二种:(保留外键约束) SET FOREIGN_KEY_CHECKS = 0;   TRUNCATE TABLE 表名;  SET FORE ...

  3. Looper分析。ThreadLocal有关

    Class used to run a message loop for a thread. Threads by default do not have a message loop associa ...

  4. Web Responsive Table, 只需CSS使table在手机和平板中完美显示

    在做responsive或者手机版页面的时候,经常碰到<Table>在手机和平板中会因为长度问题把页面撑大.最近看到一个比较好,比较方便的方法,而且仅仅用CSS 2就可以实现! 实例URL ...

  5. literallycanvas的简介

    LiterallyCanvas是什么 Literally Canvas是一个可扩展的开源(BSD许可)HTML5绘图组件,可以用于网页中插入画图板,类似于windows自带的画图板.可以用可视化工具绘 ...

  6. 系列解读Dropout

    本文主要介绍Dropout及延伸下来的一些方法,以便更深入的理解. 想要提高CNN的表达或分类能力,最直接的方法就是采用更深的网络和更多的神经元,即deeper and wider.但是,复杂的网络也 ...

  7. .Net Core 使用依赖注入

    ASP.NET Core 源码阅读笔记(1) ---Microsoft.Extensions.DependencyInjection 在asp .net中使用依赖注入很简单,只需要在Startup类的 ...

  8. Thinkpad机器BIOS下清除安全芯片和指纹数据的方法

    清除安全芯片: 首先在刚开机出现ThinkPad图标时,按F1进入BIOS界面,然后长按关机按钮关机(注意一定是断电的关机,不是重新启动)然后开机再按F1键进入BIOS设置.选择“Securiy”-〉 ...

  9. unity3d-编辑器结构

    1.Porject视图 Project视图主要存放游戏中用到的所有资源文件,常见的资源包括: 游戏脚本.预设.材质.动画.自定义字体.纹理.物理材质和GUI皮肤.这些资源需要 赋予Hierarchy视 ...

  10. 026-chmod命令

    语法# chmod [ 选项参数 ]  选择修改权限的对象   权限的改变   目标文件 语义:对哪些目标文件的哪些权限进行修改. (1)# chmod -R ugo +r /home/apple.将 ...