Advertisement
wellison1

Untitled

Oct 13th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class UpsideDown {
  3.     public static  void main (String[] args){
  4.         Scanner in = new Scanner(System.in);
  5.         boolean teste = false;
  6.  
  7.         int [][] matriz = new int[9][9];
  8.         for (int i = 0; i < 9;i++){
  9.             for (int j = 0;j < 9;j++){
  10.                 matriz[i][j] = in.nextInt();
  11.             }
  12.  
  13.         }
  14.  
  15.           teste = caminho(matriz,0,0);
  16.             if (teste){
  17.                 System.out.println("Welcome to the Upside Down.");
  18.             } else{
  19.                 System.out.println("RUN!");
  20.             }
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.     }
  28.     public static boolean caminho (int matriz [][],int i,int j){
  29.         boolean resposta = false;
  30.         //caso base
  31.         if (i==9 && j ==9){
  32.            resposta = true;
  33.         }
  34.         else {
  35.             //SUL
  36.             if (i + 1 <= 9){
  37.                 if (matriz[i + 1][j] == 1) {
  38.                     caminho(matriz, i + 1, j);
  39.                 }
  40.         }
  41.             //LESTE
  42.             else if (j+1 <= 9) {
  43.                 if (matriz[i][j+1]==1) {
  44.                     caminho(matriz, i, j + 1);
  45.                 }
  46.             }
  47.             //NORTE
  48.             else if (i-1>=0) {
  49.                 if (matriz[i-1][j]==1) {
  50.                     caminho(matriz, i - 1, j);
  51.                 }
  52.             }
  53.             //OESTE
  54.          else if (j - 1 >= 0) {
  55.                 if (matriz[i][j-1] == 1) {
  56.                     caminho(matriz, i + 1, j);
  57.                 }
  58.             }
  59.         }
  60.         return resposta;
  61.     }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement