Leetcode 招商银行-03. 点燃木棒

用广度优先,暴力搜索。代码如下

 

import java.util.*;

class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.lightSticks(1,2,new int[]{3}));
    }
    public int[] lightSticks(int height, int width, int[] indices) {
        int pointsNum = (height + 1) * (width + 1);
        boolean[][] relation = new boolean[pointsNum][4];
        for(int i = 0;i<pointsNum;i++){
            int x = i / (width+1);
            int y = i % (width+1);

            if(x>0) relation[i][0] = true;
            if(y>0) relation[i][1] = true;
            if(x<height) relation[i][2] = true;
            if(y<width) relation[i][3] = true;
        }
        for(int removed:indices){
             int mod = 2*width + 1;
             int num = removed / mod;
             int pos = removed % mod;
             int x1,y1;
             int x2,y2;

             if( pos < width){
                x1 = num ;
                x2 = num;
                y1 = pos;
                y2 = pos+1;

                 int u = x1*(width+1)+y1;
                 int v = x2*(width+1)+y2;

                 relation[u][3] = false;
                 relation[v][1]  = false;
             }
             else{
                 pos = pos - width;
                 x1 = num;
                 y1 = pos;
                 x2 = x1 + 1;
                 y2 = pos;

                 int u = x1*(width+1)+y1;
                 int v = x2*(width+1)+y2;

                 relation[u][2] = false;
                 relation[v][0] = false;
             }
        }

        int[] costs = new int[pointsNum];
        for (int i = 0; i < pointsNum; i++) {
            int cost = bfs(i,relation,pointsNum,width,height);
            costs[i] = cost;
        }
        int min = Integer.MAX_VALUE;
        for(int cost:costs){
            if(cost < min){
                min = cost;
            }
        }
        if( min == Integer.MAX_VALUE){
            return new int[0];
        }
        int size = 0;
        for(int cost:costs){
            if(cost == min){
                size++;
            }
        }
        int[] ans = new int[size];
        for(int i=0,index=0;i<pointsNum;i++){
            int cost = costs[i];
            if(cost == min){
                ans[index++]=i;
            }
        }
        return ans;
    }

    public int bfs(int start,boolean[][] originRelation,int num,int width,int height){
        boolean[][] relation = new boolean[num][4];
        for(int i = 0;i<num;i++){
            for(int j = 0;j<4;j++){
                relation[i][j] = originRelation[i][j];
            }
        }
        Queue<Integer> queue = new LinkedList<Integer>();
        queue.add(start);

        int[] travedNodes = new int[num];
        Arrays.fill(travedNodes,Integer.MAX_VALUE);
        travedNodes[start] = 0;

        while( !queue.isEmpty()) {
            int u = queue.poll();
            int x = u / (width+1);
            int y = u % (width+1) ;

            //如果可以向上燃:当前坐标不是第一行,并且存在边,并且没有遍历过
            if(relation[u][0] ){
                travedNodes[u-width-1] = travedNodes[u] + 1;
                queue.add(u-width-1);
                relation[u-width-1][2] = false;
                relation[u][0] = false;
            }
            //如果可以向左
            if(relation[u][1] ){
                travedNodes[u-1] = travedNodes[u] + 1;
                queue.add(u-1);
                relation[u][1] = false;
                relation[u-1][3] = false;
            }
            //如果可以向下
            if(relation[u][2]){
                travedNodes[u+width+1] = travedNodes[u] + 1;
                queue.add(u+width+1);
                relation[u][2] = false;
                relation[u+width+1][0] = false;
            }
            //如果可以向右
            if(  relation[u][3]){
                travedNodes[u+1] = travedNodes[u] + 1;
                queue.add(u+1);
                relation[u][3] = false;
                relation[u+1][1] = false;
            }

        }

        for(int i = 0;i<num;i++){
            for(int j = 0;j<4;j++){
                if(relation[i][j]){
                    return Integer.MAX_VALUE;
                }
            }
        }

        int max = 0;
        for(int i=0;i<num;i++){
            if( travedNodes[i] != Integer.MAX_VALUE && travedNodes[i] > max){
                max = travedNodes[i];
            }
        }
        return max;
    }
}

 

posted @ 2022-04-15 19:02  fishcanfly  阅读(174)  评论(0)    收藏  举报
//雪花飘落效果