java - recursion using a hashmap -


i have array has following numbers

int[] arr = {2,4,3,1,5,6,0,7,8,9,10,11,12,13,14,15}; 

or other order matter. need make possible combinations numbers using recursion satisfying condition next number clubbed present 1 can specific numbers given hashmap: ex when recursion takes 1 next number can {0,4,5,2,6} (from haspmap),and if make 10,the next number can {1,4,5} , on

static hashmap<integer,integer[]> possibleseq = new hashmap<integer,integer[] >(); private static void initialize(hashmap<integer,integer[]> possibleseq) {     possibleseq.put(0,new integer[]{1,4,5});     possibleseq.put(1,new integer[]{0,4,5,2,6});     possibleseq.put(2,new integer[]{1,3,5,6,7});     possibleseq.put(3,new integer[]{2,6,7});     possibleseq.put(4,new integer[]{0,1,5,8,9});     possibleseq.put(5,new integer[]{0,1,2,4,6,8,9,10});     possibleseq.put(6,new integer[]{1,2,3,5,7,9,10,11});     possibleseq.put(7,new integer[]{2,3,6,10,11});     possibleseq.put(8,new integer[]{9,4,5,12,13});     possibleseq.put(9,new integer[]{10,4,5,8,6,12,13,14});     possibleseq.put(10,new integer[]{7,6,5,9,11,15,13,14});     possibleseq.put(11,new integer[]{6,7,10,14,15});     possibleseq.put(12,new integer[]{8,9,13});     possibleseq.put(13,new integer[]{8,9,10,12,14});     possibleseq.put(14,new integer[]{9,10,11,13,15});     possibleseq.put(15,new integer[]{10,11,14});     } 

note: required make possible numbers beginning digit length 1 10. help!

try this, starters:

void findpath(set paths, stack path, int[] nextsteps, set numbersleft) {     if (numbersleft.isempty()) {         //done         paths.add(new arraylist(path));         return;     }      (int step:nextsteps) {         if (numbersleft.contains(step)) {             // can move on             path.push(step);             numbersleft.remove(step);             findpath(paths, path, possiblepaths.get(step), numbersleft);             numbersleft.add(path.pop());         }     }        } 

starting values should empty set, , empty stack, nextsteps identical initial array, , set created initial array. when returns, paths set should filled possible paths.

i haven't tested this, , there bugs more elegant solutions.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -