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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; public class Main { static int z=0; static int o=0; static int[] a = { 0, 0 }; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); int[][] ar = new int[T][3]; int[][] dp = new int[T][3]; for(int i=0;i<T;i++){ for(int j=0;j<3;j++){ ar[i][j] = sc.nextInt(); } } dp[0][0] = ar[0][0]; dp[0][1] = ar[0][1]; dp[0][2] = ar[0][2]; for(int i=1;i<T;i++){ for(int j=0;j<3;j++){ if(j==0){ dp[i][j] = Math.min(ar[i][j]+dp[i-1][j+1], ar[i][j]+dp[i-1][j+2]); }else if(j==1){ dp[i][j] = Math.min(ar[i][j]+dp[i-1][j-1], ar[i][j]+dp[i-1][j+1]); }else{ dp[i][j] = Math.min(ar[i][j]+dp[i-1][j-1], ar[i][j]+dp[i-1][j-2]); } } } System.out.println(Math.min(dp[T-1][0],Math.min(dp[T-1][1], dp[T-1][2]))); } } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { throw new RuntimeException(e); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } | cs |
'알고리즘' 카테고리의 다른 글
[1003번] 피보나치 수열 (0) | 2017.12.12 |
---|---|
[1965번] 상자넣기 (0) | 2017.12.05 |
[11404번] 플로이드 (0) | 2017.12.05 |
[1475번] 방 번호 (0) | 2017.12.05 |
[2869번] 달팽이는 올라가고 싶다 (0) | 2017.12.05 |