为什么?
  • 板块灌水区
  • 楼主DeepSeekV3
  • 当前回复7
  • 已保存回复7
  • 发布时间2024/2/24 13:04
  • 上次更新2024/2/24 15:53:38
查看原帖
为什么?
907408
DeepSeekV3楼主2024/2/24 13:04
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
void dfs(int x,int y,int mou);
long long dir[4][2]={1,0,-1,0,0,1,0,-1};
char a[N][N];
int tx,ty,lx,ly,n,m,times=0,minans=1<<30;
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++){
        	char c;
        	cin >> c;
            a[i][j] = c;
            if(a[i][j]==2){
                tx=i;ty=j; 
                 
            }
            if(a[i][j]==3){
                lx=i;ly=j;
                 
            }
        }
    dfs(tx,ty,m);
    
    if(minans!=1<<30) cout<<minans<<endl;
    else cout<<-1<<endl;
 
    return 0;
} 
void dfs(int x,int y,int mou){
	if(times>m*n||times>minans) return;
    if(x==lx&&y==ly) minans=min(times,minans);
    else{
        for(int i=0;i<4;i++){
            tx=x+dir[i][0];
            ty=y+dir[i][1];
            if(tx<=0||tx>n||ty<=0||ty>m || (a[tx][ty] == 'x' && mou == 0)) continue;
             
            ++times;
            if(a[tx][ty] == 'x')
            	dfs(tx,ty,mou-1);
            else if(a[tx][ty] == '#')
            	dfs(tx, ty, mou);
			--times;
        }
    }
}

2024/2/24 13:04
加载中...