扫雷计算概率
二、代码
#include<iostream> #include<cstdio> using namespace std; int main() { int m, n; cin >> m >> n; int shuzi; int biaoji_x, biaoji_y; char qipan[10][10] = { 'm' }; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { cin >> qipan[i][j]; } } int num[10][10] = { 0 };//记录数字周围所有未点开的格子数 int a[8] = { 1,1,0,-1,-1,-1,0,1 }; int b[8] = { 0,1,1,1,0,-1,-1,-1 }; for(int i=0;i<m;i++) for (int j = 0; j < n; j++) { if (qipan[i][j]-'0'>-1 && qipan[i][j]-'0'<10) { shuzi = qipan[i][j] - '0'; biaoji_x = i; biaoji_y = j; } } for (int k = 0; k < 9; k++) { int weizhi_x = biaoji_x + a[k]; int weizhi_y = biaoji_y + b[k]; if (weizhi_x >= 0 && weizhi_y >= 0) { if (qipan[weizhi_x][weizhi_y] == '*') { num[biaoji_x][biaoji_y]++; } } } cout << shuzi/ num[biaoji_x][biaoji_y]++; }