# 踩地雷 https://neoj.sprout.tw/problem/214/ 給一個地雷盤面,問每格周圍八格有多少個地雷 ### 輸入格式 第1行有兩個正整數n和m(1≤n,m≤100),代表著盤面的長和寬 第2行到第n+1行,每行會有m個數字,0表示沒有地雷,1表示有地雷。 ### 輸出格式 請輸出n行,每行會有m個數字,代表該格周圍八格有多少地雷 ### 範例輸入 ``` 3 3 0 0 1 1 0 1 1 0 1 ``` ### 範例輸出 ``` 1 3 1 1 5 2 1 4 1 ``` # Code ```cpp #include <iostream> #include <cstring> #include <cmath using namespace std; int in[105][105], out[105][105]; int main(){ int n, m; cin >> n >> m; for (int t = 1; t <= n; t++) for (int k = 1; k <= m; k++) cin >> in[t][k]; for (int t = 1; t <= n; t++) for (int k = 1; k <= m; k++) for (int r = -1; r <= 1; r++) { if (t + r == 0 || t + r == n + 1) continue; for (int s = -1; s <= 1; s++) { if (k + s == 0 || k + s == m + 1) continue; if (r == 0 && s == 0) continue; if (in[t + r][k + s] == 1) out[t][k] += 1; } } for (int t = 1; t <= n; t++) { for (int k = 1; k <= m; k++) { cout << out[t][k]; if (k == m) continue; cout << " "; } cout << "\n"; } } ```