반응형
https://algospot.com/judge/problem/read/BOARDCOVER
쉬운 문제이다.
얼핏보면 어려워 보이지만
정답은 Full Scan 방식으로 겁나 쉬운 문제
특이점은 값 범위를 초과하지 않도록, 그뭐냐 board를 그릴떄 #으로 한번 더 감싸주었다.
이론은 알고있었지만 적용해본건 처음이엇는데
생각보다 엄청 헷갈려서 결국 손코딩 후 재작업 ㅠㅠㅠㅠㅠㅠ
PICNIC과 같이 재귀함수를 이용하면 된다.
package algoSpot.BOARDCOVER;
import java.util.Arrays;
/**
* Created by eunbi on 2018-02-06.
*/
public class BOARDCOVER_EB {
public static StringBuilder sb = new StringBuilder();
public static java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
public static java.util.Scanner sc = new java.util.Scanner(System.in);
public static int C;
public static int H;
public static int W;
public static int count =0;
public static String[][] board;
public static void main(String[] args)throws Exception {
C=Integer.parseInt(br.readLine().trim());
for(int i=0;i<C;i++) {
solve();
}
System.out.println(sb.toString());
}
public static void solve()throws Exception {
// H=8;
// W=10;
String lines = br.readLine();
String[] spl = lines.split(" ");
H=Integer.parseInt(spl[0]);
W=Integer.parseInt(spl[1]);
/* String input = "########## \n" +
"#........# \n" +
"#........# \n" +
"#........# \n" +
"#........# \n" +
"#........# \n" +
"#........# \n" +
"########## ";
String[] lines = input.split("\n");*/
initBoard();
/**
* Data Setting
*/
for(int i=0;i<H;i++){
String input = br.readLine();
int j=1;
for(char c : input.toCharArray()){
board[i+1][j] = String.valueOf(c);
j++;
}
}
boardCover();
sb.append(count).append("\n");
}
public static void boardCover(){
/**
* Find first point '.'
*/
int x=1;
int y=1;
for(x=1;x<H+1;x++){
for(y = 1; y < W+1; y++) {
if (board[x][y].equals(".")) {
break;
}
}
if (y<W+1&&board[x][y].equals(".")) {
break;
}
}
//기저
if(x == H+1 && y == W+1){
count++;
return ;
}
// *
// @ @
if(board[x+1][y].equals(".")&&board[x+1][y+1].equals(".")){
board[x][y] = board[x+1][y] =board[x+1][y+1] = "*";
boardCover();
board[x][y] = board[x+1][y] =board[x+1][y+1] = ".";
}
// *
//@@
if(board[x+1][y-1].equals(".")&&board[x+1][y].equals(".")){
board[x][y] = board[x+1][y-1] =board[x+1][y] = "*";
boardCover();
board[x][y] = board[x+1][y-1] =board[x+1][y] = ".";
}
// *@
// @
if(board[x][y+1].equals(".")&&board[x+1][y+1].equals(".")){
board[x][y] = board[x][y+1] =board[x+1][y+1] = "*";
boardCover();
board[x][y] = board[x][y+1] =board[x+1][y+1] = ".";
}
// *@
// @
if(board[x+1][y].equals(".")&&board[x][y+1].equals(".")){
board[x][y] = board[x+1][y] =board[x][y+1] = "#";
boardCover();
board[x][y] = board[x+1][y] =board[x][y+1] = ".";
}
}
public static String printBoard(){
StringBuilder sb = new StringBuilder();
sb.append("==========================\n");
for(int x=0;x<H+2;x++){
for(int y = 0; y < W+2; y++) {
sb.append(board[x][y]+" ");
}
sb.append("\n");
}
sb.append("==========================\n");
System.out.println(sb.toString());
return sb.toString();
}
public static void initBoard(){
//3 , 7
board = new String[H+2][W+2];
for(int i=1;i<=H;i++){
board[i][0] = board[i][W+1] = "#";
}
for(int i=0;i<W+2;i++){
board[0][i] = board[H+1][i] = "#";
}
count=0;
}
}
반응형
'Develope > 알고리즘' 카테고리의 다른 글
[백준 14891] 톱니바퀴 - java (0) | 2018.02.12 |
---|---|
[알고스팟] PICNIC - java (4) | 2018.02.06 |
[백준 - 1158] 조세퍼스 문제 (0) | 2018.01.04 |
[백준 - 2140] 지뢰찾기 (0) | 2017.12.19 |
[알고스팟] XHAENEUNG (5) | 2017.12.11 |