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

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;
}
}

Leetcode 招商银行-03. 点燃木棒的更多相关文章

  1. 【LeetCode字符串#03】图解翻转字符串中的单词,以及对于for使用的说明

    翻转字符串中的单词 力扣题目链接(opens new window) 给定一个字符串,逐个翻转字符串中的每个单词. 示例 1: 输入: "the sky is blue" 输出: ...

  2. LeetCode Algorithm 05_Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  3. Leetcode:面试题 04.03. 特定深度节点链表

    Leetcode:面试题 04.03. 特定深度节点链表 Leetcode:面试题 04.03. 特定深度节点链表 先贴一下自己写过一个模板,按层数遍历: https://www.cnblogs.co ...

  4. (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters

    3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...

  5. LeetCode 面试题 02.03. 删除中间节点

    题目链接:https://leetcode-cn.com/problems/delete-middle-node-lcci/ 实现一种算法,删除单向链表中间的某个节点(除了第一个和最后一个节点,不一定 ...

  6. [LeetCode] Additive Number 加法数

    Additive number is a positive integer whose digits can form additive sequence. A valid additive sequ ...

  7. [LeetCode] Number of Digit One 数字1的个数

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...

  8. [LeetCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  9. Leetcode: LFU Cache && Summary of various Sets: HashSet, TreeSet, LinkedHashSet

    Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the f ...

  10. Leetcode 7 Reverse Integer 数论

    题意:将整数倒置,该题简单但是需要注意数据的范围,难得的好题. 如果出现1000000003或者-2000000003,倒置后的数超过int的范围,因此返回0,出现这种情况可以使用long long, ...

随机推荐

  1. Python numpy数组操作(分割数组)

    分割数组 函数 数组及操作 split 将一个数组分割为多个子数组 hsplit 将一个数组水平分割为多个子数组(按列) vsplit 将一个数组垂直分割为多个子数组(按行) numpy.split ...

  2. Blazor OIDC 单点登录授权实例5 - 独立SSR App (net8 webapp ) 端授权

    目录: OpenID 与 OAuth2 基础知识 Blazor wasm Google 登录 Blazor wasm Gitee 码云登录 Blazor OIDC 单点登录授权实例1-建立和配置IDS ...

  3. JS leetcode 有序数组的平方 题解分析,灵活运用Math.pow与Math.abs方法

    壹 ❀ 引 郁闷的周一,晚上来做一道简单的算法题提提神,题目来自leetcode977. 有序数组的平方,题目描述如下: 给定一个按非递减顺序排序的整数数组 A,返回每个数字的平方组成的新数组,要求也 ...

  4. Centos8 单机配置 Zookeeper3.6.3 集群

    安装 Zookeeper 3.6.3 前提 已经安装好 JDK8+. 如果使用JDK8, 版本需要在211以上. 下载, 解压 使用root用户 wget https://downloads.apac ...

  5. look命令

    look命令 look命令用于查询单词,仅需指定欲查询的字首字符串,它会显示所有开头字符串符合该条件的单词. 语法 look [-bdf] [-t char] string [file ...] 参数 ...

  6. SSIS数据同步系统

    缘起: 若干年前,刚到一家3C类的电商公司做DBA,到公司没几天,公司的CEO和研发总监,说现在要做个事情: 把IDC的数据库的数据,要同步一份到仓库,因单量大,仓库经常爆仓,仓库网络不好时,可以直接 ...

  7. 循环掌控:深入理解C语言循环结构,高效实现重复性任务

    欢迎大家来到贝蒂大讲堂 养成好习惯,先赞后看哦~ 所属专栏:C语言学习 贝蒂的主页:Betty's blog 引言 前面贝蒂带大家了解了选择结构,今天就来为大家介绍循环结构,也就是我们熟悉的while ...

  8. 【Android逆向】破解看雪9月算法破解第一题

    1. 安装apk到手机 2. 随意输入账号和密码,点击register,报错crackme1:ERROR 3. 将apk拖入到jadx中进行观察 public native String regist ...

  9. 硬件开发笔记(九): 硬件开发基本流程,制作一个USB转RS232的模块(八):创建asm1117-3.3V封装库并关联原理图元器件

    前言   有了原理图,可以设计硬件PCB,在设计PCB之间还有一个协同优先动作,就是映射封装,原理图库的元器件我们是自己设计的.为了更好的表述封装设计过程,本文描述了一个创建asm1117-3.3V封 ...

  10. Ubuntu防火墙相关

    查看防火墙当前状态 sudo ufw status 开启防火墙 sudo ufw enable 关闭防火墙 sudo ufw disable 查看防火墙版本 sudo ufw version 默认允许 ...