1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cassert>
using namespace std;
 
 
const int Typecover[4][3][2= {
  { { 00 }, { 10 }, { 01 } },
  { { 00 }, { 01 }, { 11 } },
  { { 00 }, { 10 }, { 11 } },
  { { 00 }, { 10 }, { 1-1 } } 
};
 
bool Cancover(vector<vector<int> > &board,int y,int x, int type,int delta) {
    
    bool Check = true;
    for (int i = 0; i < 3; i++) {
        const int dy = y + Typecover[type][i][0];
        const int dx = x + Typecover[type][i][1];
        if (dy < 0 || dy >= board.size() || dx < 0 || dx >= board[0].size()) 
            Check= false;
        //연산자 괄호 우선순위 조심 
        else if ((board[dy][dx] += delta) > 1)
            Check = false;        
    }
    return Check;
}
//FindCover 모든 빈칸을 덮는 방법수 반환 함수
//board[i][j]=1 이미 덮인칸 혹은 검은 칸
//board[i][j]=0 아직 덮이지 않은칸
int FindCover(vector<vector<int> > &board) {
    int y = -1, x = -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;
            }
        }
        if (y != -1break;
    }
    //모두 덮여있으면 1을 반환
    if (y == -1return 1;
 
    int ret = 0;
 
    for (int type = 0; type < 4; type++) {
        //만약 board[y][x]를 type형태로 덮을 수 있으면 다음 타일로 진행
        if (Cancover(board, y, x, type, 1))
            ret += FindCover(board);
        //덮어서 실패했던 경우 포함 덮었던 블록 치워준다.
        Cancover(board, y, x, type, -1);
    }
    return ret;
}
 
 
int main()
{
    int cases;
    cin >> cases;
    assert(cases <= 50);
    while (cases--) {
        int H, W;
        cin >> H >> W;
        assert(H <= 20 && W <= 20);    
        vector<vector<int> > board(H, vector<int>(W, 0));
        int whites = 0;
        for (int i = 0; i < H; i++) {
                string s;
                cin >> s;
                for (int k = 0; k < W; k++) {
                    if (s[k] == '#')
                        board[i][k] = 1;
                }
                whites += count(board[i].begin(), board[i].end(), 0);
        }
        assert(whites <= 50);
        int ways = FindCover(board);
        cout << ways << endl;
    }
    return 0;
}
 
cs

문제 출처:https://algospot.com/judge/problem/read/BOARDCOVER


Typecover를 잘못 입력해서 한참 헤맸네요.

25번째 줄 ((board[dy][dx] += delta) > 1) 여기도 괄호로 연산자 우선순위를 고려하지 않으면 실행이 중간에 진행이 되지 않았습니다.

주의해야겠어요 ㅠㅠ

+ Recent posts