46分求调!(AC+WA+RE+TLE)
查看原帖
46分求调!(AC+WA+RE+TLE)
1361171
114514isme楼主2025/6/22 15:43

REREIOT陷阱IOT陷阱

code

#include<bits/stdc++.h>
using namespace std;
char c[305][305];
int n,m;
int st[305][305];
struct point{
	int x,y;
};
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1}; 
map<char,vector<point>> mp;
point fp(int x,int y){
	char dr=c[x][y];
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(!(i==x&&j==y)&&c[i][j]==dr){
				point ret;
				ret.x=i;
				ret.y=j;
				return ret;
			} 
		}
	}
	return point{-1,-1};
}
int bfs(int x,int y){
	queue<point> q;
	q.push({x,y});
	while(!q.empty()){
		int tx=q.front().x;
		int ty=q.front().y;
//		cout<<st[tx][ty]<<'\n';
//		cout<<tx<<' '<<ty<<'\n';
		q.pop();
		if(c[tx][ty]=='=') return st[tx][ty];
		for(int i=0;i<4;i++){
			int nx=tx+dx[i];
			int ny=ty+dy[i];
			if(nx>=0&&nx<n&&ny>=0&&ny<m){
				if(st[nx][ny]<1&&c[nx][ny]!='#'){
					if(c[nx][ny]=='.'||c[nx][ny]=='='){
						st[nx][ny]=st[tx][ty]+1;
						q.push((point){nx,ny});
					}else if(c[nx][ny]>='A'&&c[nx][ny]<='Z'){
						point p=fp(nx,ny);
						if(!(p.x==-1&&p.y==-1)){
//							if(st[nx][ny]<0) st[nx][ny]++;
//							else if(st[nx][ny]==0) st[nx][ny]=st[tx][ty]+1;
							st[p.x][p.y]=st[tx][ty]+1;
							q.push(p);
						}else{
							q.push({nx,ny});
						}
					}
				}
			}
		}
	}
}
int main(){
	memset(st,-1,sizeof st);
	cin>>n>>m;
	int qx=0,qy=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin>>c[i][j];
			if(c[i][j]=='@'){
				qx=i;
				qy=j;
				st[i][j]=0;
			}
			if(c[i][j]>='A'&&c[i][j]<='Z'){
				mp[c[i][j]].push_back({i,j});
			}
		}
	}
//	cout<<qx<<' '<<qy<<'\n';
	cout<<bfs(qx,qy);
	
	return 0;
}
2025/6/22 15:43
加载中...