萌新第一天学c艹,性感BFS代码求条,70分TLE
  • 板块P1141 01迷宫
  • 楼主2011Andy
  • 当前回复7
  • 已保存回复7
  • 发布时间2024/4/27 17:19
  • 上次更新2024/4/27 19:49:06
查看原帖
萌新第一天学c艹,性感BFS代码求条,70分TLE
660871
2011Andy楼主2024/4/27 17:19
#include <bits/stdc++.h>
#define PII pair<int , int >
using namespace std;
int n , m;
queue<PII > q;
char a[1005][1005];
int mark[1005][1005];
int h[4][2] = {1 , 0 , -1 , 0 , 0 , 1 , 0 , -1};
int ans = 0;
void bfs(int x , int y){
	memset(mark , 0 , sizeof(mark));
	q.push({x , y});
	mark[x][y] = 1;
	ans++;
	while(!q.empty()){
		PII t = q.front();
		int tx = t.first , ty = t.second;
		q.pop();
		for(int i = 0 ; i < 4 ; i++){
			int dx = tx + h[i][0];
			int dy = ty + h[i][1];
			if(dx >= 1 && dx <= n && dy >= 1 && dy <= n && !mark[dx][dy] && a[dx][dy] != a[tx][ty]){
				ans++;
				mark[dx][dy] = 1;
				q.push({dx , dy});
			}
		}
	}
	cout << ans << "\n";
}
int main(){
	cin >> n >> m;
	for(int i = 1 ; i <= n ; i++){
		for(int j = 1 ; j <= n ; j++){
			cin >> a[i][j];
		}	
	}
	for(int i = 1 ; i <= m ; i++){
		int x , y;
		cin >> x >> y;
		ans = 0;
		bfs(x , y);
	}
    return 0;
}

悬关

在线等

2024/4/27 17:19
加载中...