(UVA) Sticker Collector Robot - Solution

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

The solution below just execute a graph traversal following the instructions on the problem statement.


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

class Main {
    public char[][] matrix;
    public char[] instructions;
    public String positions = "NLSO";
    public int numRows;
    public int numCols;
    public int countStickers;
   
    public void changePosition(int i, int j, char curr) {
        if (matrix[i][j] == '*') {
            countStickers++;
        }
        matrix[i][j] = curr;
    }
   
    public boolean isValidPosition(int i, int j) {
        if (i < 0 || j < 0 || i >= numRows || j >= numCols || matrix[i][j] == '#') {
            return false;
        }
        return true;
    }
   
    public void process() throws NumberFormatException, IOException {
        Scanner sc = new Scanner(System.in);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        numRows = sc.nextInt();
        numCols = sc.nextInt();
        int numInstructions = sc.nextInt();
        while (numRows != 0 || numCols != 0 || numInstructions != 0) {
            int x = 0;
            int y = 0;
           
            matrix = new char[numRows][numCols];
            for (int i = 0; i < numRows; i++) {
                String line = sc.next();
                for (int j = 0; j < numCols; j++) {
                    matrix[i][j] = line.charAt(j);
                    if (positions.indexOf(matrix[i][j]) != -1) {
                        x = i;
                        y = j;
                    }
                }
            }
           
            String line = sc.next();
            instructions = new char[numInstructions];
            for (int i = 0; i < numInstructions; i++) {
                instructions[i] = line.charAt(i);
            }
           
            countStickers = 0;
            for (int i = 0; i < numInstructions; i++) {
                char curr = matrix[x][y];
               
                if (instructions[i] == 'F') { // front
                    if (curr == 'N') {
                        if (isValidPosition(x-1, y)) {
                            x--;
                            changePosition(x, y, curr);
                        }
                    } else if (curr == 'S') {
                        if (isValidPosition(x+1, y)) {
                            x++;
                            changePosition(x, y, curr);
                        }
                    } else if (curr == 'L') {
                        if (isValidPosition(x, y+1)) {
                            y++;
                            changePosition(x, y, curr);
                        }
                    } else { // curr == 'O'
                        if (isValidPosition(x, y-1)) {
                            y--;
                            changePosition(x, y, curr);
                        }
                    }
                } else if (instructions[i] == 'D') { // right
                    int index = (positions.indexOf(curr)+1)%4;
                    matrix[x][y] = positions.charAt(index);
                } else if (instructions[i] == 'E') { // left
                    int index = (((positions.indexOf(curr)-1)%4)+4)%4;
                    matrix[x][y] = positions.charAt(index);
                }
            }
           
            bw.write(countStickers+"\n");
           
            numRows = sc.nextInt();
            numCols = sc.nextInt();
            numInstructions = sc.nextInt();   
        }
                                                      
        bw.flush();
        bw.close();
       
        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