SymmFz @ 2024-03-16 14:26:53
求助
代码如下:
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,m;
char mapp[1100][1100]; // 地图
int sx, sy; // 初始坐标
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
int mapthrouth[1100][1100]; // 属于第几个连通图
int mapthrouth_sum[150000]; // 每个连通图连通的数量
int d=0; // 第几个连通图
queue <int> q;
bool check(int x, int y, int xx, int yy) {
if (xx<0 || xx>=n || yy<0 || yy>=n) return false;
if (mapthrouth[xx][yy] > 0) return false;
bool false0 = (mapp[x][y] == '0' && mapp[xx][yy] == '0') ? true : false;
bool false1 = (mapp[x][y] == '1' && mapp[xx][yy] == '1') ? true : false;
if (false0 || false1) return false;
return true;
}
void bfs(int x, int y) {
// 处于连通图上
if (mapthrouth[x][y] > 0) return;
d++;
q.push(x);
q.push(y);
mapthrouth[x][y] = d;
mapthrouth_sum[d]++;
while (!q.empty()) {
int xx=q.front(); q.pop();
int yy=q.front(); q.pop();
for (int i=0; i<4; i++) {
int xxx=xx+dx[i], yyy=yy+dy[i];
if (!check(xx, yy, xxx, yyy)) continue;
q.push(xxx);
q.push(yyy);
// 处理连通图
mapthrouth[xxx][yyy] = d;
mapthrouth_sum[d]++;
}
}
}
signed main() {
cin >> n >> m;
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
cin >> mapp[i][j];
}
}
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
bfs(i, j);
}
}
for (int i=0; i<m; i++) {
cin >> sx >> sy;
sx = sx-1, sy=sy-1; // 坐标从 0 开始
ans = mapthrouth_sum[mapthrouth[sx][sy]];
cout << ans << endl;
}
return 0;
}
by I_like_play_eggy @ 2024-03-30 22:03:25
我也是QWQ
by I_like_play_eggy @ 2024-03-30 22:03:43
@SymmFz
by I_like_play_eggy @ 2024-03-30 22:04:20
#include<bits/stdc++.h>
#define maxn 1010
#define maxm 100010
using namespace std;
bool a[maxn][maxn];
int vis[maxn][maxn];
int b[maxm],n,m,k,x,y;
int op[4][2]={1,0,0,1,-1,0,0,-1};
string tmp;
void dfs(int x,int y,int k,int c){
vis[x][y]=k,b[k]++;
for (int i=0;i<4;i++){
int dx=x+op[i][0],dy=y+op[i][1];
if (dx>0 && dx<=n && dy>0 && dy<=n && !vis[dx][dy] && !c==a[dx][dy]){
dfs(dx,dy,k,a[dx][dy]);
}
}
}
int main(){
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++){
cin>>tmp;
for (int j=0;j<n;j++){
a[i][j+1]=tmp[j]-'0';
}
}
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
if (vis[i][j]) continue;
dfs(i,j,++k,a[i][j]);
}
}
for (int i=1;i<=m;i++){
scanf("%d%d",&x,&y);
printf("%d\n",b[vis[x][y]]);
}
return 0;
}
by I_like_play_eggy @ 2024-03-30 22:04:45
求助 #3 WA too
by I_like_play_eggy @ 2024-03-30 22:15:23
难度:黄
实际一做:蓝
by I_like_play_eggy @ 2024-04-18 12:32:10
@SymmFz @I_like_play_eggy 今天一看,沃趣,正常黄题,对比了输出,FC表示无差异