1 条题解
-
0
三维数组
#include <bits/stdc++.h> using namespace std; int n, m; // [天数][行][列] char g[105][105][105]; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> g[1][i][j]; cin >> m; for (int day = 2; day <= m; day++) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { if (g[day - 1][i][j] == '#') g[day][i][j] = '#'; else if (g[day - 1][i - 1][j] == '@' || g[day - 1][i][j - 1] == '@' || g[day - 1][i][j] == '@' || g[day - 1][i][j + 1] == '@' || g[day - 1][i + 1][j] == '@') g[day][i][j] = '@'; } } int cnt = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (g[m][i][j] == '@') cnt++; cout << cnt << "\n"; return 0; }
两个二维数组滚动
#include <bits/stdc++.h> using namespace std; int n, m; // [天数][行][列] char a[105][105]; char b[105][105]; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> a[i][j]; cin >> m; for (int day = 2; day <= m; day++) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { if (a[i][j] == '#') b[i][j] = '#'; else if (a[i - 1][j] == '@' || a[i][j - 1] == '@' || a[i][j] == '@' || a[i][j + 1] == '@' || a[i + 1][j] == '@') b[i][j] = '@'; } for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) a[i][j] = b[i][j]; } int cnt = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (a[i][j] == '@') cnt++; cout << cnt << "\n"; return 0; }
另一种滚动形式
#include <bits/stdc++.h> using namespace std; int n, m; // [天数][行][列] char a[2][105][105]; int now,last;//当前天、前一天 int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; now = 1; last = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> a[now][i][j]; cin >> m; last = now; now = 1-last; for (int day = 2; day <= m; day++) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { if (a[last][i][j] == '#') a[now][i][j] = '#'; else if (a[last][i - 1][j] == '@' || a[last][i][j - 1] == '@' || a[last][i][j] == '@' || a[last][i][j + 1] == '@' || a[last][i + 1][j] == '@') a[now][i][j] = '@'; } last = now; now = 1-last; } int cnt = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (a[last][i][j] == '@') cnt++; cout << cnt << "\n"; return 0; }
二维数组直接记录每个位置哪天被传染
#include <bits/stdc++.h> using namespace std; int n; // g[i][j] 表示 第 day 天,(i,j) 的状态 // -1: 墙 // 0: 健康人 // x: 第 x 天被传染的人 int g[105][105]; int m; int main() { cin >> n; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { char c; cin >> c; if (c == '#') g[i][j] = -1; else if (c == '.') g[i][j] = 0; else if (c == '@') g[i][j] = 1; } cin >> m; for (int day = 2; day <= m; day++) { // 把 g 数组变成下一天的情况 for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (g[i][j] == 0) { if (g[i - 1][j] == day - 1 || g[i + 1][j] == day - 1 || g[i][j - 1] == day - 1 || g[i][j + 1] == day - 1) { // g[i][j] 周边有昨天被传染的,今天就被传染了 g[i][j] = day; } } } } } int ans = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (g[i][j] > 0) ans++; cout << ans; return 0; }
- 1
信息
- ID
- 411
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 7
- 标签
- (无)
- 递交数
- 97
- 已通过
- 23
- 上传者