Advertisement
Guest User

Untitled

a guest
Nov 27th, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class Li {
  5.    
  6.    
  7.     public int[][] pole = new int[10][14];
  8.    
  9.     Li(){
  10.         int[][]mas = {
  11.                 {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
  12.                 {-1,0,0,-1,0,0,0,0,0,0,0,0,0,-1},
  13.                 {-1,0,0,-1,0,0,0,0,0,0,0,0,0,-1},
  14.                 {-1,0,0,0,0,0,0,0,0,0,0,0,0,-1},
  15.                 {-1,0,0,0,0,0,0,0,0,-1,-1,0,0,-1},
  16.                 {-1,0,-1,-1,-1,-1,0,0,0,0,0,0,0,-1},
  17.                 {-1,0,0,0,0,0,0,0,0,0,-1,-1,0,-1},
  18.                 {-1,0,0,0,0,0,0,0,0,0,-1,-1,0,-1},
  19.                 {-1,0,0,0,0,0,0,0,0,0,0,0,0,-1},
  20.                 {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
  21.                 }; 
  22.        
  23.         this.pole = mas;
  24.     }
  25.    
  26.     /**
  27.      * @param args
  28.      */
  29.     public static void main(String[] args) {
  30.        
  31.         Scanner sc = new Scanner(System.in);
  32.        
  33.          System.out.println("Введите координаты точки начала:");
  34.          int X_begin, Y_begin;
  35.          X_begin  = sc.nextInt();
  36.          Y_begin  = sc.nextInt();
  37.          
  38.          System.out.println("Введите координаты точки назначения:");
  39.          int X_end, Y_end;
  40.          X_end  = sc.nextInt();
  41.          Y_end  = sc.nextInt();
  42.          
  43.          int d = 1;
  44.          
  45.          Li board = new Li();
  46.          
  47.          if (X_begin == X_end && Y_begin == Y_end )  System.out.println("Вы уже на нужном месте!");
  48.          else {
  49.          
  50.              board.pole[X_begin][Y_begin] = d;
  51.              
  52.              int count = 1;
  53.              
  54.              while (board.pole[X_end][Y_end] == 0 && count > 0){
  55.                  
  56.                  count = 0;
  57.                  
  58.                  for (int i = 0; i < 10; i++){
  59.                      for (int j = 0; j < 14; j++){
  60.                          if (board.pole[i][j] == d){
  61.                             if (board.pole[i-1][j] == 0) {board.pole[i-1][j] = d+1; count++;}
  62.                             if (board.pole[i+1][j] == 0) {board.pole[i+1][j] = d+1; count++;}
  63.                             if (board.pole[i][j-1] == 0) {board.pole[i][j-1] = d+1; count++;}
  64.                             if (board.pole[i][j+1] == 0) {board.pole[i][j+1] = d+1; count++;}
  65.                            
  66.                             }
  67.                      }
  68.                      }
  69.                  d++;
  70.              }
  71.          
  72.          }
  73.          
  74.          System.out.println();
  75.          
  76.          if (board.pole[X_end][Y_end] == 0) System.out.println("Пути нет!");
  77.          else {}
  78.          
  79.          for (int i = 0; i < 10; i++){
  80.              for (int j = 0; j < 14; j++){
  81.                  if (board.pole[i][j] > 0)
  82.                  System.out.print((board.pole[i][j]-1)+"  ");
  83.                  else System.out.print((board.pole[i][j])+"  "); } System.out.println();}
  84.          
  85.     }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement