Chamber of Secrets(bfs)

题目:

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

InputcopyOutputcopy
3 3
.#.
...
.#.
2

Sample 2

InputcopyOutputcopy
4 3
##.
...
.#.
.#.
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.

思路:

采用宽度优先搜索解决。

首先,将第一行的所有‘#’的点加入队列q。

然后进行循环,当队列不为空时,pop出队列的第一个点,把与该所在的行列中所有为‘#且未被眼神透过的位置,加入队列中,并更新到该位置的使用魔法的最小次数。

最后,算出眼神到达最后一行时魔法的最小次数,输出该值,若无法到达则输出-1。

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
#define pa pair<int,int>
const int inf = 1000000000;
char a[1005][1005];
int b[1005][1005];
bool jd[1005][1005];
int n,m;
queue<pa> q;
queue<int> ox[1005];
queue<int> oy[1005];
int main()
{
	memset(a,0,sizeof(a));
	memset(jd,0,sizeof(jd));
	scanf("%d%d",&n,&m);
	for (int i = 0; i <= n; i++){
		for (int j = 0; j < m; j++){
			b[i][j] = inf;
		}
	 } 
	for (int i = 1; i <= n; i++)
	{
		scanf("%s",a[i]);
	}
	for (int i = 1; i <= n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			if(a[i][j] == '#')
			{
				ox[i].push(j);
				oy[j].push(i);
			}
		}
	}
	while (ox[1].size())
	{
		int tmp = ox[1].front();
		ox[1].pop();
		jd[1][tmp] = 1;
		b[1][tmp] = 1;
		q.push(pa(1,tmp));
	}
	while(q.size())
	{
		pa p = q.front();
		q.pop();
		int x = p.first;
		int y = p.second;
		while (ox[x].size())
		{
			int tmp = ox[x].front();
			ox[x].pop();
			if(jd[x][tmp]) 
				continue;
			jd[x][tmp] = 1;
			b[x][tmp] = b[x][y]+1;
			q.push(pa(x,tmp));
		}
		while (oy[y].size())
		{
			int tmp = oy[y].front();
			oy[y].pop();
			if(jd[tmp][y]) 
				continue;
			jd[tmp][y] = 1;
			b[tmp][y] = b[x][y]+1;
			q.push(pa(tmp,y));
		}
	}
	int ans = inf;
	for (int i = 0; i < m; i++)
	{
		ans = min(ans,b[n][i]);
	}
	if (ans == inf) 
		printf("-1");
	else 
		printf("%d",ans);
	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值