(UVA) 10116 - Robot Motion - Solução

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

class Main {
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main processando = new Main();
        processando.takeInput();
      
        System.exit(0);
    }

    char[][] leMapa(BufferedReader br, int linha, int coluna) throws NumberFormatException, IOException  {
        char[][] matrizChar = new char[linha][coluna];
        String conteudoLinha;
      
        for (int i = 0; i < linha; i++) {
            conteudoLinha = br.readLine();
            for (int j = 0; j < coluna; j++) {
                matrizChar[i][j] = conteudoLinha.charAt(j);          
            }
        }
      
        return matrizChar;    
    }
  
    void takeInput() throws NumberFormatException, IOException {
        String line = "";
        StringTokenizer tokenizer = null;
      
        int tamLinha;
        int tamColuna;
        int inicial;
      
        char[][] matriz;
      
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      
        while ((line = br.readLine()) != null) {
            tokenizer = new StringTokenizer(line);
          
            tamLinha = Integer.valueOf(tokenizer.nextToken());
            tamColuna = Integer.valueOf(tokenizer.nextToken());
            inicial = Integer.valueOf(tokenizer.nextToken());

            if (tamLinha == 0 && tamColuna == 0 && inicial == 0) {
                return;
            }
                          
            matriz = leMapa(br, tamLinha, tamColuna);
            int[][] matrizPassagem = new int[tamLinha][tamColuna]; // matriz para guardar os passos
          
            for (int i = 0; i < tamLinha; i++) {
                for (int j = 0; j < tamColuna; j++) {
                    matrizPassagem[i][j] = 0;          
                }
            }
          
            char direcao; // guarda para onde vai
            boolean procuraFim = true;
          
            // posição em que o robô inicia
            int linha = 0;
            int coluna = inicial-1;
            // descobre para que direção ir
            direcao = matriz[linha][coluna];
          
          
            boolean loop = false;
            int steps = 1; // qte de passos
            matrizPassagem[linha][coluna] = steps;
            int tamLoop = 0;
            int stepsBefLoop = 0;

            while (procuraFim) {
                if (direcao == 'N') {
                    linha -= 1;
                }
                else if (direcao == 'S') {
                    linha += 1;
                }
                else if (direcao == 'E') {
                    coluna += 1;
                }
                else if (direcao == 'W') {
                    coluna -= 1;
                }
                          
                if (linha < 0 || linha >= tamLinha || coluna < 0 || coluna >= tamColuna) {
                    procuraFim = false; // tá fora da matriz, encontrou uma saída
                    break;
                }
                else if (matrizPassagem[linha][coluna] != 0) {
                    procuraFim = false;
                    loop = true; // tá em loop
                    tamLoop = (steps + 1) - matrizPassagem[linha][coluna];
                    stepsBefLoop = steps - tamLoop;
                }
                else {
                    steps++; // adiciona um passo
                    matrizPassagem[linha][coluna] = steps;
                    direcao = matriz[linha][coluna];
                }
            }
          
            if (loop == false) {
                System.out.println(steps + " step(s) to exit");
            }
            else { // se entrar em loop
                System.out.println(stepsBefLoop + " step(s) before a loop of " + tamLoop + " step(s)");
            }
        }
      
        return;
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução