MC0473连营阵图
码蹄集OJ-连营阵图
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
const int N = 3e5 + 10;
struct node{int x; // 行坐标int y; // 列坐标
};
queue <node> q;
char ch[3][N];
bool stl[3][N];
int n;
int count_block = 0; int main(void)
{scanf("%d", &n);for(int i = 1; i <= 2; i++){for(int j = 1; j <= n; j++){cin >> ch[i][j];}}for(int i = 1; i <= 2; i++){for(int j = 1; j <= n; j++){if(!stl[i][j]) {count_block++; char current = ch[i][j]; // 当前连通块的字符(0或1)q.push({i, j});stl[i][j] = true;while(!q.empty()){node now = q.front();q.pop();for(int k = 0; k < 4; k++){int tx = now.x + dx[k];int ty = now.y + dy[k];if(tx >= 1 && tx <= 2 && ty >= 1 && ty <= n && !stl[tx][ty] && ch[tx][ty] == current){stl[tx][ty] = true;q.push({tx, ty});}}}}}}cout << count_block << endl;return 0;
}