forked from google/codeu_coding_assessment_2017
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackTrackingTravel.java
More file actions
24 lines (23 loc) · 849 Bytes
/
BackTrackingTravel.java
File metadata and controls
24 lines (23 loc) · 849 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.*;
public class BackTrackingTravel{
public static void main(String[] args){
Scanner console = new Scanner(System.in);
System.out.println("Enter (x, y) coordinates as two integers separated by a space: ");
int x = console.nextInt();
int y = console.nextInt();
backTrackTravel(x , y);
}
public static void backTrackTravel( int targetX, int targetY){
explore(targetX , targetY, 0 , 0, "moves: ");
}
private static void explore(int targetX, int targetY, int currX, int currY, String path){
if (currX == targetX && currY == targetY){
System.out.println(path);
}
else if( currX <= targetX && currY <= targetY){
explore(targetX, targetY, currX + 1, currY, path + " E");
explore(targetX, targetY, currX + 1, currY + 1, path + " NE");
explore(targetX, targetY, currX, currY + 1, path + " N");
}
}
}