日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

最短路徑問(wèn)題——分支限界法

 頭號(hào)碼甲 2022-09-19 發(fā)布于北京

In the N×M grid array, there is a start-cell, name A, and a end-cell, name B. The problem is finding the shortest routing scheme (i.e. the shortest path) from A to B. When wiring, it can only follow a straight line or a right angle, not a diagonal line. Black cells represent blocked squares that cannot be passed. Shown as the follow picture, in the 3×3 grid array, the length of shortest path from A to B is 6. A tow dimension array can represent the grid array, in which, ordinary cell is presented by 0, and black cell is represented by 1.

sample.png

Input:

Input N and M at first line (no more than 100), next input N×M array, at last input A's row coordinate, A's column coordinate, B's row coordinate, B's column coordinate.

Output:

print out length of the shortest path from A to B. If no path from A to B, then print 0.

Input Example:

Here is a set of inputs. For example:

3 3
0 1 0
0 1 0
0 0 0
0 0 0 2
 

Output Example:

Here is a set of outputs. For example:

6
 
代碼:
  1 package work7;
  2 
  3 import java.util.Scanner;
  4 
  5 /**
  6 *@author Noble4
  7 *@version 2020年12月5日 下午8:46:42
  8 *@package work7
  9 **/
 10 public class test2 {
 11     static int MAX_VALUE = 100;
 12     int row;int line;
 13     int[][] Figure;int[][] Tags;//記錄到走過(guò)的點(diǎn)的步數(shù)
 14     //移動(dòng)偏向(-1向左,1向右)
 15     int DIRECTION;
 16     //起點(diǎn)和終點(diǎn)的坐標(biāo)
 17     int x1;int y1;int x2;int y2;
 18     
 19     
 20     
 21     public void Init() {
 22         Scanner sr = new Scanner(System.in);
 23         row = sr.nextInt();
 24         line = sr.nextInt();
 25         Figure = new int[row][line];
 26         for (int i = 0; i < row; i++) {
 27             for (int j = 0; j < line; j++) {
 28                 Figure[i][j] = sr.nextInt();
 29             }
 30         }        
 31         x1 = sr.nextInt();
 32         y1 = sr.nextInt();
 33         x2 = sr.nextInt();
 34         y2 = sr.nextInt();       
 35         Tags = new int[row][line];
 36         //初始化記錄步數(shù)的數(shù)組
 37         for (int i = 0; i < row; i++) {
 38             for (int j = 0; j < line; j++) {
 39                 //MAX_VALUE代表沒(méi)走過(guò)
 40                 Tags[i][j] = MAX_VALUE;
 41             }
 42         }
 43         //初始化移動(dòng)偏向
 44         if(y1 < y2) {
 45             DIRECTION = 1;
 46         }else if(y1 > y2){
 47             DIRECTION = -1;
 48         }else {
 49             DIRECTION = 0;
 50         }
 51         Figure[x1][y1] = 1;
 52         Tags[x1][y1] = 0;
 53     }
 54     
 55     //單點(diǎn)向周圍分支
 56     public void Branch(int x,int y,int D) {
 57         //限界
 58         if(Tags[x][y]>=D) {
 59             Tags[x][y] = D;
 60         }else {
 61             return;
 62         }
 63         //使用移動(dòng)偏好
 64         if(y <= y1 && DIRECTION == 1) {
 65             //越界判斷應(yīng)該在前面不然后面的Figure[x+1][y] == 0可能報(bào)錯(cuò)
 66             if(!isBorder(x+1, y) && Figure[x+1][y] == 0) {
 67                 Branch(x+1,y,D+1);
 68             }
 69             if( !isBorder(x, y+1) && Figure[x][y+1] == 0) {
 70                 Branch(x,y+1,D+1);
 71             }
 72             if(!isBorder(x-1, y) && Figure[x-1][y] == 0) {
 73                 Branch(x-1,y,D+1);
 74             }            
 75         }else if(y >= y1 && DIRECTION == -1) {
 76             if(!isBorder(x-1, y) && Figure[x-1][y] == 0) {
 77                 Branch(x-1,y,D+1);
 78             }
 79             if(!isBorder(x+1, y) && Figure[x+1][y] == 0) {
 80                 Branch(x+1,y,D+1);
 81             }
 82             if(!isBorder(x, y-1) && Figure[x][y-1] == 0) {
 83                 Branch(x,y-1,D+1);
 84             }            
 85         }else {
 86             if(!isBorder(x+1, y) && Figure[x+1][y] == 0) {
 87                 Branch(x+1,y,D+1);
 88             }
 89             if(!isBorder(x-1, y) && Figure[x-1][y] == 0) {
 90                 Branch(x-1,y,D+1);
 91             }
 92             if(!isBorder(x, y+1) && Figure[x][y+1] == 0) {
 93                 Branch(x,y+1,D+1);
 94             }
 95             if(!isBorder(x, y-1) && Figure[x][y-1] == 0) {
 96                 Branch(x,y-1,D+1);
 97             }        
 98         }        
 99     }
100     
101     //判斷是否超過(guò)邊界
102     public boolean isBorder(int x,int y) {
103         if(x<0||y<0||x >= row||y >= line) {
104             return true;
105         }else {
106             return false;
107         }        
108     }
109     
110     public static void main(String[] args) {
111         test2 test2 = new test2();
112         test2.Init();
113         test2.Branch(test2.x1, test2.y1, 0);
114         System.out.println(test2.Tags[test2.x2][test2.y2]);
115         for (int[] ints : test2.Tags) {
116             for (int item : ints) {
117                 System.out.print(item + "   ");
118             }
119             System.out.println();
120         }
121     }
122 }

用例:

7 7
0 0 0 0 0 0 0
0 0 0 1 0 0 0
0 0 0 1 0 1 0
0 0 0 1 0 1 0
0 0 0 1 0 1 0
0 0 0 0 0 1 0
0 0 0 0 0 0 0
1 1 4 6

結(jié)果截圖:

問(wèn)題:

  1. 嘗試了移動(dòng)偏好,使用中存在問(wèn)題,某些特殊情況會(huì)得不到最優(yōu)路徑
  2. 不能在找到最優(yōu)路徑后停止所有尋找

希望有朋友指點(diǎn)迷津!

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多