图 2
代码实现如下(该实现方法基于思路一):
packageorg.min.algorithm;
importjava.util.ArrayList;
importjava.util.LinkedList;
importjava.util.List;
importjava.util.Random;
importjava.util.Scanner;
publicclassMaxWeightRoad
{
publicstatic void main(String[] args)
{
intm = 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 = newint[m][n];//创建m行n列的棋盘,存放格中的权值
Random random =newRandom();
//初始化棋盘中的格子,权值为随机生成的整数
for(inti = 0; i< m; i++)
for(intj = 0; j< n; j++)
{
checkerboard[i][j] = random.nextInt(30) -10;//随机生成-10~20之间的整数
}
//构建棋盘的n个有向图,用Node数据结构代表每个格子
Listroots =newArrayList();
for(inti = 0; i<checkerboard[0].length;i++)
{
introw = 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;
intweight =0;
for(List nl: roadslist)
{
inttemp =0;
for(Node nd: nl)
{
temp += nd.getWeight();
}
if(weight< temp)
{
weight = temp;
mn = nl;
}
}
//打印结果
intmaxweight= 0;
for(Node de: mn)
{
maxweight +=de.getWeight();
System.out.print(de.toString() + "\t");
}
System.out.println("\n最长路径权值之和为:"+maxweight);
}
//深度优先搜索求权值之和最长的路径
publicstatic void getMaxRoad(Node node, List list)
{
intmaxweight= 0;
Listtemplist =newLinkedList();
templist.add(node);
while()
}
//创建有向图
publicstatic void createGraph(Node node,int[][]checkerboard)
{
//得到棋盘的边界
intheight =checkerboard[0].length;
intwidth =checkerboard.length;
//获取当前格子的所在的行和列
introw =node.getRow();
intcow =node.getCow();
inttemprow =row;
inttempcow =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);
}
}
}
classNode
{
privateintrow;//格子在棋盘中第几行
privateintcow;//格子在棋盘中第几列
privateintweight;//格子中的权值
privateList branch;
publicNode(introw, intcow, intweight)
{
this.row=row;
this.cow=cow;
this.weight=weight;
this.branch=newLinkedList();
}
publicintgetRow()
{
returnrow;
}
publicintgetCow()
{
returncow;
}
publicintgetWeight()
{
returnweight;
}
publicListgetBranch()
{
returnbranch;
}
publicStringtoString()
{
return"(" +row+","+cow+")";
}
}