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.

Treasure Island may be represented as a rectangular table (n,m).

Vasya got off the ship in cell (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 (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 3≤n⋅m≤1000000), sizes of the island.

Following (n,m), i.e. the last cell of the last row.

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

Output

Print the only integer 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
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 (n,m) impossible.

D. Treasure Island

 

 

 

题意:' . '表示可以走,’#‘表示不可走,问从(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;
}

 

相关文章:

  • 2021-12-20
  • 2021-06-13
  • 2021-08-16
  • 2021-11-24
  • 2022-12-23
  • 2021-04-09
猜你喜欢
  • 2019-10-28
  • 2021-12-11
  • 2021-07-05
  • 2021-09-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案