Submission
Status:
PPPPP
Score: 100
User: Winzzwz
Problemset: เดินสลับสี
Language: cpp
Time: 0.002 second
Submitted On: 2025-03-09 12:18:44
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct hee {
int i,j,last;
};
int n,di[4] = {1,-1,0,0}, dj[4] = {0,0,1,-1},mark[40][40],cnt;
char t[40][40];
queue <hee> qu;
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> t[i][j];
}
}
qu.push({1,1,t[1][1] == '.' ? 0 : 1});
while (!qu.empty()) {
hee top = qu.front(); qu.pop();
char c = (top.last == 0) ? '.' : '#';
for (int k = 0; k < 4; k++) {
int ni = top.i + di[k];
int nj = top.j + dj[k];
if (ni >= 1 && nj >= 1 && ni <= n && nj <= n && mark[ni][nj] == 0 && c != t[ni][nj]) {
cnt++;
mark[ni][nj] = 1;
qu.push({ni,nj,t[ni][nj] == '.' ? 0 : 1});
}
}
}
cout << cnt;
return 0;
}