(UVA) All Walks of length "n" from the first node - Solution

Link to the problem: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=618

From node 1, we need to keep moving through the adjacent nodes in order to achieve the length of walk requested. For this purpose, the solution below used backtracking.


import java.io.*;
import java.util.*;
import java.text.DecimalFormat;  

class Main {
    public int numNodes;
    public int lengthOfWalks;
    public int[][] adjMatrix;
    public ArrayList<ArrayList<Integer>> pathWalks;
    public ArrayList<Integer> path;
    public int[] considered;
   
    public void rec(int row, int walk) {
        if (walk == lengthOfWalks) {
            pathWalks.add((ArrayList<Integer>) path.clone());
            return;
        }
       
        for (int i = 1; i <= numNodes; i++) {
            if (adjMatrix[row][i] == 0 || considered[i] == 1) {
                continue;
            }
            considered[i] = 1;
            path.add(i);
            rec(i, walk+1);
            path.remove(path.size()-1);
            considered[i] = 0;
        }
    }
   
    public void process() throws NumberFormatException, IOException {
        Scanner sc = new Scanner(System.in);
       
        int numCase = 0;
        boolean hasNext = false;
        do {
            if (numCase > 0) {
                System.out.println();
            }
            numNodes = sc.nextInt();
            lengthOfWalks = sc.nextInt();
            adjMatrix = new int[numNodes+1][numNodes+1];
            for (int i = 1; i <= numNodes; i++) {
                for (int j = 1; j <= numNodes; j++) {
                    adjMatrix[i][j] = sc.nextInt();
                }
            }
           
            considered = new int[numNodes+1];
            pathWalks = new ArrayList<>();
            path = new ArrayList<>();
            considered[1] = 1;
            path.add(1);
            rec(1, 0);
           
            if (pathWalks.size() == 0) {
                System.out.println("no walk of length " + lengthOfWalks);
            }
            else {
                for (int i = 0; i < pathWalks.size(); i++) {
                    ArrayList<Integer> tmp = pathWalks.get(i);
                    System.out.print("(");
                    for (int j = 0; j < tmp.size()-1; j++) {
                        System.out.print(tmp.get(j)+",");
                    }
                    System.out.println(tmp.get(tmp.size()-1)+")");
                }
            }
            numCase++;
                       
            hasNext = false;
            if (sc.hasNext()) {
                sc.next(); // ignore -9999
                hasNext = true;
            }
        } while (hasNext);       
       
        return;
    }
   
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();
       
        System.exit(0);
    }
}

Comments

Popular posts from this blog

(Coderbyte) Powers of Two - Solução

(Coderbyte) Dash Insert II - Solução

(CoderByte) Number Search - Solução