过样例50pts求调,悬2关,急急急急急
  • 板块P1141 01迷宫
  • 楼主TankGuo
  • 当前回复0
  • 已保存回复0
  • 发布时间2025/7/24 09:55
  • 上次更新2025/7/24 14:55:30
查看原帖
过样例50pts求调,悬2关,急急急急急
936437
TankGuo楼主2025/7/24 09:55

按照dalao的指点,在BFS时把经过的点都赋值避免重复,但是WA5个

急急急急急

#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
const int dx[4] = {0,0,1,-1};
const int dy[4] = {1,-1,0,0};
int n,m,ans; char c;
int xy[N][2],ansxy[N][N];
bool mp[N][N],flag[N][N];
void BFS(int x,int y){
	int len = 0;
	queue<int> qx,qy;
	qx.push(x); qy.push(y);
	while(!qx.empty()){
		int nx = qx.front();
		int ny = qy.front();
//		cout << nx << " " << ny << "\n";
		qx.pop(); qy.pop();
		for(int i = 0;i < 4;i++){
			int tx = nx + dx[i];
			int ty = ny + dy[i];
			if(tx < 1 || tx > n || ty < 1 || ty > n
			|| flag[tx][ty]) continue;
			if(mp[nx][ny] == mp[tx][ty]) continue;
			flag[tx][ty] = 1;
			qx.push(tx); qy.push(ty);
			xy[++len][0] = tx; 
			xy[len][1] = ty;
		}
	} for(int i = 1;i <= n;i++)
	for(int j = 1;j <= n;j++){
		ans += flag[i][j];
	} for(int i = 1;i <= len;i++){
//		cout << xy[i][0] << " ";
//		cout << xy[i][1] << "\n";
		ansxy[xy[i][0]][xy[i][1]] = ans;
	} return ;
} int main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
    cin >> n >> m;
	for(int i = 1;i <= n;i++)
	for(int j = 1;j <= n;j++){
		ansxy[i][j] = -1;
		cin >> c;
		mp[i][j] = c - '0';
	} for(int x = 1;x <= n;x++)
	for(int y = 1;y <= n;y++){
		if(ansxy[x][y] != -1) continue;
		for(int i = 1;i <= n;i++){
			for(int j = 1;j <= n;j++){
				flag[i][j] = 0;
			} xy[i][0] = xy[i][1] = 0;
		} flag[x][y] = 1;
		ans = 0; BFS(x,y);
		ansxy[x][y] = ans;
	} while(m--){ int x,y;
		cin >> x >> y;
		cout << ansxy[x][y] << "\n";
	} 
//	for(int i = 1;i <= n;i++){
//		for(int j = 1;j <= n;j++){
//			cout << ansxy[i][j] << " ";
//		} cout << "\n";
//	}
	return 0;
}
2025/7/24 09:55
加载中...