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

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. 21.1 动态TLS--《Windows核心编程》

    应用程序通过调用一组4个函数来使用动态 TLS,这些函数实际上最经常为 DLL 所使用. 通常情况下,如果DLL使用 TLS,那么当它用 DLL_PROCESS_ATTACH 标志调用它的 DllMa ...

  2. Python Rich:美化终端显示效果

    Rich库的功能就像它的名字一样,使Python编程更加丰富(rich),它帮助开发者在控制台(命令行)输出中创建丰富.多彩和具有格式化的文本. 本篇总结了如何使用Rich库让我们的命令行工具更加美观 ...

  3. C# 二十年语法变迁之 C# 8参考

    C# 二十年语法变迁之 C# 8参考 自从 C# 于 2000 年推出以来,该语言的规模已经大大增加,我不确定任何人是否有可能在任何时候都对每一种语言特性都有深入的了解.因此,我想写一系列快速参考文章 ...

  4. Resharper 和 Rider 的奇淫技巧,你知道多少?

    Resharper 和 Rider 的奇淫技巧,你知道多少? .NET 开发中最令人印象深刻的生产力工具之一是ReSharper.每次发布时,我都对它的功能感到震惊.不要误会我的意思,我喜欢 Visu ...

  5. 吉特日化MES & WMS 与周边系统集成架构

    作者:情缘   出处:http://www.cnblogs.com/qingyuan/ 关于作者:从事仓库,生产软件方面的开发,在项目管理以及企业经营方面寻求发展之路 版权声明:本文版权归作者和博客园 ...

  6. NC51178 没有上司的舞会

    题目链接 题目 题目描述 Ural大学有N名职员,编号为1~N. 他们的关系就像一棵以校长为根的树,父节点就是子节点的直接上司. 每个职员有一个快乐指数,用整数 \(Hi\) 给出,其中 \(1\le ...

  7. NC16671 [NOIP2006]金明的预算方案

    题目链接 题目 题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:"你的房间需要购买哪些物品,怎么布置,你说了算, ...

  8. C语言,结构体成员的地址

    先回顾一个基础的知识,不同类型的数据在16位,32位,64位的机器分别占用多少字节. 类型 16位机器(字节) 32位机器(字节) 64位机器(字节) char 1 1 1 short 2 2 2 i ...

  9. QT & C++笔记

    语法 变量声明 直接声明的变量, 其赋值操作会产生值拷贝, 例如 QString b("some text"); QString a(b); int a = 10; int b = ...

  10. 【C#】基于JsonConvert解析Json数据

    1 解析字典 ​ 1)解析为 JObject private void ParseJson() { // 解析为JObject string jsonStr = "{'name': 'zha ...