蒟蒻求助 DFS #9
查看原帖
蒟蒻求助 DFS #9
238691
Leo2020楼主2022/6/21 00:23

RT

#include<bits/stdc++.h>
using namespace std;
int n,m;
bool flag[5015][5015];
int ansx[1000015],ansy[1000015],ans,maxn;
const int DX[4]={0,-1,1,0};
const int DY[4]={1,0,0,-1};
void dfs(int x,int y){
	//cout << ans << " " << x+1 << " " << y+1 <<  endl;
	maxn=max(maxn,ans);
    if(x==n-1&&y==m-1){
        for(int i=0;i<ans;i++){
            cout << ansx[i] << " " << ansy[i] << endl;
        }
        cout << x+1 << " " << y+1 << endl;
        exit(0);
    }
    for(int i=0;i<4;i++){
        int X=x+DX[i],Y=y+DY[i];
        if(X<0||Y<0||X>=n||Y>=m||flag[X][Y]==false)continue;
        ansx[ans]=x+1;
        ansy[ans]=y+1;
        ans++;
        flag[x][y]=false;
        dfs(X,Y);
        ans--;
        flag[x][y]=true;
    }
}
int main(){
	//freopen("tem7.in","r",stdin);
    cin >> n >> m;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            char tem;
            cin >> tem;
            if(tem=='.')flag[i][j]=true;
            else flag[i][j]=false;
        }
    }
    dfs(0,0);
	cout << maxn << endl;
    return 0;
}

2022/6/21 00:23
加载中...