1652 누울 자리를 찾아라

2019. 10. 22. 09:54알고리즘/백준

수학 문제로 분류되어 있지만, 작은 N을 고려했을 때 탐색 문제로 분류 된다

 

1. X인 점에 대해서 오른쪽으로 두 칸 이상 떨어졌는지 확인한다

2. 각 행과 열에 대해서 첫번째 X에 대해서는 왼쪽도 탐색하도록 한다

3. 짐이 없는 행과 열에 대해서도 따로 처리한다

 

문제: https://www.acmicpc.net/problem/1652

깃허브주소: https://github.com/surinoel/boj/blob/master/1652.cpp

 

#include <string>
#include <iostream>
using namespace std;
int mat[100][100];
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
for (int j = 0; j < n; j++) {
if (s[j] == '.') mat[i][j] = 0;
else mat[i][j] = 1;
}
}
int row, col;
row = col = 0;
for (int i = 0; i < n; i++) {
bool ok = false;
for (int j = 0; j < n; j++) {
if (mat[i][j] == 1) {
if (!ok && j - 2 >= 0 && mat[i][j - 2] == 0 && mat[i][j - 1] == 0) {
row += 1;
}
if (j + 2 < n && mat[i][j + 1] == 0 && mat[i][j + 2] == 0) {
row += 1;
}
ok = true;
}
}
if (!ok) row += 1;
}
for (int j = 0; j < n; j++) {
bool ok = false;
for (int i = 0; i < n; i++) {
if (mat[i][j] == 1) {
if (!ok && i - 2 >= 0 && mat[i - 2][j] == 0 && mat[i - 1][j] == 0) {
col += 1;
}
if (i + 2 < n && mat[i + 1][j] == 0 && mat[i + 2][j] == 0) {
col += 1;
}
ok = true;
}
}
if (!ok) col += 1;
}
if (n == 1) row = col = 0;
cout << row << ' ' << col << '\n';
return 0;
}
view raw 1652.cpp hosted with ❤ by GitHub

'알고리즘 > 백준' 카테고리의 다른 글

[삼성] 17779 게리멘더링 2  (0) 2019.10.24
16933 벽 부수고 이동하기 3  (0) 2019.10.22
16496 큰 수 만들기  (0) 2019.10.21
2458 키 순서  (0) 2019.10.19
2010 플러그  (0) 2019.10.18