Cod sursa(job #3195266)

Utilizator TaniaKallosKallos Tania TaniaKallos Data 20 ianuarie 2024 12:44:55
Problema Ograzi Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.76 kb
#include <fstream>
#include <cstdio>
#include <map>

using namespace std;


ofstream fout("ograzi.out");


struct coord
{
    int x;
    int y;
};
struct cmp
{
    bool operator()(coord a, coord b) const
    {
        if (a.x != b.x)
            return a.x < b.x;
        return a.y < b.y;
    }
};

const int N = 5e4, M = 1e5, MAX = 1e6, MAXCH = 16;
int n, m, w, h, nroi;
coord drept[N+1], oi[M+1];
map <coord, coord, cmp> mp;


bool in_dreptunghi(coord colt, coord oaie)
{
    int difx = oaie.x - colt.x;
    int dify = oaie.y - colt.y;
    if (0 <= difx && difx <= w && 0 <= dify && dify <= h)
        return 1;
    else
        return 0;
}

int read_num(char str[], int &ind) // la final -> ind = prima poz pe care nu e o cifra
{
    int nr = 0;
    while ('0' <= str[ind] && str[ind] <= '9')
    {
        nr = nr * 10 + str[ind] - '0';
        ind++;
    }
    return nr;
}


int main()
{
    FILE * filein = fopen("ograzi.in", "r");

    char date[35];
    fgets(date, 35, filein);
    int p = 0;
    n = read_num(date, p), p++;
    m = read_num(date, p), p++;
    w = read_num(date, p), p++;
    h = read_num(date, p);

    while (feof(filein) == 0)
    {
        for (int i = 1; i <= n; i++)
        {
            char str[MAXCH];
            fgets(str, MAXCH, filein);

            int ind = 0;
            drept[i].x = read_num(str, ind);
            ind++; // sare peste spatiu
            drept[i].y = read_num(str, ind);

            coord poz;
            poz.x = drept[i].x / w;
            poz.y = drept[i].y / h;
            mp[poz] = drept[i];
        }
        for (int i = 1; i <= m; i++)
        {
            char str[MAXCH];
            fgets(str, MAXCH, filein);

            int ind = 0;
            oi[i].x = read_num(str, ind);
            ind++; // sare peste spatiu
            oi[i].y = read_num(str, ind);
        }
    }
    

    for (int i = 1; i <= m; i++)
    {
        coord poz;
        poz.x = oi[i].x / w;
        poz.y = oi[i].y / h;

        if (mp.find(poz) != mp.end() && in_dreptunghi( mp.find(poz)->second, oi[i] ))
            nroi++;

        if (poz.x > 0)
        {
            coord poz_st = poz;
            poz_st.x--;
            if (mp.find(poz_st) != mp.end() && in_dreptunghi( mp.find(poz_st)->second, oi[i] ))
                nroi++;
        }
        if (poz.y > 0)
        {
            coord poz_jos = poz;
            poz_jos.y--;
            if (mp.find(poz_jos) != mp.end() && in_dreptunghi( mp.find(poz_jos)->second, oi[i] ))
                nroi++;
        }
        if (poz.x > 0 && poz.y > 0)
        {
            coord poz_diag = poz;
            poz_diag.x--, poz_diag.y--;
            if (mp.find(poz_diag) != mp.end() && in_dreptunghi( mp.find(poz_diag)->second, oi[i] ))
                nroi++;
        }
    }


    fout << nroi;


    return 0;
}