fork(2) download
  1. #include<iostream>
  2. #include<string>
  3.  
  4. using namespace std;
  5.  
  6. int inside(int x, int y, int row, int col){
  7. if(x > row - 1 || x < 0 || y > col - 1 || y < 0)return 0;
  8. else return 1;
  9. }
  10.  
  11. int main(){
  12. int row, col;
  13. int instance = 1;
  14. int row_set [] = {1, 1, 1, -1, -1, -1, 0, 0};
  15. int col_set [] = {1, -1, 0, 1, -1, 0, 1, -1};
  16. while(cin>>row){
  17. cin>>col;
  18. if(row == 0 || col == 0){
  19. cout<<endl;
  20. continue;
  21. }
  22. char ch[row][col];
  23. char sol[row][col];
  24. char temp;
  25. for(int i = 0; i < row; i++){
  26. for(int j = 0; j < col; j++){
  27. cin>>temp;
  28. if(temp == '\n')j--;
  29. ch[i][j] = temp;
  30. }
  31. }
  32. for(int i = 0; i < row; i++){
  33. for(int j = 0; j < col; j++){
  34. if(ch[i][j] == '*')sol[i][j] = '*';
  35. else{
  36. char mines = '0';
  37. for(int k = 0; k < 8; k++){
  38. if(inside(i + row_set[k], j + col_set[k], row, col) && ch[i + row_set[k]][j + col_set[k]] == '*')mines += 1;
  39. }
  40. sol[i][j] = (char)mines;
  41. }
  42. }
  43. }
  44. cout<<endl;
  45. cout<<"Field #"<<instance<<":"<<endl;
  46. instance++;
  47. for(int i = 0; i < row; i++){
  48. for(int j = 0; j < col; j++){
  49. cout<<sol[i][j];
  50. }
  51. cout<<endl;
  52. }
  53. cout<<endl;
  54. }
  55. return 0;
  56. }
  57.  
Success #stdin #stdout 0s 3100KB
stdin
Standard input is empty
stdout
Standard output is empty