P2937 RE WA 80分求助
  • 板块学术版
  • 楼主Indestructible
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/4/20 20:55
  • 上次更新2024/4/20 22:29:59
查看原帖
P2937 RE WA 80分求助
1126325
Indestructible楼主2024/4/20 20:55
#include<bits/stdc++.h>
using namespace std;
const int maxn=105;
char pic[maxn][maxn];
int step[maxn][maxn];
int op[4][2]={1,0,0,1,-1,0,0,-1};
int n,m,xxs,xx1,yy1,xx2,yy2;
queue<pair<int,int> > q;
int bfs(int x,int y){
	q.push(pair<int,int> (x,y));
	step[x][y]=1;
	while (!q.empty()){
		int f1=q.front().first,f2=q.front().second;
		for (int i=0;i<4;i++){
			int d1=f1,d2=f2;
			while (1){
				d1+=op[i][0],d2+=op[i][1];
				if (d1==xx2 && d2==yy2) return step[f1][f2]+1;
				if (!(d1>0 && d1<=n && d2>0 && d2<=n) || pic[d1][d2]=='*') break;
				if (!step[d1][d2]) q.push(pair<int,int> (d1,d2)),step[d1][d2]=step[f1][f2]+1;
			}
		}
		q.pop();
	}
}
int main(){
	scanf("%d%d",&n,&m);
	swap(n,m);
	for (int i=1;i<=n;i++){
		scanf("%s",pic[i]+1);
		for (int j=1;j<=m;j++){
			if (pic[i][j]=='C'){
				if (xxs) xx2=i,yy2=j;
				else xx1=i,yy1=j,xxs=1;
			}
		}
	}
	printf("%d",bfs(xx1,yy1)-2);
	return 0;
}

https://www.luogu.com.cn/record/156542491

2024/4/20 20:55
加载中...