CodeForces 173B Chamber of Secrets spfa
Chamber of Secrets
题目连接:
http://codeforces.com/problemset/problem/173/B
Description
"The Chamber of Secrets has been opened again" — this news has spread all around Hogwarts and some of the students have been petrified due to seeing the basilisk. Dumbledore got fired and now Harry is trying to enter the Chamber of Secrets. These aren't good news for Lord Voldemort. The problem is, he doesn't want anybody to be able to enter the chamber. The Dark Lord is going to be busy sucking life out of Ginny.
The Chamber of Secrets is an n × m rectangular grid in which some of the cells are columns. A light ray (and a basilisk's gaze) passes through the columns without changing its direction. But with some spell we can make a column magic to reflect the light ray (or the gaze) in all four directions when it receives the ray. This is shown in the figure below.
The left light ray passes through a regular column, and the right ray — through the magic column.
The basilisk is located at the right side of the lower right cell of the grid and is looking to the left (in the direction of the lower left cell). According to the legend, anyone who meets a basilisk's gaze directly dies immediately. But if someone meets a basilisk's gaze through a column, this person will get petrified. We know that the door to the Chamber is located on the left side of the upper left corner of the grid and anyone who wants to enter will look in the direction of its movement (in the direction of the upper right cell) from that position.
This figure illustrates the first sample test.
Given the dimensions of the chamber and the location of regular columns, Lord Voldemort has asked you to find the minimum number of columns that we need to make magic so that anyone who wants to enter the chamber would be petrified or just declare that it's impossible to secure the chamber.
Input
The first line of the input contains two integer numbers n and m (2 ≤ n, m ≤ 1000). Each of the next n lines contains m characters. Each character is either "." or "#" and represents one cell of the Chamber grid. It's "." if the corresponding cell is empty and "#" if it's a regular column.
Output
Print the minimum number of columns to make magic or -1 if it's impossible to do.
Sample Input
3 3
.#.
...
.#.
Sample Output
2
Hint
题意
给你一个n*m的矩阵,你从(1,0)位置射出一个平行于x轴的光,遇到#号的时候,你可以选择转弯
然后你要要到达(n,m+1)这个位置
问你最少用多少个#
题解:
直接spfa就可以过
你可以把这个图优化成二分图
然后再跑spfa
代码
#include<bits/stdc++.h>
using namespace std;
vector<int>H[1200];
vector<int>L[1200];
int d[1200][3];
int vis[1200][3];
int n,m;
char str[1200][1200];
struct node
{
int x,y;
};
void bfs(int x,int y)
{
queue<node> Q;
for(int i=0;i<1200;i++)
for(int j=0;j<3;j++)
d[i][j]=1e9;
node now;
now.x=x,now.y=y;
d[now.x][now.y]=0;
vis[now.x][now.y]=1;
Q.push(now);
while(!Q.empty())
{
now = Q.front();
Q.pop();
vis[now.x][now.y]=0;
if(now.y==0)
{
for(int i=0;i<H[now.x].size();i++)
{
node next = now;
next.y = 1;
next.x = H[now.x][i];
if(d[next.x][next.y]>d[now.x][now.y]+1)
{
d[next.x][next.y]=d[now.x][now.y]+1;
if(vis[next.x][next.y])continue;
vis[next.x][next.y]=1;
Q.push(next);
}
}
}
if(now.y==1)
{
for(int i=0;i<L[now.x].size();i++)
{
node next = now;
next.y = 0;
next.x = L[now.x][i];
if(d[next.x][next.y]>d[now.x][now.y]+1)
{
d[next.x][next.y]=d[now.x][now.y]+1;
if(vis[next.x][next.y])continue;
Q.push(next);
}
}
}
}
if(d[n][0]>1e8)
printf("-1\n");
else
printf("%d\n",d[n][0]);
return;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%s",str[i]+1);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(str[i][j]=='#')
H[i].push_back(j),L[j].push_back(i);
bfs(1,0);
}
CodeForces 173B Chamber of Secrets spfa的更多相关文章
- CodeForces 173B Chamber of Secrets 二分图+最短路
题目链接: http://codeforces.com/problemset/problem/173/B 题意: 给你一个n*m的地图,现在有一束激光从左上角往左边射出,每遇到‘#’,你可以选择光线往 ...
- CodeForces 173B Chamber of Secrets (二分图+BFS)
题意:给定上一个n*m的矩阵,你从(1,1)这个位置发出水平向的光,碰到#可以选择四个方向同时发光,或者直接穿过去, 问你用最少的#使得光能够到达 (n,m)并且方向水平向右. 析:很明显的一个最短路 ...
- Codeforces 689B. Mike and Shortcuts SPFA/搜索
B. Mike and Shortcuts time limit per test: 3 seconds memory limit per test: 256 megabytes input: sta ...
- Codeforces Gym 100269D Dwarf Tower spfa
Dwarf Tower 题目连接: http://codeforces.com/gym/100269/attachments Description Little Vasya is playing a ...
- Codeforces 786B. Legacy 线段树+spfa
题目大意: 给定一个\(n\)的点的图.求\(s\)到所有点的最短路 边的给定方式有三种: \(u \to v\) \(u \to [l,r]\) \([l,r] \to v\) 设\(q\)为给定边 ...
- 【CF173B】Chamber of Secrets(二分图,最短路)
题意:给你一个n*m的地图,现在有一束激光从左上角往右边射出,每遇到‘#’,你可以选择光线往四个方向射出,或者什么都不做,问最少需要多少个‘#’往四个方向射出才能使关系在n行往右边射出. 思路:将每一 ...
- Codeforces 346D Robot Control DP spfa 01BFS
题意及思路:https://www.cnblogs.com/zjp-shadow/p/9562888.html 这题由于性质特殊,可以用01BFS来进行DP的转移. 代码: #include < ...
- 配置魔药 [NOIP模拟] [DP] [费用流]
问题描述在<Harry Potter and the Chamber of Secrets>中,Ron 的魔杖因为坐他老爸的 Flying Car 撞到了打人柳,不幸被打断了,从此之后,他 ...
- Harry Potter
Names appearing in "Harry Potter" 1.Harry Potter ①Harry is from Henry. ②Harry is related t ...
随机推荐
- struct 与 typedef struct
1. 基本解释 typedef为C语言的关键字,作用是为一种数据类型定义一个新名字.这里的数据类型包括内部数据类型(int,char等)和自定义的数据类型(struct等). 在编程中使用typede ...
- HDU5828 Rikka with Sequence 线段树
分析:这个题和bc round 73应该是差不多的题,当时是zimpha巨出的,那个是取phi,这个是开根 吐槽:赛场上写的时候直接维护数值相同的区间,然后1A,结果赛后糖教一组数据给hack了,仰慕 ...
- 答 “SOA会不会造成IT黑洞?”
[文/ 任英杰] 随意间看到支点网的“SOA会不会造成IT黑洞”一文,作者对SOA的认识颇有以偏概全之嫌,写点自己的感想,作为应和吧. 作者的二个对SOA的观点有些偏颇:“SOA就是一种系统集成,它是 ...
- JavaScript高级程序设计(第三版)第五章 引用类型
5.2 Array类型 var colors = new Array(3); //创建一个包含3项的数组 var names = new Array("Greg"); //创建一个 ...
- linux 后台运行程序
有些时候,我们需要在终端启动一个程序,并使之运行--但是如果关闭终端,那么这个程序也就随着关闭了.那么有没有什么方法在关闭终端后,让已经从这个终端启动的程序继续运行呢? 前置知识: xterm,con ...
- 各种less开发工具
less是前端开发CSS的神器,但如何让less代码语法高亮,智能提示,快速编译及格式化,这不是一般的IDE的less插件能做到.下面是我搜刮到的一些工具 Codekit - incident57又一 ...
- Linux下的grep搜索命令详解(一)
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...
- [原创]Devexpress XtraReports 系列 3 创建主从报表
昨天写了系列的第二篇Devexpress XtraReports 系列 2 创建表格报表 . 今天我们来继续系列 3 创建主从报表 首先我们来看看最后实现的效果.Demo最后附上. 开始吧. 第一步, ...
- 找回使用过的QQ头像
多么渴望那双眼睛能在万紫千红中发现自己:然而眼睛从来就不曾对自己留意:于是换种落寞再次接受垂直打击:然后然后的然后尼玛再换个逗比的头像证明老资无所谓老资无所谓嘿嘿: 我们换头像的时候,发现之前的一个最 ...
- to_number,Extract oracle的关键字
to_number(Extract(year from 字段名)) 简介:获取时间字段的年份后转换为数字