I am presented with a problem where I am given a room of size n, and a number of steps that I can take to get across the room. The room will always be an integer size, and so will the steps. The same step can be taken multiple times. My goal is to find the total number of possible ways to cross the room without going past the size of the room. An example of user input would be
5 9 1 3 5
Where the first integer, 5, is the size of the room, and 9 1 3 5 are the possible steps I can take. The function/algorithm would return something like this
5 //Room size
1 1 1 1 1
1 1 3
1 3 1
3 1 1
5
Conceptually, I am very aware of what is going on, I have made note that the function appears to start with the lowest values and build from there. The problem I am having is getting this from my head, into working code, any tips, pointers, or code snippets would be greatly appreciated! Thanks in advanced to any help anyone may be able to offer.
Current Code that I have been working on / with
public static void main(String[] args) {
Scanner Scan = new Scanner(System.in);
int ArraySize;
int[] num = new int[100];
int temp;
int i, n, j;
int roomLength;
System.out.println("Size of Room: ");
roomLength = Scan.nextInt();
System.out.println("\nTotal numbers that will be entered: ");
n = Scan.nextInt();
System.out.println("\nNumbers:");
for (i = 0 ; i < n; i++){
num[i] = Scan.nextInt();
}
for (j = 1; j <= n; j++) {
for (i = 0; i < n-1; i++) {
temp = num[i];
num[i] = num[i+1];
num[i+1] = temp;
uniqueCombinations(num, n, roomLength);
}
}
}
public static int uniqueCombinations(int num[], int n, int rl){
int i;
int value = 0;
int placeHolder;
for (i = 0 ; i < n ; i++){
value += num[i];
placeHolder = i;
if(value == rl){
for (i = 0 ; i <= placeHolder; i++){
System.out.print(num[i]);
}
System.out.println("");
break;
}
}
return 0;
}
}
example run:
Size of Room: 10
Total numbers that will be entered: 5
Numbers:1 2 3 4 5
2134
2314
2341
451
451
541
514
1234
I am not sure how to account for the situations such as 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 2 etc...