import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public classMaxWeightRoad
{
public static void main(String[] args)
{
int m = 0, n= 0;//m行n列的棋盤
Scanner in = newScanner(System.in);
System.out.print("請輸入棋盤行數m:");
m = in.nextInt();
System.out.print("請輸入棋盤列數n:");
n = in.nextInt();
int[][]checkerboard = new int[m][n];//創建m行n列的棋盤,存放格中的權值
Random random = newRandom();
//初始化棋盤中的格子,權值爲隨機生成的整數
for(int i = 0; i< m; i++)
for(int j = 0; j< n; j++)
{
checkerboard[i][j] = random.nextInt(30) -10;//隨機生成-10~20之間的整數
}
//構建棋盤的n個有向圖,用Node數據結構代表每個格子
Listroots = newArrayList();
for(int i = 0; i<checkerboard[0].length;i++)
{
int row = 0,cow = i, weight = checkerboard[0][i];
Node node = newNode(row,cow, weight);
createGraph(node, checkerboard);
roots.add(node);
}
List> roadslist = newArrayList>();//保存n個有向圖中權值之和最大的路徑,共n條路徑
//求每個圖的權值之和最長的路徑,並將該路徑保存在roadslist中
for(Noderoot : roots)
{
Listroad = newLinkedList();
getMaxRoad(root, road);
roadslist.add(road);
}
//得到路徑權值之和最大的路徑
Listmn = null;
int weight =0;
for(List nl: roadslist)
{
int temp =0;
for(Node nd: nl)
{
temp += nd.getWeight();
}
if(weight< temp)
{
weight = temp;
mn = nl;
}
}
//打印結果
int maxweight= 0;
for(Node de: mn)
{
maxweight +=de.getWeight();
System.out.print(de.toString() + "\t");
}
System.out.println("\n最長路徑權值之和爲:" +maxweight);
}
//深度優先搜索求權值之和最長的路徑
public static void getMaxRoad(Node node, List list)
{
int maxweight= 0;
Listtemplist = newLinkedList();
templist.add(node);
while()
}
//創建有向圖
public static void createGraph(Node node, int[][]checkerboard)
{
//得到棋盤的邊界
int height =checkerboard[0].length;
int width =checkerboard.length;
//獲取當前格子的所在的行和列
int row =node.getRow();
int cow =node.getCow();
int temprow =row;
int tempcow =cow;
Node tempnode = null;
//兵路x=x+1
temprow = row + 1;
if((temprow< width && temprow >=0) && (tempcow <height && tempcow >= 0))
{
tempnode = newNode(temprow, tempcow,checkerboard[temprow][tempcow]);
node.getBranch().add(tempnode);
createGraph(tempnode,checkerboard);
}
//日路x=x+1,y=y-1
temprow = row + 1;
tempcow = cow - 1;
if((temprow< width && temprow >=0) && (tempcow <height && tempcow >= 0))
{
tempnode = newNode(temprow, tempcow,checkerboard[temprow][tempcow]);
node.getBranch().add(tempnode);
createGraph(tempnode,checkerboard);
}
//日路x=x+1,y=y+1
temprow = row + 1;
tempcow = cow + 1;
if((temprow< width && temprow >=0) && (tempcow <height && tempcow >= 0))
{
tempnode = newNode(temprow, tempcow,checkerboard[temprow][tempcow]);
node.getBranch().add(tempnode);
createGraph(tempnode,checkerboard);
}
//馬路x=x+1,y=y-2
temprow = row + 1;
tempcow = cow - 2;
if((temprow< width && temprow >=0) && (tempcow <height && tempcow >= 0))
{
tempnode = newNode(temprow, tempcow,checkerboard[temprow][tempcow]);
node.getBranch().add(tempnode);
createGraph(tempnode,checkerboard);
}
//馬路x=x+1,y=y+2
temprow = row + 1;
tempcow = cow + 2;
if((temprow< width && temprow >=0) && (tempcow <height && tempcow >= 0))
{
tempnode = newNode(temprow, tempcow,checkerboard[temprow][tempcow]);
node.getBranch().add(tempnode);
createGraph(tempnode,checkerboard);
}
//馬路x=x+2,y=y-1
temprow = row + 2;
tempcow = cow - 1;
if((temprow< width && temprow >=0) && (tempcow <height && tempcow >= 0))
{
tempnode = newNode(temprow, tempcow,checkerboard[temprow][tempcow]);
node.getBranch().add(tempnode);
createGraph(tempnode,checkerboard);
}
//馬路x=x+2,y=y+1
temprow = row + 2;
tempcow = cow + 1;
if((temprow< width && temprow >=0) && (tempcow <height && tempcow >= 0))
{
tempnode = newNode(temprow, tempcow,checkerboard[temprow][tempcow]);
node.getBranch().add(tempnode);
createGraph(tempnode,checkerboard);
}
}
}
class Node
{
private int row;//格子在棋盤中第幾行
private int cow;//格子在棋盤中第幾列
private int weight;//格子中的權值
private List branch;
public Node(int row, int cow, int weight)
{
this.row =row;
this.cow =cow;
this.weight =weight;
this.branch =newLinkedList();
}
public int getRow()
{
return row;
}
public int getCow()
{
return cow;
}
public int getWeight()
{
return weight;
}
public ListgetBranch()
{
return branch;
}
public StringtoString()
{
return "(" +row +"," +cow +")";
}
}