站外题求助(玄关)
  • 板块学术版
  • 楼主totoe
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/7/22 15:25
  • 上次更新2024/7/22 16:14:08
查看原帖
站外题求助(玄关)
1229287
totoe楼主2024/7/22 15:25

#include<iostream>
using namespace std;
int m,n;
int x,y,x1,y1;
char c[105][105];
long long maxx=100000000000000;
int v[105][105];
int t[100]={-1,1,0,0},t1[100]={0,0,1,-1};
void vc(int a,int b,int cnt){
	v[a][b]=1;
	if(cnt<maxx&&a==x1&&b==y1){
		maxx=cnt;
		return ;
	}
	if(cnt>maxx){
		return ;
	}
	for(int i=0;i<=3;++i){
	    int o=a+t[i];
	    int p=b+t1[i];
	    if(v[o][p]==0&&o>=1&&o<=n&&p>=1&p<=m&&c[o][p]=='.'){
	    	vc(o,p,cnt+1);
			v[o][p]=0; 
		}
	}
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;++i){
    	for(int j=1;j<=m;++j){
    		cin>>c[i][j];
    		if(c[i][j]=='S'){
    			x=i;
    			y=j;
			}
			if(c[i][j]=='T'){
				x1=i;-
				y1=j;
			}
		}
	}
	vc(x,y,0);
	cout<<maxx;
    return 0;
}
2024/7/22 15:25
加载中...