[Week 2] BOARDCOVER self review - yujungee
Closed this issue · 0 comments
yujungee commented
BOARDCOVER self review
1. 해결 시도 과정
처음에는 모두 배열로 생각하여 L자 모양에 해당하는 index를 x와 y 좌표에 하나하나 더해서 바꾸려고 했는데 복잡하였다. 따라서 x와 y를 기준으로 덮을 위치를 배열에 저장하여 더하는 방식으로 바꾸었다.
2. 아이디어
책의 상단 부분을 참고하여 L모양이 덮을 좌표를 저장해두었다.
3. 코드 설명
#include <iostream>
#include <vector>
using namespace std;
const int coverType[4][3][2] = {
{{0,0},{1,0},(0,1)},
{{0,0},{0,1},{1,1}},
{{0,0},{1,0},{1,1}},
{{0,0},{1,0},{1,-1}}
};
모양 별로 덮을 좌표를 저장
// 타입은 어떤 모양이냐
bool set(vector<vector<int> >& board, int y, int x, int type) {
bool full = true;
for (int i = 0; i<3; i++) {
int ny = y + coverType[type][i][0];
int nx = x + coverType[type][i][1];
if(ny < 0 || ny >= board.size() || nx < 0 || nx >= board[0].size()) full = false; // 보드를 벗어날 때
}
return full;
}
int cover(vector<vector<int> >& board) {
int y = -1, x = -1; // 0부터 시작이니까 -1로 초기화
for (int i=0; i < board.size(); ++i) {
for (int j=0; j < board[i].size(); ++j)
if(board[i][j] == 0) { // 덮으려는 시작 위치
y = i;
x = j;
break;
}
}
int cnt = 0; // 횟수
for (int type = 0; type <4; ++type)
if (set(board, y, x, type)) cnt += cover(board);
return cnt;
}
메인 부분
int main() {
int c, h, w;
cin >> c;
for (int i = 0; i < c; i++) {
char temp;
int white = 0;
cin >> h >> w;
vector<vector<int> > board(h, vector<int>(w, 0));
for (int k = 0; k < h; k++) {
for (int j = 0; j < w; j++) {
cin >> temp;
if (temp == '#') board[k][j] = 1; // 덮히지 않은 칸은 1로 저장
else white++;
}
}
if (white % 3 != 0) cout << 0;
else cout << cover(board)<<endl;
}
return 0;
}
4. 막힌 점 및 개선 사항
cnt 가 모두 0으로 찍히는데 애초에 set()이 잘못된 것인지 cover()에서 세지지 않은 것인지 구분할 수 없습니다.