본문 바로가기
Algorithm/백준

[백준] 신나는 함수 실행 - java

by jackWillow 2021. 11. 6.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		
		StringTokenizer st = null;
		StringBuilder sb = new StringBuilder();
		String line = null;
		while((line = br.readLine()) != null) {
			if(line.equals("-1 -1 -1")) break;
			
			st = new StringTokenizer(line, " ");
			int a = Integer.parseInt(st.nextToken());
			int b = Integer.parseInt(st.nextToken());
			int c = Integer.parseInt(st.nextToken());
			int ret = w(a, b, c);
			sb.append("w(").append(a).append(", ").append(b).append(", ").append(c).append(") = ").append(ret).append('\n');
		}
		bw.write(sb.toString());
		
		bw.close();
		br.close();
	}

	private static int[][][] save = new int[21][21][21];
	
	private static int w(int a, int b, int c) {
		if(a <= 0 || b <= 0 || c <= 0) return 1;
		
		if(a > 20 || b > 20 || c > 20) return 1048576;
		
		if(save[a][b][c] != 0) return save[a][b][c];
		
		if(a < b && b < c) {
			if(save[a][b][c-1] == 0) {
				save[a][b][c-1] = w(a, b, c-1);
			}
			
			if(save[a][b-1][c-1] == 0) {
				save[a][b-1][c-1] = w(a, b-1, c-1);
			}
			
			if(save[a][b-1][c] == 0) {
				save[a][b-1][c] = w(a, b-1, c);
			}
			
			save[a][b][c] = save[a][b][c-1] + save[a][b-1][c-1] - save[a][b-1][c];
			return save[a][b][c];
		}
		
		if(save[a-1][b][c] == 0) {
			save[a-1][b][c] = w(a-1, b, c);
		}
		
		if(save[a-1][b-1][c] == 0) {
			save[a-1][b-1][c] = w(a-1, b-1, c);
		}

		if(save[a-1][b][c-1] == 0) {
			save[a-1][b][c-1] = w(a-1, b, c-1);
		}

		if(save[a-1][b-1][c-1] == 0) {
			save[a-1][b-1][c-1] = w(a-1, b-1, c-1);
		}
		
		save[a][b][c] = save[a-1][b][c] + save[a-1][b-1][c] + save[a-1][b][c-1] - save[a-1][b-1][c-1];
		return save[a][b][c];
	}
}
반응형

'Algorithm > 백준' 카테고리의 다른 글

[백준] RGB거리 1149번 -java  (0) 2021.11.07
[백준] 파도반 수열 9461번 - java  (0) 2021.11.07
[백준] N과 M (4) 15652번 - java  (0) 2021.11.06
[백준] N과 M (3) 15651번 - java  (0) 2021.11.06
[백준] N과 M (2) 15650번 - java  (0) 2021.11.06