(SPOJ) Pacman - Solution

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


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

class Main {
    public int food;
    public int maxFood;
    public char[][] matrix;
   
    public void check(int i, int j) {
        if (matrix[i][j] == 'o') {
            food++;
            maxFood = Math.max(maxFood, food);
        }
        else if (matrix[i][j] == 'A') {
            food = 0;
        }
    }
   
    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();
        int size = Integer.parseInt(line);

        matrix = new char[size][size];
       
        for (int i = 0; i < size; i++) {
            line = br.readLine();
            for (int j = 0; j < size; j++) {
                matrix[i][j] = line.charAt(j);
            }
        }       
       
        food = 0;
        maxFood = 0;
        for (int i = 0; i < size; i++) {
            if (i%2 == 0) {
                for (int j = 0; j < size; j++) {
                    check(i, j);
                }
            }
            else {
                for (int j = size-1; j >= 0; j--) {
                    check(i, j);
                }
            }
        }
       
        bw.write(maxFood+"\n");
       
        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