728x90
https://www.acmicpc.net/problem/1890
// 코드
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
|
import java.util.*;
import java.io.*;
public class Main {
// main
public static void main(String[] args) throws Exception{
// Please Enter Your Code Here
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
long[][] dp = new long[N][N];
int[][] arr = new int[N][];
for(int i=0; i<N; ++i) arr[i] = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
dp[0][0] = 1;
for( int row=0; row<N; ++row ) {
for( int col=0; col<N; ++col ) {
// 자기 자신을 더해주는 것을 막아준다.
if(row == N-1 && col == N-1) break;
// 아래로 이동
if( row+arr[row][col] < N ) dp[row+arr[row][col]][col] += dp[row][col];
// 오른쪽으로 이동
if( col+arr[row][col] < N ) dp[row][col+arr[row][col]] += dp[row][col];
}
}
bw.write(String.valueOf(dp[N-1][N-1]));
bw.flush();
bw.close();
br.close();
}
}
|
cs |
반응형