Advertisement
wellison1

Concluido

Oct 13th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.52 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.         if (teste){
  15.             boolean aux = caminho(matriz,0,0);
  16.             System.out.println("Welcome to the Upside Down.");
  17.  
  18.         }
  19.         else {
  20.             System.out.println("RUN!");
  21.         }
  22.     }
  23.     public static boolean caminho (int matriz [][],int i,int j){
  24.         boolean resposta = false;
  25.         //caso base
  26.         if (i==9 && j ==9){
  27.            resposta = true;
  28.         }
  29.         else {
  30.             //SUL
  31.             if (matriz[i + 1][j] == 1) {
  32.                 if (i < 8) {
  33.                     caminho(matriz, i + 1, j);
  34.                 }
  35.             }
  36.             //LESTE
  37.             else if (matriz[i][j + 1] == 1) {
  38.                 if (j < 8) {
  39.                     caminho(matriz, i, j + 1);
  40.                 }
  41.             }
  42.             //NORTE
  43.             else if (matriz[i][j] == 1) {
  44.                 if (i > 1) {
  45.                     caminho(matriz, i - 1, j);
  46.                 }
  47.             }
  48.             //OESTE
  49.             else if (matriz[i][j] == 1) {
  50.                 if (j > 1) {
  51.                     caminho(matriz, i, j - 1);
  52.                 }
  53.             }
  54.         }
  55.         return resposta;
  56.     }
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement