题目大概:
玩家的游戏版本低于游戏的最低版本,现在有很多游戏补丁升级策略,比如游戏玩家当前版本为1.0,而游戏已经更新至5.0。我们就需要升级,升级可以从1.0升级至2.0,然后从2.0升级至4.0,最后再从4.0升级至5.0,整个补丁过程(游戏升级过程)就完成了。当然玩家也可以选择其他升级方式进行,比如1.0->3.0->5.0。而每个补丁的文件大小都不相同,比如1.0->2.0为1M,而1.0->3.0为500k。对于给出的不同补丁版本,求出游戏升级所需最小下载量的升级补丁路径。
输入:第一行,两个参数,第一个是游戏最新版本;第二个是玩家当前版本。
后面有多行(不确定多少行),每行三个数字,为每个补丁的三个参数:第一个是补丁从哪个版本开始修复,第二个是补丁修复到哪个版本,第三个是该补丁的容量大小。
问题与分析:
题目没有指明数据量,没有说明补丁的表示方法(是不是所有的补丁都是10000开始的,下一个补丁是10010。有可能是补丁编号是0,1,2等,就是不知道补丁的数值大小),没说如果补丁不能完成升级时处理办法(比如return -1)等。
将输入数据映射到一个长宽为结束版本-起始版本+1的邻接矩阵中,将当前行的不为零的值的列作为下一个当前行,进行遍历,递归求出最小花费。说也说不清楚。
java代码如下:
package WANMEI;
import java.io.BufferedInputStream;
import java.util.Scanner;
public class T2 {
public static int[] seq;
public static int[] currentSeq;
public static int count = Integer.MAX_VALUE;
public static int currCount = 0;
public static int len = 0;
public static int[][] in;
public static int steps = 0;
public static int currSteps = 0;
public static int start = 0;
public static int end = 0;
public static void main(String[] args) {
Scanner scanner = new Scanner(new BufferedInputStream(System.in));
start = scanner.nextInt();
end = scanner.nextInt();
// if(start>=end)
len = end - start + 1;
seq = new int[len*2];
currentSeq = new int[len*2];
in = new int[len][len];
// while (scanner.hasNext()) {
for(int i = 0 ; i < 6 ; i++){
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
in[a - start][b - start] = c;
}
next(0, 0);
for(int i = 0 ; i < steps ; i++)
System.out.print((seq[i]+start)+"->");
System.out.print((end+"("+count+")"));
}
public static void next(int row, int col) {
if (col==len-1) {
if (in[row][col] + currCount < count) {
count = in[row][col] + currCount;
seq = currentSeq.clone();
steps = currSteps;
}
} else
for (int i = col; i < len; i++) {
int value = in[row][i];
if (value > 0) {
currCount += value;
currentSeq[currSteps++] = row;
next(i, i);
currSteps--;
currCount -= value;
}
}
}
}
不过只通过了75%的测试样例,题目中信息太少,我也不知道剩下的测试样例什么原因出的错误。