[codeforces1214D]Treasure Island

time limit per test : 1 second
memory limit per test : 512 megabytes

分数:1800 但是感觉确实有难度()

All of us love treasures, right? That’s why young Vasya is heading for a Treasure Island.

Treasure Island may be represented as a rectangular table n × m n×m which is surrounded by the ocean. Let us number rows of the field with consecutive integers from 1 1 to n n from top to bottom and columns with consecutive integers from 1 to m from left to right. Denote the cell in r r -th row and c c -th column as ( r , c ) (r,c) . Some of the island cells contain impassable forests, and some cells are free and passable. Treasure is hidden in cell ( n , m ) (n,m) .

Vasya got off the ship in cell ( 1 , 1 ) (1,1) . Now he wants to reach the treasure. He is hurrying up, so he can move only from cell to the cell in next row (downwards) or next column (rightwards), i.e. from cell (x,y) he can move only to cells ( x + 1 , y ) (x+1,y) and ( x , y + 1 ) (x,y+1) . Of course Vasya can’t move through cells with impassable forests.

Evil Witch is aware of Vasya’s journey and she is going to prevent him from reaching the treasure. Before Vasya’s first move she is able to grow using her evil magic impassable forests in previously free cells. Witch is able to grow a forest in any number of any free cells except cells ( 1 , 1 ) (1,1)
where Vasya got off his ship and ( n , m ) (n,m) where the treasure is hidden.

Help Evil Witch by finding out the minimum number of cells she has to turn into impassable forests so that Vasya is no longer able to reach the treasure.

Input

First line of input contains two positive integers n , m ( 3 n m 1000000 ) n, m (3≤n⋅m≤1000000) , sizes of the island.

Following n n lines contains strings s i s_i of length m m describing the island, j j -th character of string s i s_i equals “#” if cell ( i , j ) (i,j) contains an impassable forest and “.” if the cell is free and passable. Let us remind you that Vasya gets of his ship at the cell ( 1 , 1 ) (1,1) , i.e. the first cell of the first row, and he wants to reach cell ( n , m ) (n,m) , i.e. the last cell of the last row.

It’s guaranteed, that cells ( 1 , 1 ) (1,1) and ( n , m ) (n,m) are empty.

Output

Print the only integer k k , which is the minimum number of cells Evil Witch has to turn into impassable forest in order to prevent Vasya from reaching the treasure.

Examples
Input

2 2
..
..

Output

2

Input

4 4
....
#.#.
....
.#..

Output

1

Input

3 4
....
.##.
....

Output

2

Note

The following picture illustrates the island in the third example. Blue arrows show possible paths Vasya may use to go from ( 1 , 1 ) (1,1) to ( n , m ) (n,m) . Red illustrates one possible set of cells for the Witch to turn into impassable forest to make Vasya’s trip from ( 1 , 1 ) (1,1) to ( n , m ) (n,m) impossible.

在这里插入图片描述

题意:
给定一个 n m n*m 的网格,有些格子上是障碍物,你刚开始在左上角那个点,然后你每次可以向下或者向右走,问至少增加多少个障碍物能使得无法从左上角走到右下角。

题解:
不是很懂咋搞对偶图转化。
那就写个dfs好了。因为最多你只需要两个障碍物(把起点围起来)
先dfs一次把一条路给封上,如果到不了终点那么就是0个障碍,然后再dfs一次,如果还能到达右下角的话则说明要两个障碍,否则就一个障碍。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,m;
int getpos(int x,int y){return (x-1)*m+y;}
char s[1000004];
int mp[1000004],vis[1000004];
bool dfs(int x,int y){
    if(x<=0||x>n||y<=0||y>m||vis[getpos(x,y)])return 0;
    if(mp[getpos(x,y)])return 0;
    vis[getpos(x,y)]=1;
    if(x==n&&y==m)return 1;
    return (dfs(x+1,y)||dfs(x,y+1));
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%s",s+1);
        for(int j=1;j<=m;j++)mp[getpos(i,j)]=(s[j]=='#');
    }
    if(!dfs(1,1))return puts("0"),0;
    vis[getpos(1,1)]=0;vis[getpos(n,m)]=0;
    if(!dfs(1,1))return puts("1"),0;
    else return puts("2"),0;
    return 0;
}
发布了302 篇原创文章 · 获赞 19 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/dxyinme/article/details/100622805
今日推荐