【发布时间】:2020-09-19 09:11:37
【问题描述】:
网络系统被定义为二维网格。网格的每个单元都有一个与之相关的路由系数。您需要从左上角向右下角发送一个数据包。
一个数据包只能在四个方向上传播——上、下、左和右,并且只有在单元格不超出边界的情况下。一个数据包需要 |C[x, y] - C[x', y']| 的能量从单元格 (x,y) 移动到单元格 (x', y'),其中 |x|表示 x 的绝对值。
数据包在任何路径上所需的努力定义为数据包沿该路径所需的最大能量。你的任务是找出数据包从左上角到右下角遍历网络所需的最小努力。
例如,数据包在给定的网格中传输,行数 N = 3,列数 M = 4。如下所述 -
{{5,1,3,2},
{7,4,1,8},
{6,7,5,9}}
假设数据包从 5 → 1 → 4 → 7 → 6 → 7 → 5 → 9 传输。这里每个转换所需的相应能量分别为 4、3、3、1、1、2、4。因此,路径所需的努力是 4。
输入格式
第一行包含两个用空格分隔的整数,N 和 M 分别表示行数和列数。接下来是 N 行。每行包含 M 个整数,表示
网格的行。
约束
1<=N,M<=300
1<C(i,j)<1000000
输出格式
在单行输出中,尽可能少地打印。
这是我的解决方案,但在某些测试中它会超时,所以任何人都可以提出更好的解决方案吗?
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
public static int getMinEffort(List<List<Integer>> C) {
Node start = new Node(new Cell(0,0),new ArrayList<Cell>(),0);
ArrayList<Node> nodes = new ArrayList<>();
nodes.add(start);
Cell destination = new Cell(C.size()-1,C.get(0).size()-1);
while(true){
Node active = nodes.get(0);
nodes.remove(active);
//break condition
if(active.getCell().equals(destination)){
return active.getEffort();
}
//checking neighbours
active.getPath().add(new Cell(active.getCell().getX(),active.getCell().getY()));
if(active.getCell().getX()>0){
Cell neigh = new Cell(active.getCell().getX()-1,active.getCell().getY());
if(!active.getPath().contains(neigh)) {
int effort = C.get(active.getCell().getX()).get(active.getCell().getY()) - C.get(neigh.getX()).get(neigh.getY());
if(effort<0)effort*=-1;
if(effort<active.getEffort()){
effort = active.getEffort();
}
nodes.add(new Node(neigh,active.getPath(),effort));
}
}
if(active.getCell().getX()<C.size()-1){
Cell neigh = new Cell(active.getCell().getX()+1,active.getCell().getY());
if(!active.getPath().contains(neigh)) {
int effort = C.get(active.getCell().getX()).get(active.getCell().getY()) - C.get(neigh.getX()).get(neigh.getY());
if(effort<0)effort*=-1;
if(effort<active.getEffort()){
effort = active.getEffort();
}
nodes.add(new Node(neigh,active.getPath(),effort));
}
}
if(active.getCell().getY()>0){
Cell neigh = new Cell(active.getCell().getX(),active.getCell().getY()-1);
if(!active.getPath().contains(neigh)) {
int effort = C.get(active.getCell().getX()).get(active.getCell().getY()) - C.get(neigh.getX()).get(neigh.getY());
if(effort<0)effort*=-1;
if(effort<active.getEffort()){
effort = active.getEffort();
}
nodes.add(new Node(neigh,active.getPath(),effort));
}
}
if(active.getCell().getY()<C.get(0).size()-1){
Cell neigh = new Cell(active.getCell().getX(),active.getCell().getY()+1);
if(!active.getPath().contains(neigh)) {
int effort = C.get(active.getCell().getX()).get(active.getCell().getY()) - C.get(neigh.getX()).get(neigh.getY());
if(effort<0)effort*=-1;
if(effort<active.getEffort()){
effort = active.getEffort();
}
nodes.add(new Node(neigh,active.getPath(),effort));
}
}
Collections.sort(nodes);
}
}
static class Node implements Comparable<Node>{
private Cell cell;
private List<Cell> path = new ArrayList<>();
private int Effort;
public Node(Cell cell, List<Cell> path, int effort) {
this.cell = cell;
this.path = path;
Effort = effort;
}
@Override
public int compareTo(Node o) {
if (o.getEffort()>Effort){
return -1;
}else if(o.getEffort()==Effort){
return 0;
}else {
return 1;
}
}
public Cell getCell() {
return cell;
}
public void setCell(Cell cell) {
this.cell = cell;
}
public List<Cell> getPath() {
return path;
}
public void setPath(List<Cell> path) {
this.path = path;
}
public int getEffort() {
return Effort;
}
public void setEffort(int effort) {
Effort = effort;
}
}
static class Cell {
private int x;
private int y;
public Cell(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Cell)) return false;
Cell cell = (Cell) o;
return x == cell.x &&
y == cell.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
}
}
public class Solution {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
int r = scanner.nextInt();
int c = scanner.nextInt();
scanner.nextLine();
List<List<Integer>> C = new ArrayList<>(r);
for(int i = 0;i<r;i++){
ArrayList<Integer> col = new ArrayList<>(c);
String input = scanner.nextLine();
String[] num = input.split(" ");
for(int j =0;j<c;j++){
col.add(Integer.parseInt(num[j]));
}
C.add(col);
}
int result = Result.getMinEffort(C);
System.out.println(result);
}
}
【问题讨论】: