E
E
EvgenySManko2015-10-09 17:00:26
Java
EvgenySManko, 2015-10-09 17:00:26

One of the tasks of the school Olympiad in Informatics. How to decide?

I am preparing for the Olympiad in Informatics, I don’t know how to solve one task:

Several rectangles are built on the coordinate plane along the grid lines. It is necessary to count the number of points with integer coordinates belonging to all these rectangles at once.
Write a program that
• Reads information about rectangles from the file tohki.txt;
• Finds and displays the number of points with integer coordinates belonging to all these rectangles at once.
The file tohki.txt has the following structure:
• The first line contains the number of rectangles n (1≤n≤1000);
• The next line contains information about the first rectangle: first, the coordinates of the upper left vertex, then the coordinates of the lower right vertex of the rectangle - four space-separated integers not exceeding 1000 in absolute value;
• In each of the following (n-1) lines, information about the next rectangle

I will write in Java, here are attempts to solve:
import java.io.*;
import java.util.*;

class Main{
    public static void main(String[] args)throws Exception{
        FastScanner fs = new FastScanner("tohki.txt");
        try{
            int c = fs.nextInt();
            ArrayList<Integer> list = new ArrayList<Integer>();
            Pramoygolnik[] prm = new Pramoygolnik[c];
            for(int i = 0; i < c; i++){
                prm[i] = new Pramoygolnik(fs.nextInt(), fs.nextInt(), fs.nextInt(), fs.nextInt());
            }
            for(int i = 0; i < c; i++){
                if(i == c-1) break;
                for(int a = 0; a < prm[i].list.size(); a++){
                    if(prm[i].list.contains(prm[i+1].list.get(a))) list.add(Integer.parseInt(prm[i+1].list.get(a)));
                }
            }
            for(int i = 0; i < list.size(); i++){
                for(int a = i + 1; a < list.size(); a++){
                    if(list.get(i)==list.get(a)) list.remove(i);
                }
            }

            System.out.println(list.size());
        }catch (EOFException e){
        }
    }
    static class Pramoygolnik {
        int x1;
        int y1;
        int x2;
        int y2;
        ArrayList<String> list = new ArrayList<String>();
        public Pramoygolnik(int x1, int y1, int x2, int y2){
            this.x1=x1;
            this.y1=y1;
            this.x2=x2;
            this.y2=y2;
            for(int i = y1; i >= y2; i--){
                for(int a = x1; a <= x2; a++){
                    list.add(i+""+a);
                }
            }
        }
    }
    static class FastScanner{
        BufferedReader reader;
        StringTokenizer tokenizer;

        public FastScanner(String name)throws Exception{
            reader = new BufferedReader(new FileReader(name));
        }

        public String tokenizer()throws Exception{
            while(tokenizer==null||!tokenizer.hasMoreTokens()){
                tokenizer = new StringTokenizer(reader.readLine());
            }
            return(tokenizer.nextToken());
        }
        public int nextInt()throws Exception{
            return(Integer.parseInt(tokenizer()));
        }
    }
}

Does anyone have any ideas?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Cheremisin, 2015-10-09
@EvgenySManko

O! The simplest solution is probably the most inefficient!
There is a two-dimensional field array 1000x1000 filled with zeros.
There are X rectangles
Loop through the rectangles
Take the first one and COLOR ALL ITS POINTS, making +=1 in our array
Take the next rectangle and repeat the previous
one Result: scan all the points of our array and display those with a point equal to X
Done.
Well, how to write it in Java, try it yourself.
You can optimize, for this the points of the rectangles need to be logically added (conjunction, if I'm not mistaken) each with the next one, the result of the next should be the result of the addition. In other words, the result of adding two rectangles is the rectangle of their intersection. On the next cycle, we take this result and the next rectangle, we use the result on the next one.
You can not draw anything in the coordinate array at all! Fast and very efficient, only 4 operations per rectangle.

G
GavriKos, 2015-10-09
@GavriKos

There is an idea to share the reward for winning the Olympiad for the whole toaster, how do you like it?
Or the idea that olympiad, examination and other homework tasks should be solved not by a collective toaster, but by the one to whom they are assigned.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question