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

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. C 语言常用头文件解释

    C系统提供了丰富的系统文件,称为库文件,整理一下以后好实用: <stdio.h> 定义了三个变量类型.一些宏和各种函数来执行输入和输出 https://www.runoob.com/cpr ...

  2. 云原生服务网格Istio:原理、实践、架构与源码解析

    华为云原生团队600多页的Istio实战精华总结,云原生服务网格Istio:原理.实践.架构与源码解析的电子书. 图书介绍 <云原生服务网格Istio:原理.实践.架构与源码解析>分为原理 ...

  3. 【Android】MediaCodec详解

    1 前言 ​ MediaCodec 主要用于视频解码和编码操作,可以实现视频倍速播放.全关键帧转换.视频倒放等功能. ​ MediaCodec 的工作原理图如下: ​ MediaCodec 的主要接口 ...

  4. SpringCloud 注册中心Consul实战

    介绍 Consul是HashiCorp公司推出的开源工具,Consul由Go语言开发,部署起来非常容易,只需要极少的可执行程序和配置文件,具有绿色.轻量级的特点.Consul是分布式的.高可用的. 可 ...

  5. js获取格式化日期方法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. Java I/O 教程(七) DataOutputStream和DataInputStream

    Java DataOutputStream Class Java DataOutputStream class 可以以机器无关方式往指定输出流写入Java原始数据类型,例如int, double, l ...

  7. Java Http客户端选型

    JDK自带 HTTPURLConnect:功能单一 Apache HttpClient https://hc.apache.org/index.html 是一个比较古老的HTTP框架,使用非常普遍. ...

  8. click模块解析命令行参数

    click模块解析命令行参数 安装 pip install click 操作步骤 1)使用@click.command()装饰一个函数,使之成为命令行接口 2)使用@click.option()等装饰 ...

  9. python模块Configparser读取 ini(cfg,txt)等配置文件

    一个ini文件的组成: 一个ini文件是由多个section组成,每个section中以key=vlaue形式存储数据: 示例 # 安装 pip install ConfigParser # 1.导包 ...

  10. isort包

    记录 为什么会使用到这个包,原因是之前在本地开发的时候,导包的时候可能由于不规范,其实你自己看着挺规范的,但是呢后续组长进行打包的时候,代码出现了不规范的情况,导致打包失败.原因就是导包不规范造成的. ...