Romania's Cities Breadth-First Search using ma

2019-02-21 03:01发布

问题:

I'm working on a project that referred to Romania cities Breadth-First Search:

romania cities map

I have a function for creating the neighbors:

function [NeighborName PathLength StraightLineDistance] = makeneighbor(x,y,z)
NeighborName=x; 
PathLength=y; % distance from current city to neighbor
StraightLineDistance=z; % i want to use it for a* model

and in my 'main.m' I have defined city* and neighbors:

clc;clear;
city1='Arad'
[neighbor_name neighbor_distance straight_line_distance]=makeneighbor('Zerind',75,374)
[neighbor_name neighbor_distance straight_line_distance]=makeneighbor('Sibiu',140,253)
[neighbor_name neighbor_distance straight_line_distance]=makeneighbor('Timisoara',118,329)

city2='Zerind'
[neighbor_name neighbor_distance straight_line_distance]=makeneighbor('Oradea',71,380)
[neighbor_name neighbor_distance straight_line_distance]=makeneighbor('Arad',75,366)
.
.
.
city20='Neamt'
[neighbor_name neighbor_distance straight_line_distance]=makeneighbor('Iasi',87,226)  

each time compare the city with bucharest

if city*=='bucharest'
break;
else
list= add the city to list.

for next time I need to compare its neighbor with goal and each time if city ~= 'bucharest' add that city to list. Also when find the goal, add goal to list.

Now I need a function to find bucharest and putting previous cities in an array. Automatically find cities and them values. like this:

path1=['arad' 'sibiu' 'fagaras' 'bucharest'];
distance1=[140 99 211];
path2=['arad' 'zerind' 'oradea' 'sibiu' 'fagaras' 'bucharest'];
distance2=[75 71 151 99 211];
path3=['arad' 'zerind' 'oradea' 'sibiu' 'vilcea' 'pitesti' 'bucharest'];
distance3=[75 71 151 80 97 101];
.
.
pathN=[city1:cityN 'bucharest'];
distanceN=[distance1:distanceN]

disp('please explain very simple. im not so expert in matlab, and my primary language is not english. many thanx...')