蒟蒻求助
查看原帖
蒟蒻求助
537627
_LSA_楼主2022/3/28 21:36
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int tx[5] = {0,0,0,1,-1};
const int ty[5] = {0,-1,1,0,0};
char mp[60][60];
int t[60][60];
int f[60][60];
int xh,yh,xe,ye,xb,yb;
int r,c;
struct fx{
	int x,y;
	int t;
};
void bfs(){
	queue <fx> q;
	q.push(fx{xb,yb,0});
	f[xb][yb] = 1;
	while(!q.empty()){
		fx now = q.front();
		q.pop();
		for(int i=1;i<=4;i++){
			int xn = now.x+tx[i] , yn = now.y+ty[i] , tn = now.t+1;
			if(mp[xn][yn] == 'D'){
				cout << tn;
				exit(0);
			}
			if(xn < 1 || xn > r || yn < 1 || yn > c) continue;
			if(f[xn][yn] <= tn || t[xn][yn] <= tn) continue;
			if(mp[xn][yn] != '.') continue;
			q.push(fx{xn,yn,tn});
			f[xn][yn] = tn;
		}
	}
}
int main(){
	//ios::sync_with_stdio(false);
	cin >> r >> c;
	memset(f,99,sizeof(f));
	for(int i=1;i<=r;i++)
		for(int j=1;j<=c;j++){
			cin >> mp[i][j];
			if(mp[i][j] == '*') xh = i,yh = j;
			if(mp[i][j] == 'D') xe = i,ye = j;
			if(mp[i][j] == 'S') xb = i,yb = j;
		}	
	queue <fx> q;
	q.push(fx{xh,yh,0});
	while(!q.empty()){
		fx now = q.front();
		q.pop();
		for(int i=1;i<=4;i++)
			if(!t[now.x+tx[i]][now.y+ty[i]] && mp[now.x+tx[i]][now.y+ty[i]] == '.'){
				q.push(fx{now.x+tx[i],now.y+ty[i],now.t+1});
				t[now.x+tx[i]][now.y+ty[i]] = now.t+1;
			}
	}
	bfs();
	cout << "KAKTUS";
	return 0;
}

50分不知哪里错

2022/3/28 21:36
加载中...