Algorithm


Problem link- https://www.spoj.com/problems/QKP/

QKP - Queens, Knights and Pawns

no tags 

 

You all are familiar with the famous 8-queens problem which asks you to place 8 queens on a chess board so no two attack each other. In this problem, you will be given locations of queens and knights and pawns and asked to find how many of the unoccupied squares on the board are not under attack from either a queen or a knight (or both). We’ll call such squares “safe” squares. Here, pawns will only serve as blockers and have no capturing ability. The board below has 6 safe squares. (The shaded squares are safe.)

 

subir imagenes

 

Recall that a knight moves to any unoccupied square that is on the opposite corner of a 2x3 rectangle from its current position; a queen moves to any square that is visible in any of the eight horizontal, vertical, and diagonal directions from the current position. Note that the movement of a queen can be blocked by another piece, while a knight’s movement can not.

Input

There will be multiple test cases. Each test case will consist of 4 lines. The first line will contain two integers n and m, indicating the dimensions of the board, giving rows and columns, respectively. Neither integer will exceed 1000. The next three lines will each be of the form

 

k r1 c1 r2 c2 · · · rk ck

 

indicating the location of the queens, knights and pawns, respectively. The numbering of the rows and columns will start at one. There will be no more than 100 of any one piece. Values of n = m = 0 indicate end of input.

Output

Each test case should generate one line of the form

 

Board b has s safe squares.

 

where b is the number of the board (starting at one) and you supply the correct value for s.

Example

4 4
2 1 4 2 4
1 1 2
1 2 3
2 3
1 1 2
1 1 1
0
1000 1000
1 3 3
0
0
0 0

Output:
Board 1 has 6 safe squares.
Board 2 has 0 safe squares.
Board 3 has 996998 safe squares.

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <cstdlib>
#include <iostream>
#include <cstdio>

using namespace std;
char a[1000][1000];
int m, n;

void knight(int x, int y)
{
    int i, j, k;
    i = x - 2; j = y - 1; k = y + 1;
    if(i >= 0)
    {
         if(j >= 0)
            if(a[i][j] == '0')
                a[i][j] = 'x';
        if(k < m)
            if(a[i][k] == '0')
                a[i][k] = 'x';
    }
    i++; j--; k++;
    if(i >= 0)
    {
         if(j >= 0)
            if(a[i][j] == '0')
                a[i][j] = 'x';
        if(k < m)
            if(a[i][k] == '0')
                a[i][k] = 'x';
    }
    i += 2;
    if(i < n)
    {
         if(j >= 0)
            if(a[i][j] == '0')
                a[i][j] = 'x';
        if(k < m)
            if(a[i][k] == '0')
                a[i][k] = 'x';
    }
    i++; j++; k--;
    if(i < n)
    {
         if(j >= 0)
            if(a[i][j] == '0')
                a[i][j] = 'x';
        if(k < m)
            if(a[i][k] == '0')
                a[i][k] = 'x';
    }
}

void queen(int x, int y)
{
    int i, j;
    i = y - 1; j = x - 1; 
    while(i >= 0 && (a[x][i] == '0' || a[x][i] == 'x'))
        a[x][i--] = 'x';
    while(j >= 0 && (a[j][y] == '0' || a[j][y] == 'x'))
        a[j--][y] = 'x';
    i = y + 1; j = x + 1; 
    while(i < m && (a[x][i] == '0' || a[x][i] == 'x'))
        a[x][i++] = 'x';
    while(j < n && (a[j][y] == '0' || a[j][y] == 'x'))
        a[j++][y] = 'x';
    i = y + 1; j = x + 1; 
    while((i < m && j < n) && (a[j][i] == '0' || a[j][i] == 'x'))
        a[j++][i++] = 'x';
    i = y - 1; j = x - 1; 
    while((i >= 0 && j >= 0) && (a[j][i] == '0' || a[j][i] == 'x'))
        a[j--][i--] = 'x';
    i = y - 1; j = x + 1;
    while((i >= 0 && j < n) && (a[j][i] == '0' || a[j][i] == 'x'))
        a[j++][i--] = 'x';
    i = y + 1; j = x - 1;
    while((i < m && j >= 0) && (a[j][i] == '0' || a[j][i] == 'x'))
        a[j--][i++] = 'x';
}

main()
{
    int k, q, p, r, c, z = 0, w = 1;
    for(int i = 0; i < 1000; i++)
        for(int j = 0; j < 1000; j++)
            a[i][j] = '0';
    while(1)
    {
        z = 0;
        scanf("%d%d", &n, &m);
        if(n == 0 && m == 0) break;
        
        
        scanf("%d", &q);
        for(int i = 0; i < q; i++)
        {
            scanf("%d%d", &r, &c);
            a[r - 1][c - 1] = 'q';
        }
        
        scanf("%d", &k);
        for(int i = 0; i < k; i++)
        {
            scanf("%d%d", &r, &c);
            a[r - 1][c - 1] = 'k';
        }
        
        scanf("%d", &p);
        for(int i = 0; i < p; i++)
        {
            scanf("%d%d", &r, &c);
            a[r - 1][c - 1] = 'p';
        }
        
        /*for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
                printf("%c ", a[i][j]);
            printf("\n");
        }
        printf("\n\n");*/
        
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
            {
                if(a[i][j] == 'k')
                    knight(i, j);
                else if(a[i][j] == 'q')
                    queen(i, j);
            }
        
        
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                //printf("%c ", a[i][j]);
                if(a[i][j] == '0') z++;
            }
            //printf("\n");
        }
        printf("Board %d has %d safe squares.\n", w++, z);
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++>
                a[i][j] = '0';
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
4 4
2 1 4 2 4
1 1 2
1 2 3
2 3
1 1 2
1 1 1
0
1000 1000
1 3 3
0
0
0 0

Output

x
+
cmd
Board 1 has 6 safe squares.
Board 2 has 0 safe squares.
Board 3 has 996998 safe squares.
Advertisements

Demonstration


SPOJ Solution-QKP Queens, Knights and Pawns-Solution in C, C++, Java, Python ,SPOJ Solution

Previous
SPOJ Solution - Test Life, the Universe, and Everything - Solution in C, C++, Java, Python