日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Treasure Island CodeForces - 1214D(dfs)

發(fā)布時(shí)間:2023/12/15 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Treasure Island CodeForces - 1214D(dfs) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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.

題意:這個(gè)人要從(1,1)到(n,m),另一個(gè)人要阻止他,問最少種幾棵樹才能阻止他到達(dá)(n,m)。
思路:由于這個(gè)人只能走兩個(gè)方向,只要把這兩個(gè)方向堵住就行。所以答案只能是0,1,2。我們枚舉從(1,1)開始的那兩條路。看看有幾條可以到達(dá)(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;//走過的點(diǎn)要標(biāo)記,否則會出錯(cuò)。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);//起始把所有的點(diǎn)都設(shè)置為0}int flag1=0,flag2=0;dfs(0,1,flag1);dfs(1,0,flag2);cout<<flag1+flag2<<endl; }

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

總結(jié)

以上是生活随笔為你收集整理的Treasure Island CodeForces - 1214D(dfs)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。