某東Java工程師筆試題解(含BFS通用通俗模板)

題目

大體就是n行m列二維地圖 A在(x,y),B在(a,b)。地圖中有障礙物「#」表示不能通行,「.」 表示能夠通行,A每次能夠往上、下、左、右行走,不容許走出地圖也不容許穿越障礙物。
判斷A是否能成功走到B的位置node


題解

思路就是BFS (套用BFS的模板框架) 判斷條件爲當前座標是否與B的一致算法


BFS框架模板(Java)框架

//BFS算法框架

boolean[][] mark = new boolean[n][m]; //訪問標記
static int[][] go = {{0, -1}, {1, 0}, {-1, 0}, {0, 1}}; //方向向量

static class State
{
    int x,y; //座標位置
    int step; //搜索步數記錄
}

boolean Check(State s)
{
    //邊界判斷
  return s.x>=0&&s.x<n&&s.y>0&&s.y<=m;
}

void BFS(State st)
{
    queue<State> q;
    State now,next;
    st.step = 0; //步數清零;
    q.add(st); //入隊;
    while(!q.isEmpty())
    {
        now = q.poll(); //隊首元素出隊;
        if(now == 目標狀態) //出現目標狀態,此時的step爲最小值,作作相關處理後退出便可;
        {
            ……;
            return ....;
        }
        //若是沒有到目標狀態
            for(int i=0;i<4;i++)
            {
                next.x = now.x + go[i][0];//按照規則生成下一個狀態
                next.y = now.y + go[i][1];
                if(CheckState(next)&&(根據題目修改條件)&&!mark) //未越界、未被訪問、知足題目條件
                {
                    next.step = now.step + 1;
                    mark[next.x][next.y]=true;
                    q.add(next);
                }
            }
    }
}

public static void main(String[] args)
{
    //輸入
    BFS();
    //輸出
  
}

題解代碼

public class AandB {
    //初始化節點
    static class Node {
        int x;
        int y;
        public Node(int x, int y) {
            this.x = x;
            this.y = y;
        }
        public Node() {
        }
    }
    //初始化全局變量
    static int n;
    static int m;
    //行走的方向
    static int[][] dir = {{0, -1}, {1, 0}, {-1, 0}, {0, 1}};
    //a、b表明各自的座標
    static int ax = -1;
    static int ay = -1;
    static int bx = -1;
    static int by = -1;
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int total = scanner.nextint();
        while (total-- > 0) {
            n = scanner.nextint();
            m = scanner.nextint();
            Boolean[][] visit = new Boolean[n][m];
            //訪問標記
            String[] map = new String[n];
            for (int i = 0; i < n; i++) {
                map[i] = scanner.next();
            }
                          //初始化座標
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (map[i].charAt(j) == 'B') {
                        bx = i;
                        by = j;
                    }
                    if (map[i].charAt(j) == 'A') {
                        ax = i;
                        ay = j;
                    }
                }
            }
            if (bfs(ax, ay, map, visit)) {
                System.out.println("yes");
            } else {
                System.out.println("no");
            }
        }
    }
        //越界
    static Boolean check(Node node) {
        return node.x >= 0 && node.x < n && node.y >= 0 && node.y < m;
    }
    static Boolean bfs(int x, int y, String[] map, Boolean[][] visit) {
        Queue<Node> queue = new LinkedList<>();
        queue.add(new Node(x, y));
        while (!queue.isEmpty()) {
            Node node = queue.poll();
            if (node.x == bx && node.y == by) {
                return true;
            }
            for (int i = 0; i < 4; i++) {
                Node next = new Node();
                next.x = node.x + dir[i][0];
                next.y = node.y + dir[i][1];
                //判斷條件
                if (check(next) && (map[next.x].charAt(next.y) == '.' || map[next.x].charAt(next.y) == 'B') && !visit[next.x][next.y]) {
                    visit[next.x][next.y] = true;
                    queue.add(next);
                }
            }
        }
        return false;
    }
}

輸入輸出數據

2   //兩組數據
2 2  //n行m列
.B
A.
2 2
#B
A#
相關文章
相關標籤/搜索