版本比较
2.4-染色、洪水填充
正文
增加 55 行删除 55 行
26 行未改动272829303131323334353636373839404140414243444445464748495051525152535455565758595960616262636464656667686970707172737475767777787980818283848586868788899022 行未改动11311411511611711711811912012112212212312412512612712612712812913013013113213313413513613713813813914014114214314414514514614714814915015115215315415515515615715815916016116216316416516216316416516616716816914 行未改动1841851861871881891881891901911921931941951961971971981992002002012022022032042052062072072082092102112122132142152142152162172182192192202212222236 行未改动23023123223323423423523623723823924023924024124224324412 行未改动25725825926026126126226326426526 行未改动2922932942952962962972982993002 行未改动30330430530630730830730830931031031131231231331431531610 行未改动32732832933033133133233333433533433533633733833934034134234334434534534634734834934935035135135235335435535435535635735835935735835936036136136226 行未改动
/*染色&洪水填充
连通性的问题 连通性的问题
主函数里的逻辑:
枚举遍历二维数组,遇到连通块上的点 1~9,计数 枚举遍历二维数组,遇到连通块上的点 1~9,计数
洪水填充:
1. 0行0列~n+1行m+1列看作是矩阵 2. 对0,0跑染色 1. 0行0列~n+1行m+1列看作是矩阵2. 对0,0跑染色
易错点:1. 矩阵扩张到了 0行0列~n+1行m+1列,边界问题 1. 矩阵扩张到了 0行0列~n+1行m+1列,边界问题*/
int n, m, cnt, sum;int vis[105][105];char mp[105][105];int d[8][2] = { {1, 0}, {-1, 0}, {0, -1}, {0, 1}, {-1, -1}, {1, -1}, {-1, 1}, {1, 1} }; {1, 0}, {-1, 0}, {0, -1}, {0, 1}, {-1, -1}, {1, -1}, {-1, 1}, {1, 1}};
void dfs(int x, int y) { vis[x][y] = 1; // 当前格子标记成1 for (int i=0; i<8; i++) { int nx = x + d[i][0]; int ny = y + d[i][1];
if (nx < 1 or nx > n or ny < 1 or ny > m) continue; if (mp[nx][ny] == '.' or vis[nx][ny] == 1) continue;
dfs(nx, ny); } }}
signed main(){ ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> m; for (int i=1; i<=n; i++) { for (int j=1; j<=m; j++) { cin >> mp[i][j]; } }
for (int i=1; i<=n; i++) { for (int j=1; j<=m; j++) { if (mp[i][j] == 'W' and vis[i][j] == 0) { cnt++; dfs(i, j); } } }
cout << cnt; return 0;}```22 行未改动
/*染色&洪水填充
连通性的问题 连通性的问题
主函数里的逻辑:
枚举遍历二维数组,遇到连通块上的点 1~9,计数 枚举遍历二维数组,遇到连通块上的点 1~9,计数
洪水填充:
1. 0行0列~n+1行m+1列看作是矩阵 2. 对0,0跑染色 1. 0行0列~n+1行m+1列看作是矩阵2. 对0,0跑染色
易错点:1. 矩阵扩张到了 0行0列~n+1行m+1列,边界问题 1. 矩阵扩张到了 0行0列~n+1行m+1列,边界问题*/
int n, m;int vis[105][105];char mp[105][105];int d[4][2] = { {1, 0}, {-1, 0}, {0, -1}, {0, 1}}; };
void dfs(int x, int y) { vis[x][y] = 1; for (int i=0; i<4; i++) { int nx = x + d[i][0]; int ny = y + d[i][1];
if (nx < 0 or nx > n+1 or ny < 0 or n y > m+1) continue; if (mp[nx][ny] == '*' or vis[nx][ny] == 1) continue; dfs(nx, ny); }}
signed main(){ ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> m; for (int i=1; i<=n; i++) { for (int j=1; j<=m; j++) { cin >> mp[i][j]; } } dfs(0, 0); // 找mp[i][j] == '0' 并且 vis[i][j] == 0
dfs(0, 0);
// 找mp[i][j] == '0' 并且 vis[i][j] == 0 return 0;}```
14 行未改动int n, m, cnt, sum;int vis[105][105];char mp[105][105];int d[8][2] = { {1, 0}, {-1, 0}, {0, -1}, {0, 1}, {-1, -1}, {1, -1}, {-1, 1}, {1, 1} }; {1, 0}, {-1, 0}, {0, -1}, {0, 1}, {-1, -1}, {1, -1}, {-1, 1}, {1, 1}};
void dfs(int x, int y) { vis[x][y] = 1; // 当前格子标记成1 sum++; for (int i=0; i<8; i++) { int nx = x + d[i][0]; int ny = y + d[i][1];
if (nx < 1 or nx > n or ny < 1 or ny > m) continue; if (mp[nx][ny] == '.' or vis[nx][ny] == 1) continue;
dfs(nx, ny); } }}
signed main(){ ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
for (int i=1; i<=n; i++) { for (int j=1; j<=m; j++) { if (mp[i][j] == 'W' and vis[i][j] == 0) { cnt++; sum = 0; dfs(i, j); // 每次的sum,就是这次的连通块的格子数量
// 每次的sum,就是这次的连通块的格子数量 } } }
cout << cnt; return 0;}```6 行未改动
int max1, max2;
dfs() { 本次搜索的最大值 本次搜索的最大值}
宝藏 > maxn { maxn = 宝藏; 记录这次的i, j } 记录这次的i, j}*/```
代码:12 行未改动void dfs(int x, int y, int sum){ if(maxn < sum){ maxn = sum; }
for(int i = 0;i < 4;i++){ int nx = x + d[i][0]; int ny = y + d[i][1]; if(nx > n || nx < 1 || ny > m || ny < 1){26 行未改动 } } cout << xx<< ' '<< yy<< '\n'<< res; return 0;} }```
## T5
2 行未改动题意:1是雷,0不是雷。
数字:一个不是雷的格子,周围有几个雷,上面的数字就是几空格:数字是0的,是空格 空:空格的八方向连通块。 空格:数字是0的,是空格空:空格的八方向连通块。
空的数量 + 周围没有空格的数字数量 空的数量 + 周围没有空格的数字数量
1. 根据输入的矩阵,建立好棋盘 1. 根据输入的矩阵,建立好棋盘*/
#include <bit/stdc++.h>using namespace std;10 行未改动 // 让x, y的周围八格,不是雷的,值++ for (int i=0; i<8; i++) { int nx = x + d[i][0]; int ny = y + d[i][1];
if (nx < 1 or nx > n or ny < 1 or ny > m) continue; if (a[nx][ny] == 1) continue; mp[nx][ny]++; } mp[nx][ny]++; }}
int main() { cin >> n >> m; for (int i=1; i<=n; i++) { for (int j=1; j<=m; j++) { cin >> a[i][j]; } }
// 如果 a[i][j] == 1,周围的八个位置,不是雷的格子,值+1 for (int i=1; i<=n; i++) { for (int j=1; j<=m; j++) { if (a[i][j] == 1) { // 自己是雷 if (a[i][j] == 1) { // 自己是雷 mp[i][j] = -1; // 标记自己 // 让周围八个格子不是雷的,数字+1 // 让周围八个格子不是雷的,数字+1 } } } }
/* 1. 统计mp数组上周围没有0的数字数量 2. 空格连通块的数量。。。 mp数组上统计0的八连通块的数量。 */ 1. 统计mp数组上周围没有0的数字数量 2. 空格连通块的数量。。。 mp数组上统计0的八连通块的数量。 */ return 0;} }```