(SPOJ) O Mar Não Está Para Peixe - Solution

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

The solution below uses a matrix to keep the areas where a mesh is placed.


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

class Main {
    public void process() throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       
        int numMesh = Integer.parseInt(br.readLine());
        int[][] sea = new int[101][101];
        for (int i = 0; i < numMesh; i++) {
            String line = br.readLine();
            String[] s = line.split("\\s");
            int xi = Integer.parseInt(s[0]);
            int xf = Integer.parseInt(s[1]);
            int yi = Integer.parseInt(s[2]);
            int yf = Integer.parseInt(s[3]);
            for (int j = xi; j < xf; j++) {
                for (int k = yi; k < yf; k++) {
                    sea[j][k] = 1;               
                }
            }
        }
       
        int count = 0;
        for (int i = 1; i <= 100; i++) {
            for (int j = 1; j <= 100; j++) {
                if (sea[i][j] == 1) {
                    count++;
                }
            }
        }
       
        System.out.println(count);
                          
        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