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.

Input

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

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.

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.

题目大意: 起点是(1,1),,,终点(n,m),只能向下走或者向右走 ,#不能走,女巫可以将一个点变成#。问最少需要将几个点变成#才能使其不能到达终点(n,m)。

题解:先跑一边dfs,如果不可以到达的话,输出0,即不能到达,如果可以到达,把路径标记一下。然后再跑一遍dfs,如果可以不到达的话,输出1,输出2;

存图:  题目中给的数据范围是n*m<1e6 二维数组肯定不行。由于n*m所以我们开一个1e6的一维数组,然后没m个存一次,一共存m次

dfs版AC代码

#include<bits/stdc++.h>
using namespace std;
const int N=1E6+;
int d[][]={,,,};
int n,m;
char mp[N];
bool mark[N];
bool flag= false ;
void dfs(int id){
if(flag) return ;
if(id== n*m-){
flag =true;
return ;
}
else {
int x=id%m;//列
int y=id/m;//行
for(int i=;i<;i++){
int dx=y+d[i][];
int dy=x+d[i][];
if(dx>=n||dy>=m||mark[dx*m+dy]) continue ;
mark[dx*m+dy]=;
dfs(dx*m+dy);
if(flag) return ;
}
}
return ;
} int main(){
cin>>n>>m;
for(int i=;i<n;i++){
scanf("%s",mp+i*m);
for(int j=i*m;j<(i+)*m;j++){
if(mp[j]=='#') mark[j]=;
}
}
int i;
for(i=;i<=;i++){
mark[]=;
mark[n*m-] =;
flag= false ;
dfs();
if(!flag){
cout<<i<<endl;
break;
}
}
if(flag) puts("");
return ;
}

Treasure Island DFS +存图的更多相关文章

  1. [Codeforces 1214D]Treasure Island(dfs)

    [Codeforces 1214D]Treasure Island(dfs) 题面 给出一个n*m的字符矩阵,'.'表示能通过,'#'表示不能通过.每步可以往下或往右走.问至少把多少个'.'变成'#' ...

  2. D. Treasure Island

    D. Treasure Island dfs大法好== 写半天bfs疯狂MLE dfs标记掉路上的一些点 然后再跑一遍dfs #include<bits/stdc++.h> using n ...

  3. POJ 1985.Cow Marathon-树的直径-树的直径模板(BFS、DFS(vector存图)、DFS(前向星存图))

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 7536   Accepted: 3559 Case ...

  4. How far away(DFS+vector存图)

    There are n houses in the village and some bidirectional roads connecting them. Every day peole alwa ...

  5. B - Cow Marathon DFS+vector存图

    After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exerc ...

  6. Pants On Fire(链式前向星存图、dfs)

    Pants On Fire 传送门:链接  来源:upc9653 题目描述 Donald and Mike are the leaders of the free world and haven't ...

  7. Gym 100971A Treasure Island BFS 思维题

    A - Treasure Island Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64 ...

  8. 最短路 spfa 算法 && 链式前向星存图

    推荐博客  https://i.cnblogs.com/EditPosts.aspx?opt=1 http://blog.csdn.net/mcdonnell_douglas/article/deta ...

  9. Count on a tree SPOJ 10628 主席树+LCA(树链剖分实现)(两种存图方式)

    Count on a tree SPOJ 10628 主席树+LCA(树链剖分实现)(两种存图方式) 题外话,这是我第40篇随笔,纪念一下.<( ̄︶ ̄)↗[GO!] 题意 是说有棵树,每个节点上 ...

随机推荐

  1. [模板] LCA-最近公共祖先-倍增法

    2019-11-07 09:25:45 C.树之呼吸-叁之型-树上两点路径长度 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 7 (4 ...

  2. C# NAudio录音和播放音频文件及实时绘制音频波形图(从音频流数据获取,而非设备获取)

    下午写了一篇关于NAudio的录音.播放和波形图的博客,不太满意,感觉写的太乱,又总结了下 NAudio是个相对成熟.开源的C#音频开发工具,它包含录音.播放录音.格式转换.混音调整等功能.本次介绍主 ...

  3. OpenCV-Python OpenCV中的K-Means聚类 | 五十八

    目标 了解如何在OpenCV中使用cv.kmeans()函数进行数据聚类 理解参数 输入参数 sample:它应该是np.float32数据类型,并且每个功能都应该放在单个列中. nclusters( ...

  4. 如何利用python实现为每行添加行数编号

    可能还有更好的方法,在这里我是这么写的,针对小文件可以,但是如果文件内容太多,这种方法感觉不太好 先把所有的数据读取出来,然后利用W覆盖写入模式打开文件进行写入 遍历枚举类型数据后,默认是从0开始,然 ...

  5. 面试官:ThreadLocal的应用场景和注意事项有哪些?

    前言 ThreadLocal主要有如下2个作用 保证线程安全 在线程级别传递变量 保证线程安全 最近一个小伙伴把项目中封装的日期工具类用在多线程环境下居然出了问题,来看看怎么回事吧 日期转换的一个工具 ...

  6. Codeforces Round #625 (1A - 1D)

      A - Journey Planning 题意: 有一列共 n 个城市, 每个城市有美丽值 b[i], 要访问一个子序列的城市, 这个子序列相邻项的原项数之差等于美丽值之差, 求最大的美丽值总和. ...

  7. [noip模拟]种花<快速幂+结论>

    描述: OI太可怕了,我决定回家种田.我在后院里开辟了一块圆形的花圃,准备种花.种花是一种艺术,通过一定技术手法,花材的排列组合会让花变得更加的赏心悦目,这就是花艺.当然你知道,我在种田之前是OIer ...

  8. 图解JVM类加载机制和双亲委派模型

    我们都知道以 .java 结尾的 Java 源文件,经过编译之后会变成 .class 结尾的字节码文件.JVM 通过类加载器来加载字节码文件,然后再执行程序. 什么时候加载一个类 那么,什么时候类加载 ...

  9. Material Design 组件之NavigationView

    今天来看一下 NavigationView 的使用,NavigationView 是一个标准的导航菜单,其菜单内容由菜单资源文件来填充,NavigationView 一般和 DrawerLayout ...

  10. Python数据库之数据操作

    一 介绍 MySQL数据操作: DML ======================================================== 在MySQL管理软件中,可以通过SQL语句中的 ...