Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package DynamicProgramming;
- import java.util.Scanner;
- public class DynamicProgramming {
- //динамична таблица
- static long table[][] = new long[30][30];
- public static long RightDown(int row, int col) {
- if (row == 0 || col == 0) { // невалиден вход
- return 0;
- }
- if (row == 1 && col == 1){ // възможен е само един ход
- return 1;
- }
- if (table[row][col] != Integer.MIN_VALUE) { // проверка
- return table[row][col];
- }
- long result = 0;
- result += RightDown(row - 1, col); // стъпка надолу
- result += RightDown(row, col - 1); // стъпка надясно
- table[row][col] = result;
- return table[row][col];
- }
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- int rows = scan.nextInt();
- int cols = scan.nextInt();
- // пълним таблицата със стойности, които не могат да бъдат решение
- for (int i = 0; i < 30; i++) {
- for(int j = 0; j < 30; j++) {
- table[i][j] = Integer.MIN_VALUE;
- }
- }
- System.out.printf("Съществуват %d начина",RightDown(rows,cols));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment