求助

P1789 【Mc生存】插火把

第一种可能是数组越界了。 第二种可能是需要至少定义$104 \times 104$的数组,但是在遍历时遍历中间$n \times n$的区域,即左上角$(2,2)$到右下角$(n+1,n+1)$,上下左右各空两行或两列,防止数组越界,但是你最后遍历的是$2$到$n$。
by Ream4923 @ 2024-03-30 02:50:59


``` #include <bits/stdc++.h> using namespace std; int n, m, k, x, y, o, p, ans = 0,t,i,j; int main() { int a[105][105]; memset(a,0,sizeof(a)); cin >> n >> m >> k; for (i = 0; i < m; i++) { cin >> x >> y; for (j = x - 1; j <= x + 1; j++) { for (t = y - 1; t <= y + 1; t++) { a[j][t] = 1; } } a[x][y - 2] = 1; a[x][y + 2] = 1; a[x - 2][y] = 1; a[x + 2][y] = 1; } for (i = 0; i < k; i++) { cin >> o >> p; for (j = o - 2; j <= o + 2; j++) { for (t = p - 2; t <= p + 2; t++) { a[j][t] = 1; } } } for (int j = 1; j <= n; j++) { for (int t = 1; t <= n; t++) { if (a[j][t] == 0) { ans++; } } } cout << ans; return 0; } ``` 保证数组开大的同时注意代码的严谨性,输入o,p就使用o,p进行寻址。注意变量之间的相互使用
by elong123 @ 2024-03-31 21:42:16


感谢各位大佬
by FTHX @ 2024-04-01 17:17:44


|