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



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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <iostream>
#include <vector>
#include <string>
 
class Board
{
public:
    const int COVER_TYPE[4][3][2= {
        { { 00 },{ 10 },{ 01 } },    //type1
        { { 00 },{ 01 },{ 11 } },    //type2
        { { 00 },{ 10 },{ 11 } },    //type3
        { { 00 },{ 10 },{ 1-1 } }    //type4
    };
 
    int mnHeight;
    int mnWidth;
    int mnAvailableCount;
    int mnZeroCount;
    std::vector<std::vector<int>> mBoardVector;
    //std::vector<std::string> mBoadrVector;
 
    void CountAvailableSet();
 
private:
    bool _Set(std::vector<std::vector<int>>& parBoard, int parY, int parX, int parType, int parDelta);
    int _Cover(std::vector<std::vector<int>>& parBoard);
 
 
};
 
 
int main()
{
    int nTestCase;
    std::vector<Board> boardList;
 
    boardList.clear();
    std::cin >> nTestCase;
    getchar();
    if (nTestCase > 30)
        return -1;
 
    for (int i = 0; i < nTestCase; ++i)
    {
        Board tmpBoard;
        int nCount = 0;
 
        tmpBoard.mBoardVector.clear();
        std::cin >> tmpBoard.mnHeight >> tmpBoard.mnWidth;
        getchar();
 
        if (tmpBoard.mnHeight < 1 || tmpBoard.mnWidth > 20)
            return -1;
 
        for (int j = 0; j < tmpBoard.mnHeight; ++j)
        {
            std::vector<int> tmpBoardList;
            std::string strTmpBoardLine;
 
            tmpBoardList.clear();
            std::getline(std::cin, strTmpBoardLine);
 
            for (int k = 0; k < strTmpBoardLine.size(); ++k)
            {
                if (strTmpBoardLine[k] == '#')
                {
                    tmpBoardList.push_back(1);
                }
                else if (strTmpBoardLine[k] == '.')
                {
                    tmpBoardList.push_back(0);
                    nCount++;
                }
            
            }
            tmpBoard.mBoardVector.push_back(tmpBoardList);
            tmpBoard.mnZeroCount = nCount;
            if (nCount > 50)
                return -1;
        }
 
        boardList.push_back(tmpBoard);
    }
    
    for (auto& board : boardList)
    {
        board.CountAvailableSet();
        std::cout << board.mnAvailableCount << std::endl;
    }
 
 
    return 0;
}
 
void Board::CountAvailableSet()
{
    
    if ((mnZeroCount % 3!= 0)    //3의 배수가 아닌 빈칸은 의미없다.
    {
        mnAvailableCount = 0;
    }
    else
    {
        mnAvailableCount = _Cover(mBoardVector);
    }
 
}
 
bool Board::_Set(std::vector<std::vector<int>>& parBoard, int parY, int parX, int parType, int parDelta)
{
    bool isOk = true;
    for (int i = 0; i < 3++i)
    {
        //각 타입별 3개의 좌표 입력
        const int nY = parY + COVER_TYPE[parType][i][0];
        const int nX = parX + COVER_TYPE[parType][i][1];
 
 
        if (nY < 0 || nY >= mnHeight || nX < 0 || nX >= mnWidth)    //각 좌표의 범위체크
        {
            isOk = false;
        }
        else if ((parBoard[nY][nX] += parDelta) > 1)    // 각 좌표를 채우고
        {                                                // 검은칸 또는 이미 채워진 곳인지 확인
            isOk = false;
        }
    }
    return isOk;
}
 
 
//parBoard의 모든 빈 칸을 덮을 수 있는 방법의 수를 반환한다.
//parBoard[i][j] = 1 이면 이미 덮인 칸 혹은 검은 칸
//parBoard[i][j] = 0 이면 아직 덮이지 않은 칸
int Board::_Cover(std::vector<std::vector<int>>& parBoard)
{
    //아직 채우지 못한 칸 중 가장 윗줄 왼쪽에 있는 칸을 찾는다.
    int nY = -1;
    int nX = -1;
    for (int i = 0; i < parBoard.size(); ++i)
    {
        for (int j = 0; j < parBoard[i].size(); ++j)
        {
            if (parBoard[i][j] == 0)
            {
                nY = i;
                nX = j;
                break;
            }
        }
        if (nY != -1break;
    }
 
    //기저 사례 : 모든 칸을 채웠으면 1을 반환한다.
    if (nY == -1return 1;
    int ret = 0;
 
    for (int type = 0; type < 4++type)
    {
        //만약 parBoard[y][x]를 type형태로 덮을 수 있으면 재귀 호출한다.
        if(_Set(parBoard, nY, nX, type, 1))
        {
            ret += _Cover(parBoard);
        }
        //덮었던 블록을 치운다.
        _Set(parBoard, nY, nX, type, -1);
    }
    return ret;
}
 
cs


+ Recent posts