Write a program which implements the A* algorithm on an arbitrary map described
ID: 3592230 • Letter: W
Question
Write a program which implements the A* algorithm on an arbitrary map described through “connections” file and “locations” file. The algorithm should allow the exclusion of 1 or more cities in the locations file. Two possible A* algorithm heuristics are required: the “ Straight Line Distance” and the “Fewest Links". Note: The “fewest links”is the path in which the fewest number of connections are needed to get from start city to end city.
INPUT
The user input at runtime consists of the following:
The connections file and the locations file
-Allow the user to specify the full path of each file, for example:
“c:myDocsmyTestFileslocations.txt”
The start city
The end city
The city name(s) that should not be included in the solution path; let the user select 1 or mo re cities that are to be excluded.
The heuristic to use
- Straight line distance, or
- Fewest links
Whether to show just the optimal path foundor a “step-by-step”path to the solution. The step-by-step path to the solution would allow the user to control each step by pressing the “enter” key
OUTPUT
For example if the start city is A1 and the end city is C3 and the user selects to sh ow only the optimal path found , the output might look like
---
OptimalPath:
A1->B1->B2->C3
However if the user selects the “step-by-
step” path to the solution,and the heuristic is the straight line distance,the output might look like
--
Current Path: A1
Distance traveled: 0
Best move is to: B1
Current Path: A1-> B1
Distance traveled: 10
Best move is to: A2
Current Path: A1-> A2
Distance traveled: 9
Best move is to: D1
Current Path: A1->B1->D1
Distance traveled: 18
Best move is to: B2...
Optimal Path:
A1->B1->B2->C3
Distance traveled: 38
SPECIFICATIONS
City names are given as string labels that can be from 1 to 80 characters long.
There will be no more than 200 cities.
There will be no more than 10 direct roads from one city to the next city.
There can be one-way roads. For example the connections file can show that A1 is connected to B2 but the connection from B2 does not include A1.
All city locations are given in (x, y), where x and y are positive integers in the range from 0 to 800.
(0, 0) is the top left of the coordinate space. X is the horizontal position. Y is the vertical position
Program in C++*****
Explanation / Answer
// C++ code to demonstrate the working of find()
#include<iostream>
#include<map> // for map operations
using namespace std;
int main()
{
// declaring map
// of char and int
map< char, int > mp;
// declaring iterators
map<char, int>::iterator it ;
map<char, int>::iterator it1 ;
// inserting values
mp['a']=5;
mp['b']=10;
mp['c']=15;
mp['d']=20;
mp['e']=30;
// using find() to search for 'b'
// key found
// "it" has address of key value pair.
it = mp.find('b');
if(it == mp.end())
cout << "Key-value pair not present in map" ;
else
cout << "Key-value pair present : "
<< it->first << "->" << it->second ;
cout << endl ;
// using find() to search for 'm'
// key not found
// "it1" has address of end of map.
it1 = mp.find('m');
if(it1 == mp.end())
cout << "Key-value pair not present in map" ;
else
cout << "Key-value pair present : "
<< it1->first << "->" << it1->second ;
}
// C++ code to demonstrate the working of lower_bound()
#include<iostream>
#include<map> // for map operations
using namespace std;
int main()
{
// declaring map
// of char and int
map< char, int > mp;
// declaring iterators
map<char, int>::iterator it ;
map<char, int>::iterator it1 ;
map<char, int>::iterator it2 ;
// inserting values
mp['a']=5;
mp['b']=10;
mp['c']=15;
mp['h']=20;
mp['k']=30;
// using lower_bound() to search for 'b'
// key found
// "it" has address of key value pair.
it = mp.lower_bound('b');
if(it == mp.end())
cout << "Key-value pair not present in map" ;
else
cout << "Key-value pair returned : "
<< it->first << "->" << it->second ;
cout << endl ;
// using lower_bound() to search for 'd'
// key not found
// "it1" has address of next greater key.
// key - 'h'
it1 = mp.lower_bound('d');
if(it1 == mp.end())
cout << "Key-value pair not present in map" ;
else
cout << "Key-value pair returned : "
<< it1->first << "->" << it1->second ;
cout << endl;
// using lower_bound() to search for 'p'
// key not found
// "it2" has address of next greater key.
// all keys are smaller, hence returns mp.end()
it2 = mp.lower_bound('p');
if(it2 == mp.end())
cout << "Key-value pair not present in map" ;
else
cout << "Key-value pair returned : "
<< it2->first << "->" << it2->second ;
}
// C++ code to demonstrate the working of upper_bound()
#include<iostream>
#include<map> // for map operations
using namespace std;
int main()
{
// declaring map
// of char and int
map< char, int > mp;
// declaring iterators
map<char, int>::iterator it ;
map<char, int>::iterator it1 ;
map<char, int>::iterator it2 ;
// inserting values
mp['a']=5;
mp['b']=10;
mp['c']=15;
mp['h']=20;
mp['k']=30;
// using upper_bound() to search for 'b'
// key found
// "it" has address of key value pair next to 'b' i.e 'c'.
it = mp.upper_bound('b');
if(it == mp.end())
cout << "Key-value pair not present in map" ;
else
cout << "Key-value pair returned : "
<< it->first << "->" << it->second ;
cout << endl ;
// using upper_bound() to search for 'd'
// key not found
// "it1" has address of next greater key.
// key - 'h'
it1 = mp.upper_bound('d');
if(it1 == mp.end())
cout << "Key-value pair not present in map" ;
else
cout << "Key-value pair returned : "
<< it1->first << "->" << it1->second ;
cout << endl;
// using upper_bound() to search for 'p'
// key not found
// "it2" has address of next greater key.
// all keys are smaller, hence returns mp.end()
it2 = mp.upper_bound('p');
if(it2 == mp.end())
cout << "Key-value pair not present in map" ;
else
cout << "Key-value pair returned : "
<< it2->first << "->" << it2->second ;
}
// C++ code to demonstrate the working of equal_range()
#include<iostream>
#include<map> // for map operations
using namespace std;
int main()
{
// declaring map
// of char and int
map< char, int > mp;
// declaring iterators
pair<map<char, int>::iterator, map<char, int>::iterator> it;
// inserting values
mp['a']=5;
mp['b']=10;
mp['c']=15;
mp['h']=20;
mp['k']=30;
// using equal_range() to search for 'b'
// key found
// 1st element of "it" has the address to lower_bound (b)
// 2nd element of "it" has the address to upper_bound (c)
it = mp.equal_range('b');
cout << "The lower_bound of key is : "
<< it.first -> first << "->" << it.first -> second;
cout << endl;
cout << "The upper_bound of key is : "
<< it.second -> first << "->" << it.second -> second;
cout << endl << endl ;
// using equal_range() to search for 'd'
// key not found
// Both elements of it point to next greater key
// key - 'h'
it = mp.equal_range('d');
cout << "The lower_bound of key is : "
<< it.first -> first << "->" << it.first -> second;
cout << endl;
cout << "The upper_bound of key is : "
<< it.second -> first << "->" << it.second -> second;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.