Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class matrixpath {
- static int counter = 0;
- //Добре дошли в моя код :D. Имаше код в класруум, обаче реших да почна отначало (from scratch).
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- System.out.print("What size should the square matrix be?: " );
- int n = scan.nextInt();
- int[][] matrix = new int[n][n]; //Дефинираме матрицата
- possiblePaths(matrix,0 ,0);//Викаме нашия метод
- System.out.println("The number of tracks possible is: "+counter);
- }
- public static void possiblePaths(int[][] matrix, int row, int col){
- matrix[col][row] = 1;
- if(row == matrix.length -1&& col == row){ // Всеки път, когато достигне края, дава +1 на брояча на преходите
- counter++;
- }
- if(row < matrix.length - 1){ // Проверяваме дали е възможно да мръдне на там.
- possiblePaths(matrix,row+1,col);
- }
- if(col < matrix.length -1){
- possiblePaths(matrix,row,col+1);// Проверяваме дали е възможно да мръдне на там.
- }
- }
- /* Условието на задачата да се изчислят всички възможни пътища с движение само надясно и надолу е изпълнено.
- Надявам се кода да ви допада, тъй като е кратък и лесен за обяснение.
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement