(省事选拔系列---团体赛)God Save the i-th Queen(数学分析)

God Save the i-th Queen

Time Limit: 5000ms
Memory Limit: 65536KB
64-bit integer IO format: %lld      Java class name: Main
Submit Status PID: 4299
Did you know that during the ACM-ICPC World Finals a big chessboard is installed every year and is available for the participants to play against each other? In this problem, we will test your basic chess-playing abilities to verify that you would not make a fool of yourself if you advance to the World Finals.
During the yesterday’s Practice Session, you tried to solve the problem of N independent rooks. This time, let’s concentrate on queens. As you probably know, the queens may move not only
horizontally and vertically, but also diagonally.
You are given a chessboard with i−1 queens already placed and your task is to find all squares that may be used to place the i-th queen such that it cannot be captured by any of the others.

Input

The input consists of several tasks. Each task begins with a line containing three integer numbers separated by a space: X, Y , N. X and Y give the chessboard size, 1 X, Y 20 000. N = i−1 is the number of queens already placed, 0 N X·Y .
After the first line, there are N lines, each containing two numbers xk, yk separated by a space. They give the position of the k-th queen, 1 xk X, 1 yk Y . You may assume that those positions are distinct, i.e., no two queens share the same square.
The last task is followed by a line containing three zeros.

Output

For each task, output one line containing a single integer number: the number of squares which are not occupied and do not lie on the same row, column, or diagonal as any of the existing queens.

Sample Input

8 8 2
4 5
5 5
0 0 0

Sample Output

20
题意:类似八皇后问题 给你一个 你n×m的棋盘
告诉你个皇后的位置 问你棋盘最后有多少个点不被攻击到
思路:
常规方法超内存 
于是我们记录 每个皇后的攻击路线
如果point(ij)没有在攻击路线上 (原谅我不会上图。。。见代码吧 )
那么就从cnt++
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int x[40005],y[40005],ix[40005],iy[40005];
int main()
{
    int n,m,t;
    while(~scanf("%d%d%d",&n,&m,&t)&&n)
    {
        memset(x,0,sizeof(x));
        memset(y,0,sizeof(y));
        memset(ix,0,sizeof(ix));
        memset(iy,0,sizeof(iy));

        while(t--)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            x[a]=1;
            y[b]=1;
            ix[a+b]=1;
            iy[a+m-b]=1;
        }
        int cnt=0;
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= m; j++)
            {
                if(!x[i] && !y[j] && !ix[i+j] && !iy[i+m-j])
                {
                    cnt++;
                }
            }
        printf("%d\n",cnt);
    }
}




你可能感兴趣的:(算法,数学,八皇后)