/** * Author: Tang Qiao */ #include<bits/stdc++.h> usingnamespace std; int n, m; char tu[105][105]; int ans; int movex[8] = {0, 0, 1, -1, 1, 1, -1, -1}; int movey[8] = {1, -1, 0, 0, 1, -1, 1, -1};
voiddfs(int x, int y){ tu[x][y] = '.'; for (int i = 0; i < 8; i++) { int nx = x + movex[i]; int ny = y + movey[i]; if (nx < 0 || nx >= n || ny < 0 || ny >= m || tu[nx][ny] != 'W') continue; dfs(nx, ny); } }
intmain(){ cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> tu[i][j]; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (tu[i][j] == 'W') { dfs(i, j); ans++; } } } cout << ans << endl; return0; }
// 行列唯一性检查 boolfinal_check(){ set<int> row_set, col_set; for (int i = 0; i < 6; i++) { int v = 0; for (int j = 0; j < 6; ++j) { v = v * 10 + (tu[i][j] - '0'); } row_set.insert(v); } if (row_set.size() != 6) returnfalse; for (int j = 0; j < 6; ++j) { int v = 0; for (int i = 0; i < 6; ++i) { v = v * 10 + (tu[i][j] - '0'); } col_set.insert(v); } if (col_set.size() != 6) returnfalse; returntrue; }
voiddfs(int r, int c); voidtry_dfs(int r, int c){ char ch = tu[r][c]; row_cnt[r][ch - '0']++; col_cnt[c][ch - '0']++; if (check(r, c)) { int nr = r; int nc = c + 1; if (nc == 6) { nr++; nc = 0; } dfs(nr, nc); } row_cnt[r][ch - '0']--; col_cnt[c][ch - '0']--; }
voiddfs(int r, int c){ if (r == 6) { if (final_check()) { for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { cout << tu[i][j]; } } cout << endl; // 因为只有一个合法解,所以找到答案就退出 exit(0); } return; } if (mark[r][c] == 0) { tu[r][c] = '1'; try_dfs(r, c); tu[r][c] = '0'; try_dfs(r, c); } else { tu[r][c] = mark[r][c]; try_dfs(r, c); } }