路徑最優問題 - 演算法
阿新 • • 發佈:2018-11-30
一、問題描述
有一個 n * n 的矩陣,其中有四個2代表研究院,矩陣中 0 表示道路,1 表示不能走,只可以四個方向走,求找到一點(0點)距離四個研究院中最遠的研究院的距離最近。
測試資料: 5 8 8 4 2 0 0 0 0 0 2 0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 1 1 1 1 0 0 0 2 0 0 0 0 0 2 3 3 2 0 1 1 2 1 0 0 0 2 4 4 3 2 0 1 1 0 0 0 0 0 0 1 0 1 2 0 2 10 10 4 0 0 1 0 2 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 1 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 1 0 0 1 0 0 1 0 0 1 0 2 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 0 1 0 1 0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0 0 0 20 20 4 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
二、DFS 版本,必定超時
1.思路:
(1)遍歷所有的 0 點,找到距離每一個2的最近距離,儲存下來。
這裡就比較坑了,必須一個2一個2的找,當找任意一個2時,將其他的2設為0,因為2也可以走。這樣相當於每一個0就要單純的找4個2的最近距離,這樣做是比較慢的。
(2)每走一個0點,求出其最遠距離max。
(3)比較所有的0點的最遠距離max,即可求出最終答案。
2.程式碼
package com.samsung.minroads;
import java.io.*;
import java.util.*;
public class MinRoads {
static int totalRow, totalCol, totalNum, max, minLen;
static int[][] data;
static int[] steps;
static int[][] dir = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } }; // 右,下,左,上
static boolean isEnd;
static Kuang[] kuangList;
static int kuIn;
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(new File("src/minroads.txt"));
int exit = sc.nextInt();
while ((exit--) >= 0) {
totalRow = sc.nextInt();
totalCol = sc.nextInt();
totalNum = sc.nextInt();
// init
data = new int[totalRow][totalCol];
steps = new int[(totalNum + 1)];
kuangList = new Kuang[totalNum + 1];
isEnd = false;
max = 0;
minLen = 1000;
kuIn = 0;
for (int i = 0; i <= totalNum; i++) {
steps[i] = 1000;
}
for (int i = 0; i < totalRow; i++) {
for (int j = 0; j < totalCol; j++) {
data[i][j] = sc.nextInt();
if (data[i][j] == 2) {
Kuang ku = new Kuang();
ku.x = i;
ku.y = j;
kuangList[kuIn++] = ku;
}
}
}
for (int i = 0; i < totalRow; i++) {
for (int j = 0; j < totalCol; j++) {
if (data[i][j] == 0) {
// init
// System.out.println("i:" + i + ",j:" + j);
for (int x = 0; x <= totalNum; x++) {
steps[x] = 1000;
}
max = 0;
for (int curK = 0; curK < kuIn; curK++) {
bfs(i, j, 0, 1, curK);
}
for (int k = 0; k < kuIn; k++) {
max = steps[k] > max ? steps[k] : max;
//System.out.println(steps[k]);
}
// System.out.println("max:" + max);
minLen = max < minLen ? max : minLen;
}
}
}
System.out.println("minLen:" + minLen);
}
}
// 到一個點距離最近
static void bfs(int row, int col, int curVal, int step, int curK) {
// System.out.println(step);
if(step > minLen){ // 剪枝
return;
}
int newRow, newCol;
for (int i = 0; i < 4; i++) {
newRow = row + dir[i][0];
newCol = col + dir[i][1];
if (newRow >= 0 && newRow < totalRow && newCol >= 0
&& newCol < totalCol) {
if (data[newRow][newCol] == 0 || data[newRow][newCol] == 2) {
if (data[newRow][newCol] == 2
&& (newRow == kuangList[curK].x && newCol == kuangList[curK].y)) {
steps[curK] = step < steps[curK] ? step : steps[curK];
return;
}
curVal = data[newRow][newCol];
data[newRow][newCol] = 8; // 8 不能走
bfs(newRow, newCol, curVal, step + 1, curK);
data[newRow][newCol] = curVal;
}
}
}
}
static void print() {
for (int i = 0; i < totalRow; i++) {
for (int j = 0; j < totalCol; j++) {
System.out.print(data[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
}
class Kuang {
int x;
int y;
}
三、BFS 版本
方法一:
1.思路:
(1)遍歷所有的0點,記錄每個元素被遍歷的步數,所以點走完一個0點時就可以得到4個2分別的步數,比較這4個步數獲取最遠距離max即可。
(2)遍歷完所有的0點,就可以獲取到最終的答案。
2.程式碼
package com.samsung.minroads;
import java.io.File;
import java.util.Scanner;
public class MinRoads2 {
static int totalRow, totalCol, totalNum,max,minLen;
static int[][] data,newData;
static int[][] dir = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } }; // 右,下,左,上
static Stone[] queue = new Stone[500]; // 走過的點
static int start,end;
static Stone[] stoneList; // 研究中心
static int stIn;
public static void main(String[] args) throws Exception{
Scanner sc = new Scanner(new File("src/roads.txt"));
int exit = sc.nextInt();
while ((exit--) != 0) {
totalRow = sc.nextInt();
totalCol = sc.nextInt();
totalNum = sc.nextInt();
// init
data = new int[totalRow][totalCol];
newData = new int[totalRow][totalCol];
stoneList = new Stone[totalNum + 1];
stIn = 0;
max = 0;
minLen = 1000;
for (int i = 0; i < totalRow; i++) {
for (int j = 0; j < totalCol; j++) {
data[i][j] = sc.nextInt();
newData[i][j] = data[i][j];
if (data[i][j] == 2) {
Stone stone = new Stone();
stone.x = i;
stone.y = j;
stone.size = 1000; // 方便計算
stoneList[stIn++] = stone;
}
}
}
for (int i = 0; i < totalRow; i++) {
for (int j = 0; j < totalCol; j++) {
if(data[i][j] == 0){
//System.out.println("i" + i + ",j:" + j);
for(int x = 0;x < 500;x++){
queue[x] = null;
}
start = 0;
end = 1;
max = 0;
Stone startSt = new Stone();
startSt.x = i;
startSt.y = j;
startSt.size = 1;
queue[0] = startSt;
bfs(0,1);
for(int k = 0;k < totalNum;k++){
Stone st = stoneList[k];
if(null != st){
max = st.size > max ? st.size : max;
st.size = 1000;
}
}
minLen = max < minLen ? max : minLen;
for (int m = 0; m < totalRow; m++) {
for (int n = 0; n < totalCol; n++) {
data[m][n] = newData[m][n];
}
}
}
}
}
System.out.println("minLen:" + minLen);
}
}
static void bfs(int step,int curStep){
int newRow, newCol;
int temp = 0;
Stone sto = queue[start++];
if(null != sto){
temp = sto.size;
if(temp >= step){
step = step + 1;
}
for (int i = 0; i < 4; i++) {
newRow = sto.x + dir[i][0];
newCol = sto.y + dir[i][1];
if (newRow >= 0 && newRow < totalRow && newCol >= 0
&& newCol < totalCol) {
if(data[newRow][newCol] != 1){
for(int k = 0;k < stIn;k++){
Stone exiStone = stoneList[k];
if(newRow == exiStone.x && newCol == exiStone.y){
exiStone.size = step < exiStone.size ? step : exiStone.size;
stoneList[k] = exiStone;
}
}
Stone st = new Stone();
st.x = newRow;
st.y = newCol;
st.size = step;
queue[end++] = st;
data[newRow][newCol] = 1;
}
}
}
bfs(step,sto.size);
}
}
}
class Stone{
int x;
int y;
int size;
}
方法二
1.思路
從2開始遍歷,記錄每個2到所有0點的距離,也是採用bfs遍歷。這樣做可以少走很多重複的路,複雜度和時間度都相對不負責。