Battle City (Thuật toán BFS)

package Advance;




import java.io.FileInputStream;
import java.util.Scanner;

public class BattleCity {

    public static void main(String[] args) throws Exception {
        new BattleCity().inIt();
    }
    int N, M, xS, yS, xF, yF;
    final static int TARGET = -5;
    final static int BRICK = -2;
    final static int EMPTY = 0;
    int Result;
    int matrix[][];
    int layer[][];
    MyQueue mQ;
    int move[][] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

    class Location {

        int x;
        int y;
        int value;

        public Location(int a, int b, int v) {
            x = a;
            y = b;
            value = v;
        }
    }

    class MyQueue {

        Location ele[];
        int front;
        int rear;

        public MyQueue() {
            ele = new Location[10000];
            front = rear = 0;
        }

        boolean isEmpty() {
            if (front == rear) {
                return true;
            } else {
                return false;
            }
        }

        void push(Location x) {
            ele[rear] = new Location(x.x, x.y, x.value);
            rear++;
            if (rear == 10000) {
                rear = 0;
            }
        }

        Location pop() {
            int min_val = ele[front].value;
            int min_index = front;
            for (int i = front; i < rear; i++) {

                if (ele[i].value < min_val) {
                    min_val = ele[i].value;
                    min_index = i;
                }
            }
            Location temp = ele[min_index];
            ele[min_index] = ele[front];
            ele[front] = temp;

            Location a = ele[front];
            front++;
            if (front == 10000) {
                front = 0;
            }
            return a;
        }
    }

    private void inIt() throws Exception {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for (int test_case = 1; test_case <= T; test_case++) {

            N = sc.nextInt();
            M = sc.nextInt();
            String s = "";
            Result = 100000;
            matrix = new int[N][M];
            layer = new int[N][M];
            mQ = new MyQueue();

            for (int i = 0; i < N; i++) {
                s = sc.next();
                for (int k = 0; k < s.length(); k++) {
                    if (s.charAt(k) == 'Y') {
                        matrix[i][k] = 1;
                        xS = i;
                        yS = k;
                    }
                    if (s.charAt(k) == 'B') {
                        matrix[i][k] = -2;
                    }
                    if (s.charAt(k) == 'E') {
                        matrix[i][k] = 0;
                    }
                    if (s.charAt(k) == 'S') {
                        matrix[i][k] = -3;
                    }
                    if (s.charAt(k) == 'R') {
                        matrix[i][k] = -4;
                    }
                    if (s.charAt(k) == 'T') {
                        matrix[i][k] = -5;
                        xF = i;
                        yF = k;
                    }
                }
            }
            //

            layer[xS][yS] = 1;
            BFS(new Location(xS, yS, -1));

            if (Result == 100000) {
                Result = -1;
            }

            System.out.println("Case #" + test_case);
            System.out.println(Result);
        }
    }

    private void BFS(Location location) {
        mQ.push(location);
        while (!mQ.isEmpty()) {
            Location u = mQ.pop();
            if (u.x == xF && u.y == yF) {
                if (layer[u.x][u.y] < Result) {
                    Result = layer[u.x][u.y] - 1;
                }
            }

            for (int i = 0; i < 4; i++) {
                int X = u.x + move[i][0];
                int Y = u.y + move[i][1];
                if (check(X, Y)) {
                    if (layer[X][Y] == 0) {
                        if ((matrix[X][Y] == EMPTY || matrix[X][Y] == TARGET)) {
                            layer[X][Y] = layer[u.x][u.y] + 1;
                            mQ.push(new Location(X, Y, layer[X][Y]));
                        }
                        if (matrix[X][Y] == BRICK) {
                            layer[X][Y] = layer[u.x][u.y] + 2;
                            mQ.push(new Location(X, Y, layer[X][Y]));
                        }
                    }
                }
            }
        }
    }

    private boolean check(int x, int y) {
        if (x >= 0 && x < N && y >= 0 && y < M) {
            return true;
        } else {
            return false;
        }
    }
}

Nhận xét

Bài đăng phổ biến từ blog này

Nâng cấp máy tính (Thuật toán quay lui)

Tấn công thành trì (Thuật toán Prim tìm cây khung lớn nhất)