ACM——CodeForces - 1214D

https://vjudge.net/contest/361782#problem/D

解題思路:只需兩次dfs即可,第一次找到第一條(1,1)到(n,m)的路徑,並且路徑上的點mark掉;第二次dfs就是判斷是否還有(1,1)到(n,m)的路徑。

  • (1)如果第一次dfs沒有搜索到路徑,ans=0;
  • (2)如果第二次沒有搜索到路徑,ans=1;
  • (3)如果第二次搜索到了路徑,ans=2;

 

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const ll maxn=1000000+5;
string s[maxn];
//由於直接開數組(maxn*maxn)實在太大,所以根據輸入的n,m用vector的resize函數動態申請 
vector<bool > vis[maxn];
vector<bool > mark[maxn];
ll n,m;

bool dfs(int x,int y){
	if(x==n&&y==m)return true;
	if(x>n||y>m||s[x-1][y-1]=='#'||vis[x][y]||mark[x][y])return false;
	vis[x][y]=true;
	bool flag=dfs(x+1,y);//先往下走 
	if(!flag)flag=dfs(x,y+1);//往下走行不通在向右走 
	if(flag)mark[x][y]=true;//判斷在此點能否有通路,有,則mark 
	return flag;
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>s[i-1];
		vis[i].resize(m+1);//動態申請 
		mark[i].resize(m+1);
	}
	bool flag=dfs(1,1);//第一次dfs 
	if(!flag){
		cout<<0<<endl;
	}
	else {
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				vis[i][j]=false;
			}
		}
		mark[1][1]=mark[n][m]=false;// 
		flag=dfs(1,1);//第二次dfs 
		if(flag)cout<<2<<endl;
		else cout<<1<<endl;
	}
	return 0;
}
//            /\       |  /  |**、
//			 /  \      | /   |   \
//			/    \     |/    |   /  _____                      ____   |  /
//		   /------\    |\    |__/  /     \  \      /\      /  /    \  | /
//		  /        \   | \   |    /       \  \    /  \    /  /______\ |/
//		 /          \  |  \  |    \       /   \  /    \  /   \        |
//      /            \ |   \ |     \_____/     \/      \/     \_____  |
/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神獸保佑,代碼無bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */
// warm heart, wagging tail,and a smile just for you!
//
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                           O\  =  /O
//                        ____/`---'\____
//                      .'  \|     |//  `.
//                     /  \|||  :  |||//  \
//                    /  _||||| -:- |||||-  \
//                    |   | \\  -  /// |   |
//                    | \_|  ''\---/''  |   |
//                    \  .-\__  `-`  ___/-. /
//                  ___`. .'  /--.--\  `. . __
//               ."" '<  `.___\_<|>_/___.'  >'"".
//              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//              \  \ `-.   \_ __\ /__ _/   .-` /  /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章