所以这是本周早些时候完成的家庭作业,但我无法及时完成。
这是最初的“map.txt”文件,其中包含我们执行此项目所需的所有信息。:
let collection: [Int] = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5]
let random: Int = Int(arc4random_uniform(UInt32(collection.count)))
let selection: Int = collection[random]
这是我的“MapPoints”
的java文件Washington, D.C.|Norfolk|208
Washington, D.C.|Baltimore|39
Washington, D.C.|Philadelphia|235
Washington, D.C.|Pittsburgh|251
Washington, D.C.|Charleston, W.VA|380
Washington, D.C.|Charlotte|388
Washington, D.C.|Knoxville|512
Washington, D.C.|Cincinnati|523
Washington, D.C.|Richmond|108
Norfolk|Richmond|99
Knoxville|Nashville|177
Knoxville|Charlotte|231
Knoxville|Cincinnati|254
Knoxville|Raleigh|362
Knoxville|Charleston, SC|403
Charleston, SC|Myrtle Beach|98
Charleston, SC|Savannah|108
Charleston, SC|Charlotte|218
Charleston, W.VA|Pittsburgh|221
Charleston, W.VA|Louisville|257
Charleston, W.VA|Cleveland|260
Charleston, W.VA|Charlotte|314
Charleston, W.VA|Richmond|331
Orlando|Daytona Beach|58
Orlando|Tampa|84
Orlando|Lake City|160
Orlando|Miami|236
Tallahassee|Lake City|111
Tallahassee|Montgomery|208
Tallahassee|Tampa|252
Tallahassee|Mobile|253
Tallahassee|Atlanta|275
Atlanta|Birmingham|149
Atlanta|Montgomery|161
Atlanta|Knoxville|230
Atlanta|Charlotte|249
Atlanta|Nashville|252
Atlanta|Savannah|256
Atlanta|Lake City|292
Atlanta|Charleston, SC|329
Atlanta|Jacksonville|354
Atlanta|Memphis|394
Memphis|Little Rock|138
Memphis|Nashville|208
Memphis|Jackson|212
Memphis|Birmingham|241
Memphis|Springfield|281
Memphis|St. Louis|283
Jacksonville|Raleigh|536
Jacksonville|Charlotte|399
Jacksonville|Tampa|190
Jacksonville|Savannah|138
Jacksonville|Daytona Beach|99
Jacksonville|Lake City|65
Lake City|Tampa|169
Miami|Tampa|266
Miami|Key West|156
Miami|Daytona Beach|270
Kansas City|Topeka|65
Kansas City|Springfield|170
Kansas City|Wichita|183
Kansas City|Des Moines|197
Wilmington|Norfolk|259
Wilmington|Richmond|255
Wilmington|Myrtle Beach|71
Grand Forks|Fargo|76
Grand Forks|Minot|210
Grand Forks|Duluth|268
Duluth|Madison|340
Des Moines|Madison|373
Milwaukee|Madison|78
Chicago|Milwaukee|91
Chicago|Madison|152
Chicago|Indianapolis|177
Chicago|Grand Rapids|190
Chicago|St. Louis|302
Chicago|Cleveland|345
Chicago|Des Moines|362
Chicago|Nashville|473
Chicago|Kansas City|557
Chicago|Detroit|300
Detroit|Grand Rapids|157
Detroit|Cleveland|173
Detroit|Columbus|206
Detroit|Cincinnati|260
Detroit|Indianapolis|283
Detroit|Buffalo, NY|417
Buffalo, NY|Cleveland|194
Buffalo, NY|Binghamton|220
Buffalo, NY|Albany|289
Binghamton|Albany|137
Cincinnati|Louisville|102
Cincinnati|Indianapolis|108
Cincinnati|Columbus|109
Cincinnati|Columbus|202
Cincinnati|St. Louis|353
Montgomery|Birmingham|92
Montgomery|Mobile|171
Montgomery|Jackson|251
New York City|Hartford|118
New York City|Albany|160
New York City|Harrisburg|174
New York City|Binghamton|199
New York City|Cleveland|481
New York City|Philadelphia|99
Harrisburg|Philadelphia|105
Baltimore|Philadelphia|98
Boston|Hartford|101
Boston|Portland, ME|103
Boston|Albany|177
Denver|Cheyenne|102
Denver|Lamar|206
Denver|Oakley|247
Denver|North Platte|290
Denver|Cortez|411
Denver|Albuquerque|457
Denver|Salt Lake City|529
Denver|St. George|648
Portland, ME|Bangor|132
Portland, OR|Eugene|115
Portland, OR|Seattle|180
Portland, OR|Riley|268
Portland, OR|Crescent City|346
Portland, OR|Spokane|400
Portland, OR|Boise|439
Reno|Winnamucca|164
Reno|Yosemite Village|213
Reno|Ely|331
Reno|Riley|342
Boise|Riley|210
Boise|Twin Falls|123
Boise|Winnamucca|257
Boise|Spokane|379
Boise|Seattle|499
Spokane|Seattle|276
Bismarck|Minot|106
Bismarck|Fargo|194
Bismarck|Pierre|209
Bismarck|Rapid City|358
Bismarck|Billings|418
Bismarck|Malta|430
Rapid City|Pierre|174
Rapid City|Buffalo, WY|216
Rapid City|Cheyenne|290
Rapid City|Sioux Falls|350
Phoenix|Tucson|112
Phoenix|Flagstaff|141
Phoenix|Springerville|236
Phoenix|San Diego|368
Phoenix|El Paso|404
Los Angeles|Phoenix|393
Los Angeles|San Diego|118
Los Angeles|Barstow|138
Los Angeles|Yosemite Village|313
Los Angeles|Reno|522
San Francisco|Los Angeles|384
San Francisco|Reno|228
San Francisco|Barstow|442
San Francisco|Yosemite Village|193
San Francisco|Crescent City|371
San Francisco|Eugene|540
Riley|Eugene|230
Las Vegas|St. George|122
Las Vegas|Barstow|155
Las Vegas|Ely|282
Las Vegas|Phoenix|288
Las Vegas|Reno|442
Las Vegas|Salt Lake City|423
Salt Lake City|Twin Falls|222
Salt Lake City|Rawlins|294
Salt Lake City|Ely|294
Salt Lake City|St. George|305
Salt Lake City|Cortez|348
Salt Lake City|Winnamucca|362
Salt Lake City|Helena|482
Salt Lake City|Old Faithful|482
Raleigh|Wilmington|127
Raleigh|Richmond|156
Raleigh|Norfolk|170
Cleveland|Pittsburgh|141
Cleveland|Columbus|146
Cleveland|Des Moines|677
San Antonio|Eagle Pass|142
San Antonio|Laredo|154
San Antonio|Houston|201
San Antonio|Brownsville|281
San Antonio|Big Spring|298
San Antonio|El Paso|552
San Antonio|Dallas|273
Dallas|Oklahoma City|208
Dallas|Houston|237
Dallas|Big Spring|266
Dallas|Springfield|282
Dallas|Little Rock|315
Dallas|Amarillo|367
Dallas|Jackson|417
Dallas|New Orleans|509
Omaha|Des Moines|143
Omaha|Topeka|163
Omaha|Sioux Falls|175
Omaha|Kansas City|194
Omaha|North Platte|298
Omaha|Liberal|489
Omaha|Rapid City|539
Minneapolis|Duluth|152
Minneapolis|Fargo|242
Minneapolis|Sioux Falls|247
Minneapolis|Madison|260
Minneapolis|Des Moines|286
Minneapolis|Pierre|399
Minneapolis|Omaha|409
Minneapolis|St. Louis|540
New Orleans|Mobile|153
New Orleans|Jackson|183
New Orleans|Birmingham|354
New Orleans|Houston|363
Buffalo, WY|Old Faithful|296
Buffalo, WY|Billings|168
Buffalo, WY|Rawlins|236
Old Faithful|Rawlins|320
Rawlins|Cheyenne|146
North Platte|Cheyenne|215
North Platte|Oakley|158
North Platte|Pierre|264
Charlotte|Raleigh|175
Charlotte|Myrtle Beach|180
Charlotte|Wilmington|188
Nashville|Louisville|180
Nashville|St. Louis|312
Nashville|Birmingham|192
Jackson|Birmingham|234
Pittsburgh|Harrisburg|218
Pittsburgh|Columbus|184
Indianapolis|St. Louis|244
Indianapolis|Columbus|179
Indianapolis|Louisville|124
Brownsville|Houston|365
Brownsville|Laredo|204
Eagle Pass|Laredo|124
Billings|Malta|206
Billings|Helena|243
Helena|Old Faithful|216
Helena|Malta|293
Helena|Missoula|116
Spokane|Missoula|198
Twin Falls|Missoula|401
Twin Falls|Ely|255
Little Rock|Springfield|219
Little Rock|Jackson|267
Little Rock|Houston|315
Little Rock|Oklahoma City|336
Little Rock|St. Louis|350
St. Louis|Des Moines|387
St. Louis|Louisville|259
St. Louis|Kansas City|250
St. Louis|Springfield|224
Malta|Minot|457
Fargo|Minot|268
Fargo|Duluth|259
Fargo|Sioux Falls|253
Flagstaff|St. George|286
Flagstaff|Cortez|263
Flagstaff|Barstow|348
San Diego|Barstow|181
El Paso|Big Spring|344
El Paso|Amarillo|412
El Paso|Eagle Pass|484
El Paso|Houston|746
El Paso|Tucson|324
El Paso|Albuquerque|270
Albuquerque|Lamar|393
Albuquerque|Flagstaff|325
Albuquerque|Amarillo|296
Albuquerque|Cortez|273
Albuquerque|Springerville|233
Albuquerque|Big Spring|418
Albuquerque|Liberal|421
Amarillo|Liberal|164
Amarillo|Oklahoma City|259
Amarillo|Big Spring|224
Amarillo|Lamar|228
Liberal|Lamar|168
Liberal|Oakley|143
Liberal|Wichita|209
Topeka|Wichita|138
Topeka|Oakley|289
Topeka|Liberal|361
Oklahoma City|Wichita|156
Oklahoma City|Liberal|256
Oklahoma City|Springfield|283
这是我的“节点”
的java文件public class MapPoint
{
private String StartingPoint = null;
private String EndingPoint = null;
private int distance = 0;
private boolean visited = false;
private boolean backEdge = false;
public MapPoint(String sp, String ep, int d)
{
StartingPoint = sp;
EndingPoint = ep;
distance = d;
visited = false;
backEdge = false;
}
public String getStartPoint()
{
return StartingPoint;
}
public String getEndPoint()
{
return EndingPoint;
}
public int getDistance()
{
return distance;
}
public boolean getVisit()
{
return visited;
}
public boolean getBackEdge()
{
return backEdge;
}
public void setStartPoint(String input)
{
StartingPoint = input;
}
public void setEndPoint(String input)
{
EndingPoint = input;
}
public void setDistance(int input)
{
distance = input;
}
public void setVisit(boolean b)
{
visited = b;
}
public void setBackEdge(boolean t)
{
backEdge = t;
}
public String toString()
{
String output = "Starting point: " + getStartPoint() + "\nEnding point: " + getEndPoint() + "\nDistance: " + getDistance();
return output;
}
}
这是我的“NodeList”
的java文件public class Node
{
private MapPoint data = null;
private Node next = null;
private Node previous = null;
private boolean visited = false;
public Node(MapPoint inData, Node inNext, Node inPrev)
{
data = inData;
next = inNext;
previous = inPrev;
visited = false;
}
/**
* getNext returns what's in the next position
* @return The next Node
*/
public Node getNext()
{
return next;
}
/**
* setNext sets the data to be stored in the next slot in the linked list
* @param inNext The next Node
*/
public void setNext(Node inNext)
{
next = inNext;
}
/**
* getPrevious returns the previous Node
* @return The previous Node
*/
public Node getPrevious()
{
return previous;
}
/**
* setPrevious sets the data to be stored in the previous slot in the linked list
* @param inPrev The previous Node
*/
public void setPrevious(Node inPrev)
{
previous = inPrev;
}
public void setVisit(boolean b)
{
visited = b;
}
public boolean getVisit()
{
return visited;
}
/**
* getData returns the data stored in that Node
*/
public MapPoint getData()
{
return data;
}
}
基本上,我们需要同时进行深度优先搜索和广度优先搜索(并制作MST,但这比这部分更容易)。
这是我的程序驱动程序的代码:
import java.util.ArrayList;
/**
* The NodeList.java class controls the order of the linked list and can add and remove data from the list
*/
public class NodeList
{
Node head = null;
/**
* add adds data to the end of the linked list
* @param
*/
public void add(MapPoint data)
{
if(head == null)
head = new Node(data, null, null);
else
{
Node current = head;
while(current.getNext() != null)
{
current = current.getNext();
}
Node temp = new Node(data, null, current);
current.setNext(temp);
}
}
/**
* remove removes data from the linked list and moves other data around to fill the missing data's spot
* @param
* @return
*/
public MapPoint remove(MapPoint point)
{
Node target = search(point);
if(target == null)// not there/no items in list
return null;
else// there/more items in list
{
if(target == head)// first item in list
{
if(target.getNext() == null)// What if only one item in list?
{
head = null;
return target.getData();
}
else// first item and more than one item in the list
{
target.getNext().setPrevious(null);
head = target.getNext();
return target.getData();
}
}
else// not first item in list
{
target.getPrevious().setNext(target.getNext());
if(target.getNext() != null)// if item is at end of list
{
target.getNext().setPrevious(target.getPrevious());
return target.getData();
}
return target.getData();
}
}
}
/**
* search searches the linked list
* @param point
* @return
*/
public Node search(MapPoint point)
{
Node current = head;
while(current != null)
{
if(current.getData().getStartPoint().equals(point.getStartPoint()))
return current;
current = current.getNext();
}
return null;
}
public Node search(String sp){
Node current = head;
while(current != null){
if(current.getData().getStartPoint().equals(sp))
return current;
current = current.getNext();
}
return null;
}
public ArrayList<MapPoint> searchAll(String sp){
ArrayList<MapPoint> mp = new ArrayList<>();
Node current = head;
while(current != null){
if(current.getData().getStartPoint().equals(sp) && current.getData().getVisit() == false)
mp.add(current.getData());
else if(current.getData().getEndPoint().equals(sp) && current.getData().getVisit() == false){
current.getData().setEndPoint(current.getData().getStartPoint());
current.getData().setStartPoint(sp);
mp.add(current.getData());
}
current = current.getNext();
}
return mp;
}
/**
* numElements counts the number of elements in the linked list
* @return
*/
public int numElements()
{
Node current = head;
int count = 0;
if(current == null)
return 0;
while(current != null)
{
count++;
current = current.getNext();
}
return count;
}
/**
* toString returns information about what is inside the NodeList
* @return A String containing information about the contents of the NodeList
*/
public String toString()
{
Node current = head;
String output = "";
while(current != null)
{
output = output + current.getData() + "\n";
current = current.getNext();
}
return output;
}
}
这是我为DFS制作的对象的代码:
import java.util.*;
import java.lang.*;
import java.io.*;
public class MapDriver
{
private static int cities = 1;
private static int totWeight = 0;
public static NodeList createMap()
{
Scanner input;
String filename = "map.txt";
NodeList map = new NodeList();
try
{
input = new Scanner(new File(filename));
String[] values;
while(input.hasNext())
{
values = input.nextLine().trim().split("\\|");
map.add(new MapPoint(values[0], values[1], Integer.parseInt(values[2])));
}
}
catch(FileNotFoundException e)
{
System.out.println("File not found.");
}
catch(NumberFormatException e)
{
System.out.println("Number format exception.");
}
return map;
}
public static void main(String[] args)
{
String start = "Grand Forks";
//Depth First Search
System.out.println("City: " + start);
System.out.println("Total Weight: " + totWeight);
System.out.println("# of Cities: " + cities);
System.out.println("");
map = createMap();
DFS dfs = new DFS(map);
dfs.drive(start);
}
}
有没有简单的方法可以将我的DFS.java转换为NodeList的工作广度优先搜索,它将覆盖所有114个城市?我无法独自解决这个问题。