Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Li {
- public int[][] pole = new int[10][14];
- Li(){
- int[][]mas = {
- {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
- {-1,0,0,-1,0,0,0,0,0,0,0,0,0,-1},
- {-1,0,0,-1,0,0,0,0,0,0,0,0,0,-1},
- {-1,0,0,0,0,0,0,0,0,0,0,0,0,-1},
- {-1,0,0,0,0,0,0,0,0,-1,-1,0,0,-1},
- {-1,0,-1,-1,-1,-1,0,0,0,0,0,0,0,-1},
- {-1,0,0,0,0,0,0,0,0,0,-1,-1,0,-1},
- {-1,0,0,0,0,0,0,0,0,0,-1,-1,0,-1},
- {-1,0,0,0,0,0,0,0,0,0,0,0,0,-1},
- {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
- };
- this.pole = mas;
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.println("Введите координаты точки начала:");
- int X_begin, Y_begin;
- X_begin = sc.nextInt();
- Y_begin = sc.nextInt();
- System.out.println("Введите координаты точки назначения:");
- int X_end, Y_end;
- X_end = sc.nextInt();
- Y_end = sc.nextInt();
- int d = 1;
- Li board = new Li();
- if (X_begin == X_end && Y_begin == Y_end ) System.out.println("Вы уже на нужном месте!");
- else {
- board.pole[X_begin][Y_begin] = d;
- int count = 1;
- while (board.pole[X_end][Y_end] == 0 && count > 0){
- count = 0;
- for (int i = 0; i < 10; i++){
- for (int j = 0; j < 14; j++){
- if (board.pole[i][j] == d){
- if (board.pole[i-1][j] == 0) {board.pole[i-1][j] = d+1; count++;}
- if (board.pole[i+1][j] == 0) {board.pole[i+1][j] = d+1; count++;}
- if (board.pole[i][j-1] == 0) {board.pole[i][j-1] = d+1; count++;}
- if (board.pole[i][j+1] == 0) {board.pole[i][j+1] = d+1; count++;}
- }
- }
- }
- d++;
- }
- }
- System.out.println();
- if (board.pole[X_end][Y_end] == 0) System.out.println("Пути нет!");
- else {}
- for (int i = 0; i < 10; i++){
- for (int j = 0; j < 14; j++){
- if (board.pole[i][j] > 0)
- System.out.print((board.pole[i][j]-1)+" ");
- else System.out.print((board.pole[i][j])+" "); } System.out.println();}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement