(SPOJ) Labirinto - Solution

Link to the problem: http://br.spoj.com/problems/LAB07/

The solution below uses the Breadth-First Search algorithm to find the answer to this problem. Every time, we have the choice of moving or staying in the same position.
 

import java.io.*;
import java.util.*;

class solucao {
    public int[][] matrix;
    public boolean[][] visited;
    public int[] vi = {-1,0,0,1};
    public int[] vj = {0,-1,1,0};
    public int numRows;
    public int numCols;
   
    public int[][] updateMatrix(int[][] matrix) {
        int[][] matrixTmp = new int[numRows][numCols];
        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j < numCols; j++) {
                matrixTmp[i][j] = (matrix[i][j]+1)%10;
            }
        }
       
        return matrixTmp;
    }
   
    public int bfs(int startI, int startJ) {
        Queue<Coord> queue = new ArrayDeque<Coord>();
        queue.add(new Coord(startI, startJ, 0, 0, matrix));
       
        boolean[][][] visited = new boolean[numRows][numCols][10];
       
        while (queue.size() > 0) {
            Coord curr = queue.poll();
            int currI = curr.i;
            int currJ = curr.j;
            int currDay = curr.day;
            int currDayFinal = curr.dayFinal;
            int[][] currMatrix = curr.matrix;
           
            if (visited[currI][currJ][currDay]) {
                continue;
            }
            visited[currI][currJ][currDay] = true;
           
            //System.out.println(currI + " " + currJ + " " + currDay);
            if (currI == numRows-1 && currJ == numCols-1) {
                return currDayFinal;
            }
           
            int count = 0;
            int[][] updatedMatrix = updateMatrix(currMatrix);
            for (int i = 0; i < 4; i++) {
                int nextI = currI+vi[i];
                int nextJ = currJ+vj[i];
                if (nextI < 0 || nextJ < 0 || nextI >= numRows || nextJ >= numCols || visited[nextI][nextJ][(currDay+1)%10]) {
                    continue;
                }
                if (currMatrix[nextI][nextJ] > currMatrix[currI][currJ]+1) {
                    continue;
                }
                count++;
                queue.add(new Coord(nextI, nextJ, (currDay+1)%10, currDayFinal+1, updatedMatrix));       
            }
           
            // wait
            queue.add(new Coord(currI, currJ, (currDay+1)%10, currDayFinal+1, updatedMatrix));
        }
       
        return -1;
    }
   
    public void process() throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
       
        String line = br.readLine();
        String[] s = line.split(" ");
        numRows = Integer.parseInt(s[0]);
        numCols = Integer.parseInt(s[1]);
       
        visited = new boolean[numRows][numCols];
        matrix = new int[numRows][numCols];
        for (int i = 0; i < numRows; i++) {
            line = br.readLine();
            s = line.split(" ");
            for (int j = 0; j < numCols; j++) {
                matrix[i][j] = Integer.parseInt(s[j]); 
            }
        }
       
        int result = bfs(0, 0);
       
        bw.write(result+"\n");
           
        bw.flush();
        bw.close();
       
        return;
    }
   
    public static void main(String[] args) throws NumberFormatException, IOException {
        solucao m = new solucao();
        m.process();
       
        System.exit(0);
    }
}

class Coord {
    int i;
    int j;
    int day;
    int dayFinal;
    int[][] matrix;
   
    public Coord(int i, int j, int d, int dF, int[][] m) {
        this.i = i;
        this.j = j;
        day = d;
        dayFinal = dF;
        matrix = m;
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução