Mines
Limits 1s, 512 MB
The game of mines is a tricky one. You start with a grid and you click your way through the cells to find out what’s under them. If you click on a cell with a mine under it, it’s game over! If you click on a cell without a mine under it, you get to continue. Every time you uncover an empty cell, the cell may show a number indicating the number of mines that are around it in the 8 adjacent cells.
In this problem, you will be given a grid with the locations of all of the mines. You will then have to print the same grid with the appropriate numbers in the empty cells.
If a cell does not have any mine in its adjacent cells, you will leave the cell unchanged.
Input
The input will start with two integers, R and C (0 < R, C ≤ 10). The following R lines will each contain C characters representing the contents of the corresponding cells.
An asterisk (*) is used to indicate a mine and a dot (.) is used indicate an empty cell.
Output
Print the same grid, but with the appropriate numbers in the cells that has mines adjacent to them.
Sample
Input | Output |
---|---|
3 9 .**..*... ....*..** ..*...... | 1**22*222 1333*22** .1*211122 |
Solution:
#include <bits/stdc++.h> using namespace std; using ll = long long; char s[12][12]; int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}; int dy[] = {0, 1, 0, -1, 1, -1, 1, -1}; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int r, c,i,j,cnt=0,k; scanf("%d %d", &r, &c); for( i = 1; i <= r; i++) scanf(" %s", s[i] + 1); for( i = 1; i <= r; i++) { for( j = 1; j <= c; j++) { if(s[i][j] != '*') { cnt = 0; for( k = 0; k < 8; k++) { int tx = i + dx[k]; int ty = j + dy[k]; if(s[tx][ty] == '*') cnt++; } if(cnt) s[i][j] = (char)('0' + cnt); else s[i][j] = '.'; } } } for( i = 1; i <= r; i++) { for( j = 1; j <= c; j++) printf("%c", s[i][j]); puts(""); } return 0; }
0 Comments