Bnu4299--god Save the i-th Queen —————— "Queen attack, find the corresponding compression space"

Source: Internet
Author: User
Tags integer numbers

God Save the i-th queentime limit:5000msmemory limit:65536kb64-bit integer IO format:%lld Java class name: Main Prev Submit Status Statistics discuss nextdid you know this during the ACM-ICPC World Finals a big chessboard is installed Every year and was available for the participants to play against each other? In this problem, we'll test your basic chess-playing abilities to verify so you would don't make a fool of yourself if Y OU advance to the world finals.during the yesterday's practice Session, you tried to solve the problem of N Independent ro OKs. This time, let's concentrate on Queens. As you probably know, the Queens could move not onlyhorizontally and vertically, but also diagonally. You were given a chessboard with i−1 queens already placed and your task was to find all squares the May was used to place t He i-th queen such that it cannot is captured by any of the others. Inputthe input consists of several tasks. Each task begins with a line containing three integer numbers separated by a space: X, Y, N. Xand YGive 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 is NLines, each containing, numbers XK, YKseparated by a space. They give the position of the k-th Queen, 1 XK X, 1 YK Y. Assume that those positions is distinct, i.e., No. Queens share the same square. The last task was followed by a line containing three zeros. Outputfor each task, output one line containing a single integer number:the number of squares which is not occupied and Do not lie on the same row, column, or diagonal as any of the existing queens. Sample Input
8 8 24 55 50 0 0
Sample Output
20


Problem-solving ideas: Just get the problem with the violence, the result of the array of super memory, and set, and time out. Later, you can only open 4 arrays to save coverage. That is, the Row,col,pie,na array to record rows and apostrophes (diagonal cases). You can see that the pie array is added by X, Y and minus 1. The NA array can convert Y to the position relative to the upper-right corner (y-y+1). Then enumerate the points in the map, and then determine whether the point is not in the row, nor the apostrophe (diagonal), the number of records can be.


#include <bits/stdc++.h>using namespace Std;const int Maxn=21000;bool row[maxn],col[maxn],pie[maxn*2],na[maxn*    2];void init () {memset (row) (row,0,sizeof);    memset (col,0,sizeof (col));    memset (pie,0,sizeof (PIE)); memset (na,0,sizeof (NA));}    int main () {int x,y,n;        while (scanf ("%d%d%d", &x,&y,&n)!=eof&& (x+y+n)) {init ();            for (int i=0;i<n;i++) {int x, y;            scanf ("%d%d", &x,&y);       Row[x]=1;       Record that the line is overwritten col[y]=1;   Record that the column is overwritten pie[x+y-1]=1;    The top right-to-left diagonal of the record is covered na[y-y+x]=1;        Record left top to bottom right diagonal is overwritten} int num=0; for (int i=1;i<=x;i++) {for (int j=1;j<=y;j++) {if ((!row[i]) && (!col[j]) && (!p                Ie[i+j-1]) && (!na[y-j+i]) {//Enumerate each point, if the row and column is not covered, add 1 num++;    }}} printf ("%d\n", num); } return 0;}

  

Bnu4299--god Save the i-th Queen —————— "Queen attack, find the corresponding relationship compression space"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.