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

题意:这个人要从(1,1)到(n,m),另一个人要阻止他,问最少种几棵树才能阻止他到达(n,m)。
思路:由于这个人只能走两个方向,只要把这两个方向堵住就行。所以答案只能是0,1,2。我们枚举从(1,1)开始的那两条路。看看有几条可以到达(n,m)。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;const int maxx=1e6+100;
int d[][2]={{0,1},{1,0}};
string s[maxx];
vector<int> vis[maxx];
int n,m;inline void dfs(int x,int y,int &flag)
{if(x>=n||y>=m||s[x][y]=='#'||flag) return ;if(x==n-1&&y==m-1){flag=1;return ;}for(int i=0;i<2;i++){int tx=x+d[i][0];int ty=y+d[i][1];if(tx==n-1&&ty==m-1) {flag=1;return ;}if(tx>=n||ty>=m||vis[tx][ty]==1) continue;vis[tx][ty]=1;//走过的点要标记,否则会出错。dfs(tx,ty,flag);if(flag) return ;}
}
int main()
{scanf("%d%d",&n,&m);for(int i=0;i<n;i++) cin>>(s[i]);for(int i=0;i<n;i++){for(int j=0;j<m;j++) vis[i].push_back(0);//起始把所有的点都设置为0}int flag1=0,flag2=0;dfs(0,1,flag1);dfs(1,0,flag2);cout<<flag1+flag2<<endl;
}

努力加油a啊,(o)/~

Treasure Island CodeForces - 1214D(dfs)相关推荐

  1. Tournament CodeForces - 27B(dfs)

    The tournament «Sleepyhead-2010» in the rapid falling asleep has just finished in Berland. n best pa ...

  2. codeforces 723D(DFS)

    题目链接:http://codeforces.com/problemset/problem/723/D 题意:n*m的矩阵中,'*'代表陆地,'.'代表水,连在一起且不沿海的水形成湖泊.问最少填多少块 ...

  3. 三十二、图的创建深度优先遍历(DFS)广度优先遍历(BFS)

    一.图的基本介绍 为什么要有图 前面我们学了线性表和树 线性表局限于一个直接前驱和一个直接后继的关系 树也只能有一个直接前驱也就是父节点 当我们需要表示多对多的关系时, 这里我们就用到了图. 图的举例 ...

  4. 【 MATLAB 】离散傅里叶级数(DFS)及 IDFS 的 MATLAB 实现

    有关离散傅里叶级数(DFS)我之前也写过一些博文,例如:离散周期信号的傅里叶级数(DFS) 这里我再次给出标准公式. 分析式: 其中: 综合式: 这里我必须先声明,关于分析式和综合式前面那个系数1/N ...

  5. 部署分布式文件系统(DFS)

    部署分布式文件系统(DFS) 使用 DFS 命名空间,可以将位于不同服务器上的共享文件夹组合到一个或多个逻辑结构的命名空间.每个命名空间作为具有一系列子文件夹的单个共享文件夹显示给用户.但是,命名空间 ...

  6. Java实现算法导论中图的广度优先搜索(BFS)和深度优先搜索(DFS)

    对算法导论中图的广度优先搜索(BFS)和深度优先搜索(DFS)用Java实现其中的伪代码算法,案例也采用算法导论中的图. import java.util.ArrayList; import java ...

  7. 广度优先搜索(BFS)与深度优先搜索(DFS)

    一.广度优先搜索(BFS) 1.二叉树代码 # 实现一个二叉树 class TreeNode:def __init__(self, x):self.val = xself.left = Nonesel ...

  8. 7.9模拟赛T1图的遍历(dfs)

    图的遍历(dfs) [题目描述] 对于一个有向图G来说,我们存在一个经典的遍历算法,就是DFS (深度优先搜索遍历).将G以1号点为起点进行DFS后,我们可以 得到G的一棵DFS遍历树T.就此,我们可 ...

  9. 7.6 T1 深度优先搜索(dfs)

    深度优先搜索(dfs) [题目描述] sol:50pts随便写写,就是大众分了,直接n2dpOK,100分要找点规律,需要数学头脑 官方题解 //#include <bits/stdc++.h& ...

最新文章

  1. opencv 线性滤波器
  2. php俩个字符串合并,php分割合并两个字符串的函数实例
  3. “GIS讲堂”第二课内容的公布
  4. 详细介绍 安装ns3步骤
  5. java导入日期处理,java实现Excel表格的导入日期变成数字的问题
  6. 拒绝无用的长篇大论!仅12张图片,最全的中台精华都在这里了
  7. 310. 最小高度树
  8. 让getElementsByName适应IE和firefox
  9. 5.企业应用架构模式 --- 并发
  10. Deep Learning经典论文列表(Reading List)
  11. 国二c语言南开版的机试100题,[互联网]免费ncre全国计算机等级考试二级c语言上机---南开100题答案...
  12. 【C语言笔记】【宏定义系列】 判断是否2的n次幂对齐
  13. 用html如何做发帖的页面,如何用html发帖
  14. UltraEdit下Shift键失效
  15. java 加密并打包_我想把java文件先加密然后打包,请高手指教怎么加密,有那种好的加密算法吗?...
  16. Linux 下 美化字体
  17. 广告投放管理平台 oython源码_【直播】全新腾讯广告投放管理平台如何帮助广告主乘风破浪...
  18. java 基础练习(1-5)
  19. 点到点轨迹规划【1】——梯形速度曲线规划
  20. 黑马Python数据分析网课个人笔记01

热门文章

  1. java生成pdf_Java实现PDF文件生成并且打印pdf文件 demo
  2. python时间序列分析航空旅人_python时间序列分析
  3. mysql schedule every_Mysql 架构及优化之-定时计划任务
  4. 论文阅读:SSD: Single Shot MultiBox Detector
  5. h5 app title隐藏_荒岛求生H5:有难度的文字冒险生存游戏,你能生存多久?
  6. getvalue参数计数不匹配_OpenCV开发笔记(六十八):红胖子8分钟带你使用特征点Flann最邻近差值匹配识别...
  7. java 基本语法与流程控制_Java基础语法之控制流程
  8. php常见错误及总结,PHP常见的错误级别总结
  9. AJPFX讲解Java 性能优化[4]:关于 finalize 函数
  10. asp.net 4.0 新特性(翻译)