[CF1214D]Treasure Island

題面描述

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 which is surrounded by the ocean. Let us number rows of the field with consecutive integers from 1 to n from top to bottom and columns with consecutive integers from 1 to m from left to right. Denote the cell in r-th row and c-th column as (r,c). Some of the island cells contain impassable forests, and some cells are free and passable. Treasure is hidden in cell (n,m).

Vasya got off the ship in cell (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) he can move only to cells (x+1,y) and (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) where Vasya got off his ship and (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.

輸入格式

First line of input contains two positive integers n, m (3≤n?m≤1000000), sizes of the island.

Following n lines contains strings si of length m describing the island, j-th character of string si equals "#" if cell (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), i.e. the first cell of the first row, and he wants to reach cell (n,m), i.e. the last cell of the last row.

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

輸出格式

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.

樣例數(shù)據(jù)

樣例輸入

2 2
..
..

樣例輸出

2

題解

簡單題......
事實上,考慮最大答案拯啦,就是將(1,2)和(2,1)一起堵上框仔,或者將(n-1,m)和(n,m-1)一起堵上垢揩,因此答案最大為2衬横。
再考慮走法菱阵。如果單純判斷是否能到達(dá)起點/終點的相鄰節(jié)點拯坟,可能會出現(xiàn)以下情況:

input:
. . . . . # .
. . . . . # .
. . . . . # .
. . . . . . .
# # # # # # .
. . . . . . .

這時我們發(fā)現(xiàn) 起點和終點的兩個臨近節(jié)點都能到達(dá)晤碘,而答案則是1衍锚,因此我們需要其它方案友题。
考慮是否有兩條通路。我們先進(jìn)行一次dfs戴质,若沒有通路則直接輸出0度宦。若有通路則將這條路徑堵上,并進(jìn)行第二次dfs告匠。若第二次dfs有答案則輸出2斗埂,否則輸出1。
關(guān)于正確性凫海,因為dfs的特性是不碰到障礙物就不會改變移動的方向(左手法則/右手法則)呛凶,因此即使有兩個解,我們也會優(yōu)先堵上不會對另外一個答案產(chǎn)生影響的通路行贪,可以保證正確漾稀。

#include<bits/stdc++.h>
#define int long long
#define maxn 1000
using namespace std;
inline char get(){
    static char buf[30000],*p1=buf,*p2=buf;
    return p1==p2 && (p2=(p1=buf)+fread(buf,1,30000,stdin),p1==p2)?EOF:*p1++;
}
inline int read(){
    register char c=getchar();register int f=1,_=0;
    while(c>'9' || c<'0')f=(c=='-')?-1:1,c=getchar();
    while(c<='9' && c>='0')_=(_<<3)+(_<<1)+(c^48),c=getchar();
    return _*f;
}
int n,m;
set<pair<int,int> > mp,vis;
int dfs(int x,int y){
    //cout<<x<<" "<<y<<endl;
    if(x==n && y==m)return 1;
    if(vis.count(make_pair(x,y)))return 0;
    if(mp.count(make_pair(x,y)))return 0;
    if(x>n || y>m)return 0;
    vis.insert(make_pair(x,y));
    if(dfs(x+1,y)==1){
        mp.insert(make_pair(x+1,y));
        return 1;
    }
    if(dfs(x,y+1)==1){
        mp.insert(make_pair(x,y+1));
        return 1;
    }
    return 0;
}
signed main(){
    //freopen("1.txt","r",stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>m;
    for(register int i=1;i<=n;i++){
        char cas;
        for(register int j=1;j<=m;j++){
            cin>>cas;
            if(cas=='#')mp.insert(make_pair(i,j));
        }
    }
    if(dfs(1,1)==0){
        cout<<0;
        return 0;
    }
    vis.clear();
    if(dfs(1,1)==0){
        cout<<1;
        return 0;
    }
    else{
        cout<<2;
        return 0;
    }
    return 0;
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末模闲,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子崭捍,更是在濱河造成了極大的恐慌尸折,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,723評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件殷蛇,死亡現(xiàn)場離奇詭異实夹,居然都是意外死亡,警方通過查閱死者的電腦和手機粒梦,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,485評論 2 382
  • 文/潘曉璐 我一進(jìn)店門亮航,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人匀们,你說我怎么就攤上這事麦射∪呃酰” “怎么了厅缺?”我有些...
    開封第一講書人閱讀 152,998評論 0 344
  • 文/不壞的土叔 我叫張陵棒妨,是天一觀的道長。 經(jīng)常有香客問我祖灰,道長钟沛,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,323評論 1 279
  • 正文 為了忘掉前任局扶,我火速辦了婚禮讹剔,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘详民。我一直安慰自己延欠,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 64,355評論 5 374
  • 文/花漫 我一把揭開白布沈跨。 她就那樣靜靜地躺著由捎,像睡著了一般。 火紅的嫁衣襯著肌膚如雪饿凛。 梳的紋絲不亂的頭發(fā)上狞玛,一...
    開封第一講書人閱讀 49,079評論 1 285
  • 那天,我揣著相機與錄音涧窒,去河邊找鬼心肪。 笑死,一個胖子當(dāng)著我的面吹牛纠吴,可吹牛的內(nèi)容都是我干的硬鞍。 我是一名探鬼主播,決...
    沈念sama閱讀 38,389評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼固该!你這毒婦竟也來了锅减?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,019評論 0 259
  • 序言:老撾萬榮一對情侶失蹤伐坏,失蹤者是張志新(化名)和其女友劉穎怔匣,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體桦沉,經(jīng)...
    沈念sama閱讀 43,519評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡每瞒,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,971評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了纯露。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片剿骨。...
    茶點故事閱讀 38,100評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖苔埋,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情蜒犯,我是刑警寧澤组橄,帶...
    沈念sama閱讀 33,738評論 4 324
  • 正文 年R本政府宣布,位于F島的核電站罚随,受9級特大地震影響玉工,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜淘菩,卻給世界環(huán)境...
    茶點故事閱讀 39,293評論 3 307
  • 文/蒙蒙 一遵班、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧潮改,春花似錦狭郑、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,289評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至糕殉,卻和暖如春亩鬼,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背阿蝶。 一陣腳步聲響...
    開封第一講書人閱讀 31,517評論 1 262
  • 我被黑心中介騙來泰國打工雳锋, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人羡洁。 一個月前我還...
    沈念sama閱讀 45,547評論 2 354
  • 正文 我出身青樓玷过,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子冶匹,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 42,834評論 2 345

推薦閱讀更多精彩內(nèi)容