正文
增加 109 行删除 54 行
26 行未改动2728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899022 行未改动11311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816914 行未改动1841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222236 行未改动23023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026110 行未改动27227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630726 行未改动/*染色&洪水填充连通性的问题主函数里的逻辑:枚举遍历二维数组,遇到连通块上的点 1~9,计数洪水填充:1. 0行0列~n+1行m+1列看作是矩阵2. 对0,0跑染色易错点: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}};void dfs(int x, int y) {vis[x][y] = 1; // 当前格子标记成1for (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. 0行0列~n+1行m+1列看作是矩阵2. 对0,0跑染色易错点: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] == 0return 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}};void dfs(int x, int y) {vis[x][y] = 1; // 当前格子标记成1sum++;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,就是这次的连通块的格子数量}}}cout << cnt;return 0;}```6 行未改动int max1, max2;dfs() {本次搜索的最大值}宝藏 > maxn {maxn = 宝藏;记录这次的i, j}*/```## T5```Cpp/*题意:1是雷,0不是雷。数字:一个不是雷的格子,周围有几个雷,上面的数字就是几空格:数字是0的,是空格空:空格的八方向连通块。空的数量 + 周围没有空格的数字数量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]++;}}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,周围的八个位置,不是雷的格子,值+1for (int i=1; i<=n; i++) {for (int j=1; j<=m; j++) {if (a[i][j] == 1) { // 自己是雷mp[i][j] = -1; // 标记自己// 让周围八个格子不是雷的,数字+1}}}/*1. 统计mp数组上周围没有0的数字数量2. 空格连通块的数量。。。 mp数组上统计0的八连通块的数量。*/return 0;}```26 行未改动2728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899022 行未改动11311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816914 行未改动1841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222236 行未改动23023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531610 行未改动32732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136226 行未改动/*染色&洪水填充连通性的问题主函数里的逻辑:枚举遍历二维数组,遇到连通块上的点 1~9,计数洪水填充:1. 0行0列~n+1行m+1列看作是矩阵2. 对0,0跑染色易错点: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}};void dfs(int x, int y) {vis[x][y] = 1; // 当前格子标记成1for (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. 0行0列~n+1行m+1列看作是矩阵2. 对0,0跑染色易错点: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] == 0return 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}};void dfs(int x, int y) {vis[x][y] = 1; // 当前格子标记成1sum++;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,就是这次的连通块的格子数量}}}cout << cnt;return 0;}```6 行未改动int max1, max2;dfs() {本次搜索的最大值}宝藏 > maxn {maxn = 宝藏;记录这次的i, j}*/```代码:```cpp#include<bits/stdc++.h>#define int long longusing namespace std;int n, m;const int N = 3e3 + 5;int a[N][N];int v[N][N];int d[4][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};int xx, yy, maxn;int res;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){continue;}if(a[nx][ny] >= a[x][y]){continue;}dfs(nx, ny, sum + 1);}}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 >> a[i][j];}}for(int i = 1;i <= n;i++){for(int j = 1;j <= m;j++){maxn = 0;dfs(i, j, 1);if (maxn > res) {res = maxn;xx = i;yy = j;}}}cout << xx<< ' '<< yy<< '\n'<< res;return 0;}```## T5```cpp/*题意:1是雷,0不是雷。数字:一个不是雷的格子,周围有几个雷,上面的数字就是几空格:数字是0的,是空格空:空格的八方向连通块。空的数量 + 周围没有空格的数字数量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]++;}}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,周围的八个位置,不是雷的格子,值+1for (int i=1; i<=n; i++) {for (int j=1; j<=m; j++) {if (a[i][j] == 1) { // 自己是雷mp[i][j] = -1; // 标记自己// 让周围八个格子不是雷的,数字+1}}}/*1. 统计mp数组上周围没有0的数字数量2. 空格连通块的数量。。。 mp数组上统计0的八连通块的数量。*/return 0;}```