CF173B: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.


题意:初始在第一行,目标最后一行,问最少需要几个'#'进行转弯。

思路:每个'#'自身x对y建双向边,最短路。

# include <bits/stdc++.h>
# define pb push_back
using namespace std;
const int maxn = 2003;
int n, m, q[maxn<<1], vis[maxn], dis[maxn];
vector<int>v[maxn];

int spfa()
{
    memset(vis, 0, sizeof(vis));
    memset(dis, 0x3f, sizeof(dis));
    int l=0, r=0;
    q[r++] = 1;
    dis[1] = 0;
    while(l<r)
    {
        int s = q[l++];
        vis[s] = 0;
        for(auto e : v[s])
        {
            if(dis[e] > dis[s] + 1)
            {
                dis[e] = dis[s] + 1;
                if(!vis[e])
                q[r++] = e, vis[e] = 1;
            }
        }
    }
    return (dis[n] == 0x3f3f3f3f)?-1:dis[n];
}
int main()
{
    char c;
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; ++i)
    {
        getchar();
        for(int j=1; j<=m; ++j)
        {
            c = getchar();
            if(c == '#')
            {
                v[i].pb(j+n);
                v[j+n].pb(i);
            }
        }
    }
    printf("%d\n",spfa());
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值