[AOJ] BOARDCOVER
[ALGOSPOT Online Judge(AOJ)] BOARDCOVER C++
๋ฌธ์ : AOJ BOARDCOVER
๋ฌธ์ ์ค๋ช
ํ
์คํธ ์ผ์ด์ค๋ง๋ค H*W ํฌ๊ธฐ์ ๊ฒ์ํ์ด ์ฃผ์ด์ง๋๋ค.
๊ฒ์ํ์ ๊ฐ ์๋ผ๋๋ ๊ฒ์ ์นธ์ด๋ ํฐ ์นธ์ผ๋ก ๋์ด์๋๋ฐ, L์ ๋ชจ์์ ๋ธ๋ก์ผ๋ก ํฐ ์นธ์ด ์ ๋จ๋๋ก ์ฑ์์ผ ํฉ๋๋ค.
L์ ๋ชจ์์ ๋ธ๋ก์ ์์ ๋กญ๊ฒ ํ์ ํ ์ ์์ง๋ง, ๋ธ๋ก๋ผ๋ฆฌ ๊ฒน์น๊ฑฐ๋ ๊ฒ์ ์นธ์ ๋ฎ์ผ๋ฉด ์ ๋ฉ๋๋ค.
์ด๋ L์ ๋ชจ์์ ๋ธ๋ก์ผ๋ก ๋ชจ๋ ํฐ ์นธ์ ์ฑ์ธ ์ ์๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๊ตฌํ๋ ๋ฌธ์ ์
๋๋ค.
Solution
์์ ํ์
๊ฒ์ํ์ ๊ฐ์ฅ ์์ชฝ, ์ผ์ชฝ์ ํ๋ถํฐ ์์ํด์ ์ค๋ฅธ์ชฝ, ์๋์ชฝ์ผ๋ก ๋ด๋ ค๊ฐ๋ฉฐ ์์ ํ์์ผ๋ก ํด๊ฒฐํด์ผ ํฉ๋๋ค.
๊ฒ์ํ์ ํ ์นธ์ ๊ธฐ์ค ์ผ์ ์ํํ๋ฉด์ L์ ๋ชจ์์ ๋ธ๋ก์ด ๊ทธ ์นธ์ ๊ธฐ์ค์ผ๋ก ํ์ ํ ์ ์๋ ๋ค ๊ฐ์ง ๊ฒฝ์ฐ๋ฅผ ๋ฃ์ด์ฃผ๋ฉฐ ๊ฒฝ์ฐ์ ์๋ฅผ ๊ณ์ฐํฉ๋๋ค.
H, W๊ฐ ์ต๋ 20์ด๋ฏ๋ก 400์นธ์ด ๋์ค๊ณ ์ด๋ฅผ 3์ผ๋ก ๋๋๋ฉด ๊ฒฝ์ฐ์ ์๊ฐ ํฌ๊ฒ ๋๊ปด์ง์ง๋ง, ํ๋์ L์ ๋ธ๋ก์ ๋ฃ์ผ๋ฉด ๋จ์ํ ์นธ ์ ๋ณด๋ค ๋ฃ์ ์ ์๋ ๊ฒฝ์ฐ์ ์๊ฐ ๋ ์ค์ด๋ค๋ฏ๋ก ๋ ์ ์ ๊ฒฝ์ฐ์ ์๋ก ํด๊ฒฐํ ์ ์์ต๋๋ค.
Description
nextMove
๋ฐฐ์ด์ ์ด์ฉํ์ฌ L์ ๋ธ๋ก์ด ํ์ ํ ์ ์๋ ๋ค ๊ฐ์ง ๊ฒฝ์ฐ๋ฅผ ๊ณ์ฐํ์ต๋๋ค.vector<vector<int>>& map
๋ก ๋ฐฐ์ด์ ํจ์์ ์ง์ ๋ฃ์ด์คฌ์ต๋๋ค.isOk
์value
๊ฐ 1์ธ ๊ฒฝ์ฐ๋ L์ ๋ธ๋ก์ ์ถ๊ฐํ๋ ๊ฒฝ์ฐ, -1์ธ ๊ฒฝ์ฐ๋ ๋ธ๋ก์ ์ญ์ ํ๋ ๊ฒฝ์ฐ์ ๋๋ค.
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> map;
int nextMove[4][3][2] = { { {0,0},{1,0},{1,1} },{ {0,0},{1,0},{1,-1} },{ {0,0},{0,1},{1,1} },{ {0,0},{0,1},{1,0} } };
bool isOk(vector<vector<int>>& map, int y, int x, int type ,int value );
int chkMap(vector<vector<int>>& map);
int c, h, w;
int main(){
cin >> c;
while(c--){
map.clear();
cin >> h >> w;
map.resize(h+1);
for(int i=1;i<=h;i++){
map[i].push_back(0);
for(int j=1; j<=w;j++){
char nowLot;
cin >> nowLot;
if(nowLot=='#') map[i].push_back(1);
else map[i].push_back(0);
}
}
int result = chkMap(map);
cout << result << '\n';
}
return 0;
}
bool isOk(vector<vector<int>>& map, int y, int x, int type ,int value ){
bool isOk = true;
for(int i=0;i<3;i++){
int ny = y + nextMove[type][i][0];
int nx = x + nextMove[type][i][1];
if(ny<1||nx<1||ny>h||nx>w) isOk = false;
else if((map[ny][nx]+=value) > 1) isOk = false;
}
return isOk;
}
int chkMap(vector<vector<int>>& map){
int y = -1;
int x = -1;
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
if(map[i][j]==0){
y=i;
x=j;
break;
}
}
if(y!=-1) break;
}
if(y ==-1) return 1;
int ret = 0;
for(int i=0;i<4;i++){
if(isOk(map, y,x,i,1)) ret += chkMap(map);
isOk(map,y,x,i,-1);
}
return ret;
}