87分求助!!
查看原帖
87分求助!!
915727
hao_zi6366楼主2023/7/30 16:35

第#6和#14个点WA了

#include<bits/stdc++.h>				P1825
typedef long long ll;
//#define INF 0x7fffffffffffffff
#define N 350
int n,m,sx,sy,fx,fy;
char mp[N][N];
bool vis[N][N];
int step[N][N];
int dx[]={1,-1,0,0},dy[]={0,0,1,-1};
std::map<std::pair<int,int>,std::pair<int,int> > mm;

void bfs(){
	std::queue<std::pair<int,int> > q;
	q.push({sx,sy});
	vis[sx][sy]=1;
	while(!q.empty()){
		auto u=q.front();q.pop();
		int x=u.first,y=u.second;
		for(int i=0;i<4;i++){
			int tx=x+dx[i],ty=y+dy[i];
			if(tx<1 || ty<1 || tx>n || ty>m)continue;
			step[tx][ty]=step[x][y]+1;
			if(tx==fx && ty==fy){
				std::cout<<step[tx][ty];
				return ;
			}
			//std::cout<<tx<<" "<<ty<<" "<<step[x][y]<<"\n";
			if(mp[tx][ty]=='#')continue;
			if(vis[tx][ty])continue; 
			vis[tx][ty]=1;
			if(mp[tx][ty]>='A' && mp[tx][ty]<='Z'){
				auto xy=mm[{tx,ty}];
				tx=xy.first,ty=xy.second;
				step[tx][ty]=step[x][y]+1;
			}
			q.push({tx,ty});
		}
	}
}
int main(){
	std::ios::sync_with_stdio(0);
	std::cin.tie(0);
	std::cout.tie(0);
	
	std::cin>>n>>m;
	std::vector<std::vector<std::pair<int,int> > > w(26);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			std::cin>>mp[i][j];
			if(mp[i][j]=='@')sx=i,sy=j;
			if(mp[i][j]=='=')fx=i,fy=j;
			if(mp[i][j]>='A' && mp[i][j]<='Z'){
				w[mp[i][j]-'A'].push_back({i,j});
			}
		}
	}
	for(int i=0;i<26;i++){
		if(w[i].size()>=2){
			mm[w[i][0]]=w[i][1];
			mm[w[i][1]]=w[i][0];
		} 
	}
	bfs();
	return 0;
}

附上#6的输入输出: 输入:

50 50
##################################################
#DC#A#B..C.D#X.................#........#....#...#
#..#.###.####..................#....#...#..#.#.#.#
#..#..#..##.#..................#....#...#..#.#.#.#
#..#..###...#..................#....#...#..#.#.#.#
#..##..##...#..................#....#...#..#.#.#.#
#.J#I#.......############......#....#...#..#.#.#.#
#..##.#.#.#..#.#.....#.........#....#...#..#.#.#.#
#..#..............##.#..#......#....#...#..#.#.#.#
#..#.......#####...#.#..#......#....#...#..#.#.#.#
#..#.......#S......#.####......#....#...#..#.#.#.#
#..#..######.......#....#......#....#...#..#.#.#.#
#..#..#.............#..M#...........#......#...#.#
##.#####.#.#####################################.#
#..####.........#######....T.U.V..W....V..T..U..W#
#.#####.###############.##########################
#..#.....#..............#.#..#.#...#....#........#
#..#.....#..###########.#...#.#..#...#.....#..#..#
#.E#.....#..#....#..#...####.#.#..#...#.....#....#
#.##......#.#..#..#..#......#.#.#..#...#.....#..S#
#..#.######.#...#..#..#.####.#.#..#...#.....#....#
#..#......#.###..#..#...#...#.#..#...#.....#.#...#
#..######.#.#...###..#..#.#..#.#...#....#.....#..#
#......M#.###......#....#.#..#.#..#..#.#...#.....#
#.#######.#.#.####################################
#.........#.....................................K#
##################################################
#..#...#..........#...#..#................#.....##
#.........#...#..#.......##.#.....#.#....##.#..#K#
#....#.#..........#..#..#...#..#....#....#...#...#
#...........#.....#...............#..............#
##############################.###################
#.........F.....G....H.....G.....I...H......F....#
#.##############################################.#
#.#........#...................................#.#
#J#...###...###...#...###.....###.#...#..###...#.#
##....#..#..#....#.#..#..#....#...##..#..#..#..#.#
#.....#B..#.###.#####.#...#...###.#.#.#..#...#.#.#
#.....#..#..#...#...#.#..#....#...#..##..#..#..#.#
####..###...###.#...#.###.....###.#...#..###...#.#
#..........#...................................#.#
#.##########...................................#.#
#........#........N.N.O.O.R.R.................##.#
########.#.................................###...#
#........#..........................########....Y#
#........######..#Q.#...#############......#######
#.#......#...L#..####...#....#....#.#.....Q#X#...#
#.##.....#..###.........#..#.#..#...########.#Z#.#
#@..APPLE#.....#...........#....#.........Y#.Z.#.=
##################################################

输出:

272

我的输出:

275
2023/7/30 16:35
加载中...