Cod sursa(job #913563)

Utilizator Stefex09Stefan Teodorescu Stefex09 Data 13 martie 2013 16:57:54
Problema Secv8 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.53 kb
#include <cstdio>
#include <queue>

using namespace std;

int Mat[110][110];
const int dx[] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int dy[] = {-1, 0, 1, 1, 1, 0, -1, -1};
int S[110][110];
int P[110][110];
pair <int, int> Sol[110];

int main()
{
    freopen ("soricel2.in", "r", stdin);
    freopen ("soricel2.out", "w", stdout);

    int N, M, Adap, Obst, i, j, x, y;
    queue < pair <int, int> > QS;
    queue < pair <int, int> > QP;
    pair <int, int> now;
    int nowx, nowy;
    int vx, vy;
    int Ans = 0;

    scanf ("%d %d", &N, &M);
    scanf ("%d %d", &x, &y);
    QS.push (make_pair (x, y));
    S[x][y] = 1;
    scanf ("%d %d", &x, &y);
    QP.push (make_pair (x, y));
    P[x][y] = 1;

    scanf ("%d", &Adap);
    while (Adap --){
        scanf ("%d %d", &x, &y);
        Mat[x][y] = 1;
    }

    scanf ("%d", &Obst);
    while (Obst --){
        scanf ("%d %d", &x, &y);
        Mat[x][y] = -1;
    }

    for (i = 0; i <= M + 1; i ++)
        Mat[0][i] = -1, Mat[N + 1][i] = -1;

    for (i = 0; i <= N + 1; i ++)
        Mat[i][M + 1] = -1, Mat[i][0] = -1;

    while (!QS.empty ()){
        now = QS.front ();
        QS.pop ();
        nowx = now.first;
        nowy = now.second;

        for (i = 0; i < 8; i ++){
            vx = nowx + dx[i];
            vy = nowy + dy[i];

            if ((!S[vx][vy] || ((S[nowx][nowy] + 1) < S[vx][vy])) && Mat[vx][vy] >= 0){
                S[vx][vy] = S[nowx][nowy] + 1;
                QS.push (make_pair (vx, vy));
            }
        }
    }

    while (!QP.empty ()){
        now = QP.front ();
        QP.pop ();
        nowx = now.first;
        nowy = now.second;

        for (i = 0; i < 8; i ++){
            vx = nowx + dx[i];
            vy = nowy + dy[i];

            if ((!P[vx][vy] || ((P[nowx][nowy] + 1) < P[vx][vy])) && Mat[vx][vy] >= 0)
                if (vx >= 1 && vx <= N && vy >= 1 && vy <= M){
                    P[vx][vy] = P[nowx][nowy] + 1;
                    QP.push (make_pair (vx, vy));
                }
        }
    }

    for (i = 1; i <= N; i ++)
        for (j = 1; j <= M; j ++){
            if (Mat[i][j] == 1 && S[i][j] && (S[i][j] * 2 <= P[i][j]))
                Ans ++, Sol[Ans].first = i, Sol[Ans].second = j;

            if (Mat[i][j] == 1 && S[i][j] && !P[i][j])
                Ans ++, Sol[Ans].first = i, Sol[Ans].second = j;
        }

    printf ("%d\n", Ans);

    for (i = 1; i <= Ans; i ++)
        printf ("%d %d\n", Sol[i].first, Sol[i].second);

    return 0;
}