B. Chamber of Secrets

B. Chamber of Secrets
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

"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.

Examples
input
3 3
.#.
...
.#.
output
2
input
4 3
##.
...
.#.
.#.
output
2
Note

The figure above shows the first sample test. In the first sample we should make both columns magic. The dragon figure represents the basilisk and the binoculars represent the person who will enter the Chamber of secrets. The black star shows the place where the person will be petrified. Yellow lines represent basilisk gaze moving through columns.


题目大意:一个二维数组存在镜子,遇到镜子可以发生折射,否则一直走,要求你从左上角到右下角,是否可以到达,至少要几个镜子

解题思路:这道题最短路二维数组用的是BFS,但是因为他有折射,也就是说我要把一个格子当作两个格子去处理,彼此之间双连通,然后从终点往回bfs求即可

#include<iostream>  
#include<cstdio>
#include<stdio.h>
#include<cstring>  
#include<cstdio>  
#include<climits>  
#include<cmath> 
#include<vector>
#include <bitset>
#include<algorithm>  
#include <queue>
#include<map>
using namespace std;
vector<int> tu[20005];
bool vis[20005];
int n, m;
char a[10005][10005];
int lu[10005];

void bfs()
{
	int i, x, y;
	queue<int> qua;
	vis[n] = true;
	qua.push(n);
	while (!qua.empty())
	{
		x=qua.front();
		qua.pop();
		for (i = 0; i < tu[x].size(); i++)
		{
			y = tu[x][i];
			if (vis[y] != true)
			{
				vis[y] = true;
				qua.push(y);
				lu[y] = lu[x] + 1;
			}
		}
	}
}
int main()
{
	int i, j;
	cin >> n >> m;
	memset(vis, false, sizeof(vis));
	memset(lu, 0, sizeof(lu));
	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
		{
			cin >> a[i][j];
			if (a[i][j] == '#')
			{
				tu[i].push_back(j + n);
				tu[j + n].push_back(i);
			}
		}
	}
	bfs();
	if (lu[1] != 0)
	{
		cout << lu[1] << endl;
	}
	else
	{
		cout << -1 << endl;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ISPE(International Society for Pharmaceutical Engineering)控温室是一种专门用于药物工业的设备,用于控制和维持特定温度条件的封闭环境。它通常用于药品制造、质量控制和研发过程中,以确保药品的稳定性和质量。 ISPE控温室具有以下特点: 1. 温度控制:ISPE控温室具备精确的温度控制功能,可以根据需要设置和维持特定的温度范围。这对于药品质量和稳定性至关重要,因为许多药物的有效性和物化特性会随温度变化而改变。通过控温,药品制造商和研究人员可以在稳定的温度条件下进行研发和生产,以确保药品质量符合预期。 2. 精确度和稳定性:ISPE控温室设计和制造时注重精度和稳定性。它采用高精度的温度控制系统和优质的绝缘材料,以减少温度波动和外界温度对内部环境的影响。这种设计保证了控温室内部能够提供准确、稳定的温度环境,使药品制造过程更加可靠和重复性。 3. 安全性:ISPE控温室还具备重要的安全功能。它配备了安全措施,如过温保护系统和报警装置,以确保在温度异常或意外情况下能及时采取措施避免药品受损或危害人员安全。控温室还可以提供密封的环境,减少外部污染物和湿度对药品质量的影响。 总之,ISPE控温室是一种用于药物工业的设备,旨在提供精确稳定的温度环境,以确保药品的质量和稳定性。通过控温室,药品制造商和研究人员可以在一定温度范围内进行研发、生产和质量控制,满足监管要求并确保药品的安全和有效性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值