参考代码
#include<stdio.h> #include<math.h> #include<string.h> #include<stdbool.h> #include<stdlib.h> void f(int x, int y, int n, char** maze) { if (x - 1 >= 0 && maze[x - 1][y] == '.') { maze[x][y] = '2'; } else if (x + 1 < n && maze[x + 1][y] == '.') { maze[x][y] = '2'; } else if (y - 1 >= 0 && maze[x][y - 1] == '.') { maze[x][y] = '2'; } else if (y + 1 < n && maze[x][y + 1] == '.') { maze[x][y] = '2'; } } void dfs(int x, int y, int n,char** maze) { if (x < 0 || x >= n || y < 0 || y >= n || maze[x][y] != '#') { return ; } maze[x][y] = '1'; dfs(x - 1, y, n, maze); dfs(x + 1, y, n, maze); dfs(x, y + 1, n, maze); dfs(x, y - 1, n, maze); } void dfs1(int x, int y, int n,char** maze) { if (x < 0 || x >= n || y < 0 || y >= n || maze[x][y] == '.' || maze[x][y] == '3') { return ; } maze[x][y] = '3'; dfs1(x - 1, y, n, maze); dfs1(x + 1, y, n, maze); dfs1(x, y + 1, n, maze); dfs1(x, y - 1, n, maze); } int main() { int n; int sign = 0, sign1 = 0; scanf("%d", &n);getchar();getchar(); char** maze = (char**)malloc(sizeof(char*) * n); for (int i = 0; i < n; ++i) { maze[i] = (char*)malloc(sizeof(char) * (n + 1)); } for (int i = 0; i < n; ++i) { gets(maze[i]); } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (maze[i][j] == '#') { dfs(i, j, n, maze); ++sign1; } } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (maze[i][j] == '.') { continue; } f(i, j, n, maze); } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (maze[i][j] == '1') { dfs1(i, j, n, maze); ++sign; } } } printf("%d\n", sign1 - sign); return 0; }
:
0.0分
2 人评分
C语言程序设计教程(第三版)课后习题7.5 (C语言代码)浏览:670 |
C语言程序设计教程(第三版)课后习题4.9 (C语言代码)浏览:387 |
C语言程序设计教程(第三版)课后习题10.2 (C语言代码)浏览:564 |
WU-C语言程序设计教程(第三版)课后习题11.11 (C++代码)(想学链表的可以看看)浏览:1464 |
母牛的故事 (C语言代码)浏览:739 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:1100 |
1126题解浏览:649 |
C语言程序设计教程(第三版)课后习题11.1 (C语言代码)浏览:525 |
用筛法求之N内的素数。 (C语言代码)浏览:595 |
矩形面积交 (C语言代码)浏览:1433 |