Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package robotti2;
- import java.util.ArrayList;
- import java.util.Scanner;
- import static robotti2.Robotti2.*;
- class Maze {
- public final int[] layout;
- public final int[] distances;
- public int pos;
- public boolean isAlive;
- public Maze(int[] layout) {
- this.layout = layout;
- this.distances = createDistanceMatrix();
- this.pos = 0;
- this.isAlive = true;
- }
- public int getDistance() {
- return distances[pos];
- }
- public int getDistance(int dir) {
- if (!canMove(layout, pos, dir)) return getDistance();
- return distances[pos+dir];
- }
- private int[] createDistanceMatrix() {
- int a[] = new int[16];
- for (int i = 0; i < 16; i++)
- a[i] = (layout[i] == 1) ? 255 : 99;
- a[15] = 0;
- boolean f;
- do {
- f = false;
- for (int i = 0; i < 16; i++) {
- if (a[i] < 255) {
- int min = a[i];
- for (int m : mov) {
- // if (i+m < 0 || i+m >= 16 || a[i+m] == -1) continue;
- if (canMove(layout, i, m) && a[i+m] < min)
- min = a[i+m];
- }
- if (a[i] > min + 1) {
- a[i] = min + 1;
- f = true;
- }
- }
- }
- } while (f);
- return a;
- }
- public void move(int dir) {
- int npos = pos + dir;
- if (npos < 0 || npos >= 16) return;
- if (layout[npos] == 1) return;
- if (dir == 1 && pos % 4 == 3) return;
- if (dir == -1 && pos % 4 == 0) return;
- if (Math.abs((npos - pos) % 4) > 1) return;
- pos = npos;
- if (pos == 15) this.isAlive = false;
- }
- public void print() {
- for (int i = 0; i < 4; i++) {
- for (int j = 0; j < 4; j++)
- System.out.printf("%-2s", i*4 + j == pos ? "x" : layout[i*4 + j]);
- System.out.println();
- }
- System.out.println();
- }
- }
- public class Robotti2 {
- public static final int R = 1;
- public static final int L = -1;
- public static final int U = -4;
- public static final int D = 4;
- public static final int[] mov = new int[] {R,L,U,D};
- public static ArrayList<Maze> mazes = new ArrayList<>();
- public static boolean isSolution(String s) {
- int remaining = mazes.size();
- for (char c : s.toCharArray()) {
- for (Maze maze : mazes) {
- if (!maze.isAlive) continue;
- if (c == 'R')
- maze.move(R);
- if (c == 'L')
- maze.move(L);
- if (c == 'U')
- maze.move(U);
- if (c == 'D')
- maze.move(D);
- if (!maze.isAlive) {
- remaining--;
- }
- }
- }
- for (Maze maze : mazes) maze.isAlive = true;
- System.out.println(remaining);
- return remaining == 0;
- }
- public static void main(String[] args) {
- generateMazes("0");
- handGame();
- }
- public static void handGame() {
- int remaining = mazes.size();
- String seq = "";
- Scanner sc = new Scanner(System.in);
- while (remaining > 0) {
- System.out.println("Sequence: " + seq + " Length: " + seq.length());
- System.out.println("---------------------");
- System.out.println("Removed: " + remaining);
- System.out.println("Remaining: " + remaining);
- int _a = 0;
- int[] a = new int[16];
- int[] p = new int[16];
- for (Maze maze : mazes) {
- if (!maze.isAlive) continue;
- for (int i = 0; i < 16; i++) {
- a[i] += maze.layout[i];
- _a += a[i];
- }
- p[maze.pos]++;
- }
- System.out.println("");
- System.out.printf("%-36s", " | Obstacles");
- System.out.printf(" | Current positions\n");
- for (int i = 0; i < 4; i++) {
- System.out.print(" | ");
- for (int j = 0; j < 4; j++) {
- System.out.printf("%4d", a[i*4 + j]);
- System.out.print(" ");
- }
- System.out.print(" | ");
- for (int j = 0; j < 4; j++) {
- System.out.printf("%4d", p[i*4 + j]);
- System.out.print(" ");
- }
- System.out.println();
- }
- System.out.println();
- System.out.print("Command: ");
- String cmd = sc.nextLine();
- seq += cmd.toUpperCase();
- for (Maze maze : mazes) {
- if (!maze.isAlive) continue;
- if (cmd.equals("d"))
- maze.move(R);
- if (cmd.equals("a"))
- maze.move(L);
- if (cmd.equals("w"))
- maze.move(U);
- if (cmd.equals("s"))
- maze.move(D);
- if (!maze.isAlive) {
- remaining--;
- }
- }
- }
- }
- public static void generateMazes(String layout) {
- if (layout.length() == 16) {
- if (layout.charAt(15) == '1') return;
- int[] a = new int[16];
- for (int i = 0; i < 16; i++) a[i] = Character.digit(layout.charAt(i), 10);
- if (solutionExists(a, 15)) mazes.add(new Maze(a));
- return;
- }
- generateMazes(layout+'1');
- generateMazes(layout+'0');
- }
- public static boolean solutionExists(int[] a, int k) {
- if (a[0] == 2) return true;
- if (a[k] == 2) return false;
- for (int m : mov) {
- if (canMove(a, k, m)) {
- a[k] = 2;
- boolean f = solutionExists(a, k+m);
- a[k] = 0;
- if (f) return true;
- }
- }
- return false;
- }
- public static boolean canMove(int[] a, int pos, int dir) {
- int npos = pos + dir;
- if (npos < 0 || npos >= 16) return false;
- if (a[npos] == 1) return false;
- if (dir == 1 && pos % 4 == 3) return false;
- if (dir == -1 && pos % 4 == 0) return false;
- return Math.abs((npos - pos) % 4) <= 1;
- }
- public static int[] cpy(int[] arr) {
- int[] a = new int[arr.length];
- System.arraycopy(arr, 0, a, 0, a.length);
- return a;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement