# Minesweeper(UVA10189) ## [程式繳交區](https://hackmd.io/@Renektonn/rkui5kpOye/edit) ## 題目 [點我](https://onlinejudge.org/external/101/10189.pdf) ## 解題網站 [UVA](https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1130) [ZJ](https://zerojudge.tw/ShowProblem?problemid=e605) ## 程式碼 ```cpp= #include <bits/stdc++.h> using namespace std; int dy[] = {1 , 1 , 0 , -1 , -1 , -1 , 0 , 1}; int dx[] = {0 , 1 , 1 , 1 , 0 , -1 , -1 , -1}; int m , n; bool isValid(int i , int j){ return 1 <= i && i <= m && 1 <= j && j <= n; } void countMines(int arr[][101] , int i , int j){ for(int k = 0 ; k < 8 ; k++){ if(isValid(i , j) && arr[i + dy[k]][j + dx[k]] != -1){ arr[i + dy[k]][j + dx[k]]++; } } } int main() { int cnt = 1; while(1){ cin >> m >> n; if(m == 0 && n == 0) break; if(cnt != 1) cout << endl; cout << "Field #" << cnt++ << ":" << endl; int arr[101][101] = {}; vector <pair<int , int>> v; for(int i = 1 ; i <= m ; i++){ for(int j = 1 ; j <= n ; j++){ char tmp; cin >> tmp; if(tmp == '*'){ v.push_back(make_pair(i , j)); arr[i][j] = -1; } } } for(int i = 0 ; i < v.size() ; i++){ countMines(arr , v[i].first , v[i].second); } for(int i = 1 ; i <= m ; i++){ for(int j = 1 ; j <= n ; j++){ if(arr[i][j] == -1){ cout << "*"; } else{ cout << arr[i][j]; } } cout << endl; } } return 0; } ```