Java Google Foobar第4级运行,Bunnies StackOverFlow
我面临未知输入的StackOverFlow异常。我在本地尝试了许多测试用例,但没有找到一个。但在提交解决方案时,我遇到了它。有人能指出我缺少的测试用例吗,或者建议一个更好的方法吗 问题和我的代码如下所示 你和你获救的兔子囚犯需要尽快走出这个正在倒塌的空间站死亡陷阱!不幸的是,一些兔子由于长期监禁而变得虚弱,不能跑得很快。他们的朋友正试图帮助他们,但如果你也参与进来,这种逃跑会快得多。防御性舱壁门已经开始关闭,如果你不及时通过,你将被困住!你需要抓住尽可能多的兔子,在它们关上之前穿过舱壁 从起点移动到所有兔子和舱壁所需的时间将以整数的平方矩阵形式给出。每一排都会告诉你到达起点所需的时间,第一只兔子,第二只兔子,…,最后一只兔子,然后依次到达舱壁。行的顺序遵循相同的模式(开始、每个兔子、隔板)。兔子可以跳到你的怀里,所以抓起它们是瞬间的,同时到达舱壁,因为它密封,仍然允许一个成功的,如果戏剧性的,逃脱。(别担心,任何你没有捡到的兔子都可以和你一起逃走,因为它们不再需要携带你捡到的兔子。)如果你愿意,你可以重游不同的地点,搬到舱壁并不意味着你必须立即离开——如果时间允许,你可以在舱壁上来回走动,捡起更多的兔子 除了花时间在兔子之间旅行外,一些路径还与空间站的安全检查站相互作用,并将时间加回到时钟上。向时钟添加时间将延迟舱壁门的关闭,如果在舱壁门已经关闭后时间返回到0或正数,则会触发舱壁重新打开。因此,在一个圆圈中行走并不断获得时间是可能的:也就是说,每次穿过一条路径时,使用或增加的时间量是相同的 写一个函数形式的答案(时间,时间限制)来计算你能捡到的最多的兔子以及它们是哪只兔子,同时在门永远关上之前仍然通过舱壁逃走。如果有多组大小相同的兔子,则按排序顺序返回具有最低囚犯ID(作为索引)的兔子集兔子被表示为按囚犯ID排序的列表,第一个兔子为0。最多有5只兔子,时间限制是一个非负整数,最多999。 时间限制为1时,五个内部阵列行分别指定起点、兔子0、兔子1、兔子2和舱壁门出口。您可以选择以下路径:Java Google Foobar第4级运行,Bunnies StackOverFlow,java,algorithm,graph,stack-overflow,graph-theory,Java,Algorithm,Graph,Stack Overflow,Graph Theory,我面临未知输入的StackOverFlow异常。我在本地尝试了许多测试用例,但没有找到一个。但在提交解决方案时,我遇到了它。有人能指出我缺少的测试用例吗,或者建议一个更好的方法吗 问题和我的代码如下所示 你和你获救的兔子囚犯需要尽快走出这个正在倒塌的空间站死亡陷阱!不幸的是,一些兔子由于长期监禁而变得虚弱,不能跑得很快。他们的朋友正试图帮助他们,但如果你也参与进来,这种逃跑会快得多。防御性舱壁门已经开始关闭,如果你不及时通过,你将被困住!你需要抓住尽可能多的兔子,在它们关上之前穿过舱壁 从起点移
Start End Delta Time Status
- 0 - 1 Bulkhead initially open
0 4 -1 2
4 2 2 0
2 4 -1 1
4 3 2 -1 Bulkhead closes
3 4 -1 0 Bulkhead reopens; you and the bunnies exit
使用此解决方案,您将拾取兔子1和2。这是空间站走廊的最佳组合,所以答案是[1,2]
测试用例
投入:
(int) times = [[0, 1, 1, 1, 1], [1, 0, 1, 1, 1], [1, 1, 0, 1, 1], [1, 1, 1, 0, 1], [1, 1, 1, 1, 0]]
(int) time_limit = 3
(int) times = [[0, 2, 2, 2, -1], [9, 0, 2, 2, -1], [9, 3, 0, 2, -1], [9, 3, 2, 0, -1], [9, 3, 2, 2, 0]]
(int) time_limit = 1
输出:
(int list) [0, 1]
(int list) [1, 2]
投入:
(int) times = [[0, 1, 1, 1, 1], [1, 0, 1, 1, 1], [1, 1, 0, 1, 1], [1, 1, 1, 0, 1], [1, 1, 1, 1, 0]]
(int) time_limit = 3
(int) times = [[0, 2, 2, 2, -1], [9, 0, 2, 2, -1], [9, 3, 0, 2, -1], [9, 3, 2, 0, -1], [9, 3, 2, 2, 0]]
(int) time_limit = 1
输出:
(int list) [0, 1]
(int list) [1, 2]
我的代码
基本上我要做的是首先检查是否有一个负循环。如果是,那么所有的兔子都可以被救出来。如果没有,我基本上会做一个dfs
import java.io.*;
import java.util.*;
public class RunningWithTheBunnies
{
public static int maxCount = 0;
public static int[] toReturn = null;
public static int[] arr = new int[5];
public static int rows = 0;
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int rows = Integer.parseInt(br.readLine());
int[][] times = new int[rows][rows];
String[] arr = null;
for(int i = 0 ; i < rows ; i++)
{
arr = br.readLine().split(" ");
for(int j = 0 ; j < rows ; j++)
{
times[i][j] = Integer.parseInt(arr[j]);
}
}
int time_limit = Integer.parseInt(br.readLine());
System.out.println(answer(times,time_limit));
for(int i = 0 ; i < toReturn.length ; i++)
{
System.out.print(toReturn[i] + " ");
}
System.out.println("");
}
public static int[] answer(int[][] times,int time_limit)
{
rows = times.length;
int containsCycle = containsNegativeCycle(times);
if(containsCycle == 1){
System.out.println("has negative cycle");// for degubbing
toReturn = new int[rows - 2];
for(int i = 0 ; i < rows - 2 ; i++)
{
toReturn[i] = i;
}
return toReturn;
}
else
{
System.out.println("has no negative cycle");// for debugging
//return new int[2];
int[] visiting = new int[rows];
for(int i = 0 ; i < rows ; i++)
{
visiting[i] = -2;
}
dfs(0,0,time_limit,visiting,times);
return toReturn;
}
}
public static void dfs(int vertex,int count,int timeStatus,int[] visiting,int[][] times)
{
if(timeStatus < -1)
return;
System.out.println("at vertex : " + vertex + ", with status = " + timeStatus);// for debugging purpose.
visiting[vertex] = timeStatus;
for(int i = 0 ; i < rows ; i++)
{
if(i != vertex && visiting[i] == -2 && timeStatus - times[vertex][i] > -2)
{
//arr[count] = i;
//dfs(vertex,count + 1,timeStatus - times[vertex][i],visiting,times);
if(i != 0 && i != rows - 1)
{
arr[count] = i - 1;
dfs(i,count + 1,timeStatus - times[vertex][i],visiting,times);
}
else
{
dfs(i,count,timeStatus - times[vertex][i],visiting,times);
}
}
// else if(i != vertex && (visiting[i] < timeStatus - times[vertex][i] || i == rows - 1 || i == 0) && timeStatus - times[vertex][i] > -2)
else if(i != vertex && timeStatus - times[vertex][i] > -2)
{
dfs(i,count,timeStatus - times[vertex][i],visiting,times);
}
}
if(vertex == rows - 1 && timeStatus >= 0 && arr.length > maxCount)
{
toReturn = new int[arr.length];
for(int i = 0 ; i < arr.length ; i++)
{
toReturn[i] = arr[i];
System.out.println("added " + toReturn[i] + ",at i = " + i );// for debugging
}
maxCount = arr.length;
}
visiting[vertex] = -2;
}
public static int containsNegativeCycle(int[][] times)
{
int[] dist = new int[rows];
dist[0] = 0;
for(int i = 1 ; i < rows ; i++)
{
dist[i] = Integer.MAX_VALUE;
}
for(int k = 0 ; k < rows - 1 ; k++)
{
for(int i = 0 ; i < rows ; i++)
{
for(int j = 0 ; j < rows ; j++)
{
if(i != j && dist[i] != Integer.MAX_VALUE)
{
if(dist[j] > dist[i] + times[i][j])
{
dist[j] = dist[i] + times[i][j];
}
}
}
}
}
for(int i = 0 ; i < rows ; i++)
{
for(int j = 0 ; j < rows ; j++)
{
if(i != j && dist[j] > dist[i] + times[i][j])
return 1;
}
}
return 0;
}
}
import java.io.*;
导入java.util.*;
与兔子赛跑的公共课
{
公共静态int maxCount=0;
公共静态int[]toReturn=null;
公共静态int[]arr=新int[5];
公共静态int行=0;
公共静态void main(字符串[]args)引发IOException
{
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
int rows=Integer.parseInt(br.readLine());
int[][]次=新int[行][行];
字符串[]arr=null;
对于(int i=0;i-2)
{
//arr[count]=i;
//dfs(顶点,计数+1,时间状态-次数[顶点][i],访问,次数);
如果(i!=0&&i!=行-1)
{
arr[count]=i-1;
dfs(i,count+1,timeStatus-times[vertex][i],访问,次数);