D. Treasure Island

D. Treasure Island
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

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

Treasure Island may be represented as a rectangular table n×mn×m which is surrounded by the ocean. Let us number rows of the field with consecutive integers from 11 to nn from top to bottom and columns with consecutive integers from 11 to mm from left to right. Denote the cell in rr-th row and cc-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)(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.ide

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

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

Input

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

Following nn lines contains strings sisi of length mm describing the island, jj-th character of string sisi 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.xml

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

Output

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

Examples
input
Copy
2 2
..
..
output
Copy
2
input
Copy
4 4
....
#.#.
....
.#..
output
Copy
1
input
Copy
3 4
....
.##.
....
output
Copy
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.ci

 

 

 

題意:' . '表示能夠走,’#‘表示不可走,問從(1,1)到(n,m),要堵住幾個位置,才能使(1,1)到(n,m)沒有路徑可走

 

題解:首先答案只有三個0,1,2(由於從(1,1)往下走一步只有兩條路,向下走或向右走,最多隻要堵住這兩個點就無路可走了),因此跑兩遍DFS便可,第一遍判斷是否有一條路,第二次在第一次標記過的基礎上再判斷是否有第二條路,若沒有就是答案1,不然就是答案2;

 

注意:不能用二維數組保存路徑和標記,會爆掉

#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
#define ll long long
#define mx 10000010
using namespace std;
char s[mx];
int vis[mx];
int n,m;
int dfs(int x,int y)
{
    if(vis[x*m+y]==1||s[x*m+y]=='#'||x>=n||y>=m)//標記過,有阻礙,出界
        return 0;
    if(x==n-1&&y==m-1)
        return 1;
    if(x!=0||y!=0)
        vis[x*m+y]=1;
    return dfs(x+1,y)||dfs(x,y+1);//兩個方向的搜索
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
        scanf("%s",s+i*m);
    if(dfs(0,0)==0)//第一次搜索沒有路徑,直接輸出0
        printf("0\n");
    else if(dfs(0,0)==0)//第一次搜索有路徑,可是第二次搜索沒有,說明僅有一條路徑可走
        printf("1\n");
    else
        printf("2\n");
    return 0;
}
相關文章
相關標籤/搜索