BFS

76 阅读1分钟

关键词

  • 最短、最少,

关键

  • Map:key是图中的每个点
  • queue:T是图中的点
  • 广搜

773. 滑动谜题

class Solution {
    class Node{
        String str;
        int x,y;
        Node(String str,int x,int y){
            this.str=str;
            this.x=x;
            this.y=y;
        }
    }
    int n=2,m=3;
    String s,e;
    int x,y;
    public int slidingPuzzle(int[][] board) {
        s="";
        e="123450";
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                s+=board[i][j];
                if(board[i][j]==0){
                    x=i;
                    y=j;
                }
            }
        }
        int ans=bfs();
        return ans;
    }
    int[][] dirs=new int[][]{{1,0},{-1,0},{0,1},{0,-1}};
    int bfs(){
        LinkedList<Node> q=new LinkedList<>();
        Map<String,Integer> map=new HashMap<>();
        Node root=new Node(s,x,y);
        q.add(root);
        map.put(s,0);
        while(!q.isEmpty()){
            Node poll=q.poll();
            int step=map.get(poll.str);
            if(poll.str.equals(e)) return step;
            int dx=poll.x,dy=poll.y;
            for(int[] di:dirs){
                int nx=dx+di[0],ny=dy+di[1];
                if(nx<0||nx>=n||ny<0||ny>=m) continue;
                String nStr=update(poll.str,dx,dy,nx,ny);
                if(map.containsKey(nStr)) continue;
                Node next=new Node(nStr,nx,ny);
                q.add(next);
                map.put(nStr,step+1);
            }
        }
        return -1;
    }
    String update(String str,int dx,int dy,int nx,int ny){
        char[] cs=str.toCharArray();
        char tmp=cs[dx*m+dy];
        cs[dx*m+dy]=cs[nx*m+ny];
        cs[nx*m+ny]=tmp;
        return String.valueOf(cs);
    }
}

815. 公交路线

  • 点的定义:此处一个点就是i号路线
class Solution {
    int m,n;
    public int numBusesToDestination(int[][] routes, int source, int target) {
        m=routes.length;n=routes[0].length;
        if(source==target) return 0;
        return bfs(routes,source,target);
    }
    private int bfs(int[][] routes,int source,int target){
        Map<Integer,Set<Integer>> map=new HashMap<>();
        Map<Integer,Integer> record=new HashMap<>();
        LinkedList<Integer> q=new LinkedList<>();
        for(int i=0;i<m;i++){
            for(int station:routes[i]){
                if(station==source){
                    q.add(i);
                    record.put(i,1);
                }
                Set<Integer> hs=map.getOrDefault(station,new HashSet<>());
                hs.add(i);
                map.put(station,hs);
            }
        }
        while(!q.isEmpty()){
            int line=q.poll();
            int step=record.get(line);
            for(int station:routes[line]){
                if(station==target) return step;
                Set<Integer> lines=map.get(station);
                if(lines==null) continue;
                for(int l:lines){
                    if(record.containsKey(l))
                        continue;
                    q.add(l);
                    record.put(l,step+1);
                }
            }
        }
        return -1;
    }
}