알고리즘
[C++] 백준 1996번 지뢰 찾기
J3SUNG
2019. 11. 4. 17:09
728x90
숫자가 입력되어 있는곳은 상하좌우 대각선 방면에 지뢰의 합이 숫자의 개수와 같습니다.
숫자가 있는곳의 상하좌우 대각선 방면에 현재 숫자를 더해주어서
최종적으로 9를 넘으면 M으로, 지뢰면 *, 그 외에는 더해진 수를 출력하면 됩니다.
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#define MAX 987654321
using namespace std;
int n;
char input[1010][1010];
int map[1010][1010];
int moveX[8] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int moveY[8] = { -1, 0, 1, -1, 1, -1, 0, 1 };
string s;
int main(int argc, char* argv[])
{
cin >> n;
memset(map, 0, sizeof(map));
for (int i = 1; i <= n; ++i) {
cin >> s;
for (int j = 1; j <= n; ++j) {
input[i][j] = s[j - 1];
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
for (int k = 0; k < 8; ++k) {
int x = i + moveX[k];
int y = j + moveY[k];
if (input[x][y] != '.') {
continue;
}
if (input[i][j] < '0' || input[i][j] > '9') {
continue;
}
map[x][y] += input[i][j] - '0';
}
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (input[i][j] != '.') {
cout << "*";
continue;
}
if (map[i][j] > 9) {
cout << "M";
continue;
}
cout << map[i][j];
}
cout << endl;
}
return 0;
}