Yet Another Runtime Error

Write here if you have problems with your Java source code

Moderator: Board moderators

Post Reply
danielsm6
New poster
Posts: 1
Joined: Wed Jun 17, 2015 8:47 am

Yet Another Runtime Error

Post by danielsm6 »

This is the code I'm pasting into the submit form.

Code: Select all

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

class Main {
	private int t;
	private char[][] matriz;
	
	enum POSICAO { valida, invalida, fim};
	
	class Pair{
		public Pair(int x, int y) {
			super();
			this.x = x;
			this.y = y;
		}
		public int x;
		public int y;
	}
	
	public char[][] input(){
		t = Integer.parseInt(ReadLn().trim());
		matriz = new char[t][t];
		for(int i = 0; i<t;i++){
			String linha = ReadLn().trim();
			for(int j = 0; j<t; j++){
				matriz[i][j] = linha.charAt(j);
			}
		}
		return matriz;
	}
	
	public Pair encontrarBranca(){
		for(int i = 0; i<t; i++){
			for(int j = 0; j<t; j++){
				if(matriz[i][j]=='W'){
					return new Pair(j,i);
				}
			}
		}
		return null;
	}
	
	public Pair andarEsquerda(Pair posAtual){
		if(posAtual.x-1 < 0){
			return null;
		}
		else if(posAtual.y == 0){
			return posAtual;
		}
		else if(matriz[posAtual.y-1][posAtual.x-1]=='B'){
			return new Pair(posAtual.x-2,posAtual.y-2);
		}
		else{
			return new Pair(posAtual.x-1,posAtual.y-1);
		}
	}
	
	public Pair andarDireira(Pair posAtual){
		if(posAtual.x+1 >= t){
			return null;
		}
		else if(posAtual.y == 0){
			return posAtual;
		}
		else if(matriz[posAtual.y-1][posAtual.x+1]=='B'){
			return new Pair(posAtual.x+2,posAtual.y-2);
		}
		else{
			return new Pair(posAtual.x+1,posAtual.y-1);
		}
	}
	
	public POSICAO validarPosicao(Pair pos){
		if(pos == null){
			return POSICAO.invalida;
		}
		else if(pos.x<0 || pos.y<0 || pos.x>t || pos.y>t || matriz[pos.y][pos.x]!='.'){
			return POSICAO.invalida;
		}
		else if(pos.y==0){
			return POSICAO.fim;
		}
		else{
			return POSICAO.valida;
		}
	}
	
	public int andar(Pair posAtual){
		Pair proxEsquerda = andarEsquerda(posAtual);
		Pair proxDireita = andarDireira(posAtual);		
		return verificarCaminho(proxEsquerda) + verificarCaminho(proxDireita);
		
	}
	
	private int verificarCaminho(Pair pos) {
		POSICAO verificacao = validarPosicao(pos);
		if(verificacao == POSICAO.valida){
			return andar(pos);
		}
		else if(verificacao == POSICAO.fim){
			return 1;
		}
		else{
			return 0;
		}
	}
	
	static String ReadLn () 
    {
        int inbyte = -1;
        StringBuffer line = new StringBuffer();
    
        try
        {
            inbyte = System.in.read();
            while ((inbyte >= 0) && (inbyte != '\n'))
            {
                line.append((char) inbyte);
                inbyte = System.in.read();
            }
        }
        catch (IOException e)
        {
            return (null);
        }

        if ((inbyte < 0) && (line.length() == 0)) return (null);  // eof
        return line.toString();
    }
	
	public static void main(String args[]){
		//Scanner scanner = new Scanner(System.in);
		int n;
		n = Integer.parseInt(ReadLn().trim());
		String saida = "";
		for(int i=0; i<n; i++){
			Main d = new Main();
			d.input();
			Pair posicaoBranca = d.encontrarBranca();
			int caminhos = 0;
			caminhos = d.andar(posicaoBranca);
		}
		System.out.println(saida);
	}
}
I have tried a lot to narrow down exactly where the error is happening, but I cant reproduce it locally.
Can you help me?
Thanks
Post Reply

Return to “Java”