39分
查看原帖
39分
1057034
Dark_into_Lie楼主2024/6/16 12:49
#include<bits/stdc++.h>
using namespace std;

char c[305][305];

struct node{
	int t;
	int x,y;
}q[90005];

int dy[10] = {0,1,0,-1,0};
int dx[10] = {0,0,1,0,-1};
bool vis[305][305];

int main(){
	int n,m,xx,yy;
	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] == '@') xx = i,yy = j;
		}
	}
	int head = 1,tail = 2;
	q[head].t = 0,q[head].x = xx,q[head].y = yy;
	while(head < tail){
		if(c[q[head].x][q[head].y] == '='){
			cout << q[head].t << '\n';
			return 0;
		}
		if(c[q[head].x][q[head].y]>='A'&&c[q[head].x][q[head].y]<='Z'){
			int nxx,nyy;
			for(int i = 1;i <= n;i++){
				for(int j = 1;j <= m;j++){
					if(c[i][j]>='A'&&c[i][j]<='Z'&&(i != q[head].x||j != q[head].y)){
						nxx = i,nyy = j;	
					}
				}
			}
			q[head].x = nxx;
			q[head].y = nyy;
			//cout << nxx << ' ' << nyy << ' ' << q[head].t+1 << '\n';
		}
		for(int i = 1;i <= 4;i++){
			int nx,ny;
			nx = q[head].x + dx[i];
			ny = q[head].y + dy[i];
			if(vis[nx][ny]==1) continue;
			if(nx<1||nx>n||ny<1||ny>m) continue;
			if(c[nx][ny] == '#') continue;
			vis[nx][ny] = 1;
			//cout << nx << ' ' << ny << ' ' << q[head].t+1 << '\n';
			q[tail].t = q[head].t+1;
			q[tail].x = nx;
			q[tail].y = ny;
			tail++;
		}
		//cout << '\n';
		head++;
	}
	cout << q[tail - 1].t << '\n';
	return 0;
}
2024/6/16 12:49
加载中...