티스토리 뷰

[BJ] 17070.파이프 옮기기1

www.acmicpc.net/problem/17070

 

17070번: 파이프 옮기기 1

유현이가 새 집으로 이사했다. 새 집의 크기는 N×N의 격자판으로 나타낼 수 있고, 1×1크기의 정사각형 칸으로 나누어져 있다. 각각의 칸은 (r, c)로 나타낼 수 있다. 여기서 r은 행의 번호, c는 열의

www.acmicpc.net

 

import java.io.*;
import java.util.*;
// 0306

public class Main_BJ_17070_파이프옮기기1 {
	static int N, cnt=0;
	static int[][] map;
	static int[] dr = {0, 1, 1}; // 가로, 세로, 대각선 순
	static int[] dc = {1, 0, 1};
	
	static int stoi(String str) {
		return Integer.parseInt(str);
	}
	
	static void move(int[] ip1, int[] ip2) {
		
		if(ip2[0]==N && ip2[1]==N) {
			++cnt;
			return;
		}
		
		for(int d=0; d<3; d++) {
			int nr = ip2[0] + dr[d];
			int nc = ip2[1] + dc[d];
			
			// 범위 체크
			if (nr<=0 || nr>N || nc<=0 || nc>N || map[nr][nc]==1) continue;
			
			if (ip1[0]==ip2[0] && d==1) continue; // 가로로 놓인 경우 세로 방향 이동 불가
			if (ip1[1]==ip2[1] && d==0) continue; // 세로로 놓인 경우 가로 방향 이동 불가
	
			// 대각선 방향으로 갈 경우 우, 좌 빈칸 여부도 확인
			if (d==2 && (map[nr-1][nc]==1 || map[nr][nc-1]==1)) continue;
			
			// 이동 가능
			move(ip2, new int[] {nr, nc});
			
		}
	}
	
	public static void main(String[] args) throws Exception {
		//System.setIn(new FileInputStream("res/input_BJ_17070_파이프옮기기1.txt"));
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		
		N = stoi(br.readLine());
		map = new int[N+1][N+1]; // 1행 1열부터 시작
		
		for(int i=1; i<=N; i++) {
			st = new StringTokenizer(br.readLine(), " ");
			for(int j=1; j<=N; j++) {
				map[i][j] = stoi(st.nextToken());
			}
		}
	
		move(new int[] {1,1},new int[] {1,2});
		
		System.out.println(cnt);
		
		br.close();
	} // main
}

'코딩테스트 > 백준' 카테고리의 다른 글

[BJ] 1463. 1로 만들기  (0) 2021.03.23
[BJ] 2206. 벽 부수고 이동하기  (0) 2021.03.17
[BJ] 3109. 빵집  (0) 2021.03.06
[BJ] 17406. 배열 돌리기 4  (0) 2021.03.05
[BJ] 17135: 캐슬 디펜스  (0) 2021.03.04
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/09   »
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
글 보관함