2210번: 숫자판 점프

111111, 111112, 111121, 111211, 111212, 112111, 112121, 121111, 121112, 121211, 121212, 211111, 211121, 212111, 212121 이 가능한 경우들이다.

www.acmicpc.net

 

 


 

 

  • 생각

 

모든 경우의 수를 확인해야 되는 문제이다.

 

dfs 방식

1. 파라미터는 현재 index(a, b), 숫자가 몇개인지(depth), 현재 숫자(number) 이다.

2. basecase는 숫자가 몇개인지를 체크해서 6개가 되고 이전에 없던 숫자이면 answer을 ++하고 return해준다.

3. 체크하는 방법은 boolean으로 1000001개를 만들어 index로 체크한다. (hashset으로 했었는데 더 느리다.)

4. 상, 하, 좌, 우를 돌리면서 (이전 숫자*10 + 현재 숫자), 다음 index, depth를 바꿔주면서 dfs를 돌린다.

 

 

 

  • 코드

 

import java.io.IOException;
import java.io.InputStream;
import java.util.InputMismatchException;

public class Main {
	static int answer;
	static int[][] array;
	static boolean[] check;
	static int[] x = { -1, 1, 0, 0 };
	static int[] y = { 0, 0, -1, 1 };

	public static void main(String[] args) throws Exception {
		SetData();
		System.out.println(answer);
	}

	private static void SetData() throws Exception {
		InputReader in = new InputReader(System.in);

		array = new int[5][5];
		check = new boolean[1000001];
		answer = 0;

		for (int i = 0; i < 5; i++) {
			for (int j = 0; j < 5; j++) {
				array[i][j] = in.nextInt();
			}
		}

		for (int i = 0; i < 5; i++) {
			for (int j = 0; j < 5; j++) {
				dfs(i, j, 0, array[i][j]);
			}
		}
	}

	public static void dfs(int a, int b, int depth, int number) {
		// basecase
		if (depth == 5) {
			if (!check[number]) {
				check[number] = true;
				answer++;
			}
			return;
		}
		
		// 6번 까지 모든 경우의 수 이동
		for (int i = 0; i < 4; i++) {
			int r = a + x[i];
			int c = b + y[i];

			if (r < 0 || c < 0 || r >= 5 || c >= 5) 	continue;
			dfs(r, c, depth + 1, number * 10 + array[r][c]);

		}

	}
}

class InputReader {
	private final InputStream stream;
	private final byte[] buf = new byte[8192];
	private int curChar, snumChars;

	public InputReader(InputStream st) {
		this.stream = st;
	}

	public int read() {
		if (snumChars == -1)
			throw new InputMismatchException();
		if (curChar >= snumChars) {
			curChar = 0;
			try {
				snumChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (snumChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}

	public int nextInt() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public long nextLong() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		long res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public int[] nextIntArray(int n) {
		int a[] = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt();
		}
		return a;
	}

	public String nextLine() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isEndOfLine(c));
		return res.toString();
	}

	public boolean isSpaceChar(int c) {
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}

	private boolean isEndOfLine(int c) {
		return c == '\n' || c == '\r' || c == -1;
	}
}

+ Recent posts