您的位置:首页 > 其它

【学习笔记】简单寻路算法(低效率)

2017-03-07 07:31 232 查看
存在一个N*N的矩阵,其中有若干位置不可通行,现在要从A点走到B点,请规划出最佳路线(0可通行,1为不可通行,2为路线)

依次输入
计算次数 1 < M <= 10;
矩阵大小N
矩阵点图
A点位置
B点位置

输出
最终矩形点图

范例:
Input:
1
5
0 0 0 1 0
0 1 0 1 0
0 1 0 1 0
0 1 0 1 0
0 1 0 0 0
0 4
4 0

输出:
2 2 2 1 2
2 1 2 1 2
2 1 2 1 2
2 1 2 1 2
2 1 2 2 2

就一个遍历寻路 把每次寻路过程当做一个对象处理, 不走无法走的路,不走走过的路,当找到某条路后 开始计算长度,长度>最长路途时 停止寻路 效率很低 去看看A*算法

import java.util.ArrayList;
import java.util.Scanner;

public class test_2 {
static ArrayList<Point> AllPoints;
static ArrayList<Point> EndPoints;
static int max = 100000000;
static int M;
static int[][] num;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
while(N-->0){
max = 100000000;
AllPoints = new ArrayList<Point>();
M = sc.nextInt();//矩阵大小
num = new int[M][M];
for(int i = 0;i < M;i++){
for(int j = 0;j < M;j++){
num[j][i] = sc.nextInt();
}
}
int x1 = sc.nextInt();
int y1 = sc.nextInt();
int x2 = sc.nextInt();
int y2 = sc.nextInt();
new Recursion(x1,y1,x2,y2,0,new ArrayList<Point>());
for(int i = 0;i < EndPoints.size();i++){
Point p = EndPoints.get(i);
num[p.x][p.y] = 2;
}
for(int i = 0;i < M;i++){
for(int j = 0;j <M;j++){
System.out.print(num[j][i]+" ");
}
System.out.println();
}
}
}
static class Point{
int x;
int y;
Point(int x,int y){
this.x = x;
this.y = y;
}
}
static class Recursion{
ArrayList<Point> LastPoints;
Recursion(int nowX,int nowY,int endX,int endY,int length,ArrayList<Point> nextPoints){
Point point = new Point(nowX, nowY);
this.LastPoints = (ArrayList<Point>) nextPoints.clone();
LastPoints.add(point);
AllPoints.add(point);
if(length < max){
if(nowX > 0){
boolean flag = true;
for(int i = 0;i < LastPoints.size();i++){
Point p = LastPoints.get(i);
if(p.x == nowX - 1 && p.y == nowY){
flag = false;
}
}
if(flag){
if(num[nowX-1][nowY] == 0){
if(nowX - 1 == endX && nowY == endY){
Point endpoint = new Point(endX, endY);
LastPoints.add(endpoint);
EndPoints = LastPoints;
max = length;
return;
}
new Recursion(nowX-1,nowY,endX,endY,lengt
4000
h+1,LastPoints);
}
}
}
if(nowX < M-1){
boolean flag = true;
for(int i = 0;i < LastPoints.size();i++){
Point p = LastPoints.get(i);
if(p.x == nowX + 1 && p.y == nowY){
flag = false;
}
}
if(flag){
if(num[nowX+1][nowY] == 0){
if(nowX + 1 == endX && nowY == endY){
Point endpoint = new Point(endX, endY);
LastPoints.add(endpoint);
EndPoints = LastPoints;
max = length;
return;
}
new Recursion(nowX + 1,nowY,endX,endY,length+1,LastPoints);
}
}
}
if(nowY > 0){
boolean flag = true;
for(int i = 0;i < LastPoints.size();i++){
Point p = LastPoints.get(i);
if(p.x == nowX && p.y == nowY - 1){
flag = false;
}
}
if(flag){
if(num[nowX][nowY - 1] == 0){
if(nowX == endX && nowY - 1 == endY){
Point endpoint = new Point(endX, endY);
LastPoints.add(endpoint);
EndPoints = LastPoints;
max = length;
return;
}
new Recursion(nowX,nowY - 1,endX,endY,length+1,LastPoints);
}
}
}
if(nowY < M-1){
boolean flag = true;
for(int i = 0;i < LastPoints.size();i++){
Point p = LastPoints.get(i);
if(p.x == nowX && p.y == nowY + 1){
flag = false;
}
}
if(flag){
if(num[nowX][nowY + 1] == 0){
if(nowX == endX && nowY + 1 == endY){
Point endpoint = new Point(endX, endY);
LastPoints.add(endpoint);
EndPoints = LastPoints;
max = length;
return;
}
new Recursion(nowX,nowY + 1,endX,endY,length+1,LastPoints);
}
}
}
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: