https://algospot.com/search/?q=BOARDCOVER
교재 코드에서 x, y 좌표로 되어있는게 헷갈려서
행, 열 순서대로 나오도록 코드 고쳤는데 이상하게 답이 안 나옴.
세시간 째 보는데 아무리봐도 못 잡겠다.
#include <iostream>
#include <vector>
using namespace std;
enum MODE{
COVER = 1,
REMOVE = -1,
};
const int block[4][3][2]= {
{ {0, 0}, {0, 1}, {1, 0} },
{ {0, 0}, {1, 0}, {1, 1} },
{ {0, 0}, {0, 1}, {1, 1} },
{ {0, 0}, {0, 1}, {-1, 1} },
};
/*for debuging */
void printBoard(const vector<vector <int> > &board){
const int row = board.size();
const int col = board[0].size();
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; j++) {
cout << board[i][j];
}
cout << endl;
}
cout << endl;
}
bool inRange(const vector<vector <int> > &board, const int r, const int c){
const int rows = board.size();
const int cols = board[0].size();
if(r < 0 || c < 0) return false;
if(r >= rows || c >= cols){
return false;
}
return true;
}
bool set(vector<vector<int> > &board, const int type, const int row, const int col, const int mode){
bool successToCover = true;
for (int i = 0; i < 3; ++i) {
const int nrow = row + block[type][i][0];
const int ncol = col + block[type][i][1];
if(!inRange(board, nrow, ncol)){
successToCover = false;
}
else if((board[nrow][ncol] += mode) > 1){
successToCover = false;
}
}
return successToCover;
}
int cover(vector<vector<int> > &board){
int row = -1, col= -1;
for (int i = 0; i < board.size(); ++i) {
for (int j = 0; j < board[i].size(); j++) {
if(!board[i][j]){
row = i;
col = j;
break;
}
}
if(row != -1){
break;
}
}
if(row == -1){
return 1;
}
int ret= 0;
for(int type=0; type<4; type++){
if(set(board,type, row,col, MODE::COVER)){
ret += cover(board);
}
set(board, type, row, col, MODE::REMOVE);
}
return ret;
}
int main(){
int tc = 0;
cin >> tc;
for (int t = 0; t <tc; ++t) {
int row = 0, col = 0;
cin >> row >> col;
vector<vector <int> > board(row, vector<int>(col, 0));
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; j++) {
char temp = 0;
cin >> temp;
board[i][j] = (temp == '#'); /* 흰 칸이 0 되도록 */
}
}
cout << cover(board) << "
";
}
}
int block[][][] 의 네번째 블록이 이상한 것 같아요 맨 왼쪽위 좌표에서 -1한 곳은 이미 덮여있으니 확인해 봐야할 블록과는 다른 모양인 것 같네요