From 4e3b93b7c2ee8ebcefbcc2c53037e8fbded42843 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 19 May 2024 15:58:59 +0800 Subject: [PATCH] =?UTF-8?q?:white=5Fcheck=5Fmark:=20=E6=9A=B4=E5=8A=9B?= =?UTF-8?q?=E6=B7=B1=E5=BA=A6=E4=BC=98=E5=85=88=E6=90=9C=E7=B4=A2=E8=A7=A3?= =?UTF-8?q?=E5=86=B3=E7=BF=BB=E5=8D=A1=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- flip-card/main.cc | 69 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 flip-card/main.cc diff --git a/flip-card/main.cc b/flip-card/main.cc new file mode 100644 index 0000000..ed98985 --- /dev/null +++ b/flip-card/main.cc @@ -0,0 +1,69 @@ +#include +#include +#include +using namespace std; + +const int N = 50; + +char tiles[N + 5][N + 5]; +bool visited[N + 5][N + 5]; + +int dx[4] = {-1, 1, 0, 0}; +int dy[4] = {0, 0, -1, 1}; + +bool in_range(int x, int y, int rows, int cols) { + return x >= 0 && x < rows && y >= 0 && y < cols; +} + +int dfs(int x, int y, int rows, int cols) { + if (!in_range(x, y, rows, cols) || visited[x][y] || tiles[x][y] == 'B') { + return 0; + } + + visited[x][y] = true; + int size = 1; + + for (int i = 0; i < 4; ++i) { + int px = x + dx[i]; + int py = y + dy[i]; + + size += dfs(px, py, rows, cols); + } + + return size; +} + +int find_largest_area(int rows, int cols) { + int max_size = 0; + + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + if (!visited[i][j] && tiles[i][j] == 'A') { + int size = dfs(i, j, rows, cols); + max_size = max(max_size, size); + } + } + } + + return max_size; +} + +int main() { + int n; + cin >> n; + for (int x = 0; x < n; x++) + for (int y = 0; y < n; y++) + cin >> tiles[x][y]; + int max_chunk = 0; + for (int x = 0; x < n; x++) { + for (int y = 0; y < n; y++) { + if (tiles[x][y] == 'A') + continue; + tiles[x][y] = 'A'; + memset(visited, 0, sizeof visited); + max_chunk = max(max_chunk, find_largest_area(n, n)); + tiles[x][y] = 'B'; + } + } + cout << max_chunk << endl; +}