题目-Treasure Island-dfs

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×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.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.InputFirst line of input contains two positive integers nn

, mm

(3≤n⋅m≤10000003≤n⋅m≤1000000

), sizes of the island.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.It’s guaranteed, that cells (1,1)(1,1)

and (n,m)(n,m)

are empty.OutputPrint 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.Examples Input 2 2


Output 2
Input 4 4

#.#.

.#…
Output 1
Input 3 4

.##.

Output 2

题目大意:一个n行m列的地图,一个人要从(1,1)这个点走到(n-,m-1)这个点,#符号代表不能经过这里,·代表可以经过这里,一个巫婆可以把·变成#,问巫婆最少变多少次可以让这个人走不到终点。
分析:巫婆改变多少次,取决于这个人可以有几条不同的路径走到终点,而最多改变俩次就可以让这个人走不到终点,可以把这个起点的下面和右面都改成#即可,所以需要dfs俩次,第一次dfs如果能找到一条路径到达终点,那么把这条路劲的所有点标记为走过,然后如果第二次dfs还能找到一条路径的话,便需要修改俩个点。同时观察到n*m<1e6,所以我们这里把二维转化为一维坐标求解,而·转化为一维坐标就得保证起点是(0,0).
ac代码如下:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int N=1e6+10;
int vis[N];
int ans;
int n,m;
char s[N];
 void dfs(int x,int y){
 if(ans)  return;
 if(x==n-1 && y==m-1){
  ans++;
  return ;
 }
 if(x+1<n && vis[(x+1)*m+y]==0){
  vis[(x+1)*m+y]=1;dfs(x+1,y);
 }
 if(ans)   return;
 if(y+1<m && vis[x*m+y+1]==0){
  vis[x*m+y+1]=1;dfs(x,y+1);
 }
}
int main(){
 cin>>n>>m;
 for(int i=0;i<n;i++){
  scanf("%s",s);
  for(int j=0;j<m;j++){
   if(s[j]=='#')  vis[i*m+j]=1;
  }
 }
 vis[0]=1,vis[(n-1)*m+m-1]=0;
 dfs(0,0) ;
 if(ans==0)   puts("0");
 else{
  ans=0;
  vis[0]=1,vis[(n-1)*m+m-1]=0;
  dfs(0,0);
  if(ans==0)   puts("1");
  else         puts("2");
 }
 return 0;
}
发布了29 篇原创文章 · 获赞 24 · 访问量 3674

猜你喜欢

转载自blog.csdn.net/qq_45772483/article/details/104294669
今日推荐