BNU4299——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

Prev

Submit Status Statistics Discuss

Next

Type:

None

None
 
Graph Theory
 
    2-SAT
 
    Articulation/Bridge/Biconnected Component
 
    Cycles/Topological Sorting/Strongly Connected Component
 
    Shortest Path
 
        Bellman Ford
 
        Dijkstra/Floyd Warshall
 
    Euler Trail/Circuit
 
    Heavy-Light Decomposition
 
    Minimum Spanning Tree
 
    Stable Marriage Problem
 
    Trees
 
    Directed Minimum Spanning Tree
 
    Flow/Matching
 
        Graph Matching
 
            Bipartite Matching
 
            Hopcroft–Karp Bipartite Matching
 
            Weighted Bipartite Matching/Hungarian Algorithm
 
        Flow
 
            Max Flow/Min Cut
 
            Min Cost Max Flow
 
DFS-like
 
    Backtracking with Pruning/Branch and Bound
 
    Basic Recursion
 
    IDA* Search
 
    Parsing/Grammar
 
    Breadth First Search/Depth First Search
 
    Advanced Search Techniques
 
        Binary Search/Bisection
 
        Ternary Search
 
Geometry
 
    Basic Geometry
 
    Computational Geometry
 
    Convex Hull
 
    Pick‘s Theorem
 
Game Theory
 
    Green Hackenbush/Colon Principle/Fusion Principle
 
    Nim
 
    Sprague-Grundy Number
 
Matrix
 
    Gaussian Elimination
 
    Matrix Exponentiation
 
Data Structures
 
    Basic Data Structures
 
    Binary Indexed Tree
 
    Binary Search Tree
 
    Hashing
 
    Orthogonal Range Search
 
    Range Minimum Query/Lowest Common Ancestor
 
    Segment Tree/Interval Tree
 
    Trie Tree
 
    Sorting
 
    Disjoint Set
 
String
 
    Aho Corasick
 
    Knuth-Morris-Pratt
 
    Suffix Array/Suffix Tree
 
Math
 
    Basic Math
 
    Big Integer Arithmetic
 
    Number Theory
 
        Chinese Remainder Theorem
 
        Extended Euclid
 
        Inclusion/Exclusion
 
        Modular Arithmetic
 
    Combinatorics
 
        Group Theory/Burnside‘s lemma
 
        Counting
 
    Probability/Expected Value
 
Others
 
    Tricky
 
    Hardest
 
    Unusual
 
    Brute Force
 
    Implementation
 
    Constructive Algorithms
 
    Two Pointer
 
    Bitmask
 
    Beginner
 
    Discrete Logarithm/Shank‘s Baby-step Giant-step Algorithm
 
    Greedy
 
    Divide and Conquer
 
Dynamic Programming
                  Tag it!

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: XNand give the chessboard size, 1  X, Y 20 000. i1 is the number of queens already placed, 0  N  X·.

After the first line, there are 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

解题思路:刚拿到题目的时候用的暴力,结果数组超内存,又用了set,又超时。后来知道,可以只开4个数组来存覆盖情况。即row,col,pie,na数组来记录行列和撇捺(对角线情况)。可以发现pie数组由x,y相加减1后得到。na数组可以将y转化为相对于右上角的位置为(Y-y+1)。然后枚举地图中各个点,然后判断该点既不在行列,也不在撇捺(对角线)的情况,记录个数即可。
#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,0,sizeof(row));
    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;       //记录该行被覆盖
            col[y]=1;       //记录该列被覆盖
            pie[x+y-1]=1;   //记录右上到左下的对角线被覆盖
            na[Y-y+x]=1;    //记录左上到右下的对角线被覆盖
        }
        int num=0;
        for(int i=1;i<=X;i++){
            for(int j=1;j<=Y;j++){
                if((!row[i])&&(!col[j])&&(!pie[i+j-1])&&(!na[Y-j+i])){
                        //枚举各个点,如果行列撇捺都没覆盖,加1
                    num++;
                }
            }
        }
        printf("%d\n",num);
    }
    return 0;
}

  

时间: 2024-10-10 18:27:30

BNU4299——God Save the i-th Queen——————【皇后攻击,找到对应关系压缩空间】的相关文章

C语言解决八皇后问题

1 #include <stdio.h> 2 #include <stdlib.h> 3 4 /* this code is used to cope with the problem of the eight queens. 5 * array borad[9][9] is a virtual borad. 6 * line 0 and volumn 0 is ignored. 7 * at first we find a place to set a queen on it,

15、蛤蟆的数据结构笔记之十五栈的应用之栈与递归之八皇后问题

15.蛤蟆的数据结构笔记之十五栈的应用之栈与递归之八皇后问题 本篇名言:"人的一生应当这样度过:当回忆往事的时候,他不致于因为虚度年华而痛悔,也不致于因为过去的碌碌无为而羞愧:在临死的时候,他能够说:"我的整个生命和全部精力,都已经献给世界上最壮丽的事业--为人类的解放而斗争." 继续递归问题,本次是经典的八皇后问题: 欢迎转载,转载请标明出处: 1.  八皇后问题 八皇后问题,是一个古老而著名的问题,是回溯算法的典型案例.该问题是国际西洋棋棋手马克斯·贝瑟尔于1848年提出

HDU 2553 N皇后问题 (回溯法)

题意  中文n*n的棋盘放n个皇后(攻击同行/列/主副对角线)  使任何两个都不互相攻击  有多少种方法 枚举每一行  用vis[3][i]记录列 主对角线 副对角线是否被占  同列和对角线都没被占就继续枚举下一行  当枚举到n+1行的时候就是一个合法答案了 注: n*n的方阵中主对角线可以用(i-j+n)标号  副对角线可以用(i+j)标号 //ans[]={0,1,0,0,2,10,4,40,92,352,724}; #include<cstdio> #include<cstring

【搜索】还是N皇后

先看题才是最重要的: 这道题有点难理解,毕竟Code speaks louder than words,所以先亮代码后说话: 1 #include<iostream> 2 using namespace std; 3 char s[1000];int n,map[1000],mod,ans; 4 void dfs(int deep,int line,int lr,int rl) 5 { 6 if(deep>n) 7 { 8 ans++; 9 return; 10 } 11 int pos

回溯法 -数据结构与算法

1.回溯法算法思想: 定义: 回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步重新选择,这种走不通就退回再走的技术为回溯法,而满足回溯条件的某个状态的点称为“回溯点”. 1.回溯法适用:有许多问题,当需要找出它的解集(全部解)或者要求回答什么解是满足某些约束条件的最优解时,往往要使用回溯法. 2.有组织的穷举式搜索:回溯法的基本做法是搜索或者有的组织穷尽搜索.它能避免搜索所有的可能性.即避免不必要的搜索.这种方

【策略】UVa 278 - Chess

Chess  Almost everyone knows the problem of putting eight queens on an  chessboard such that no Queen can take another Queen. Jan Timman (a famous Dutch chessplayer) wants to know the maximum number of chesspieces of one kind which can be put on an  

回溯法与分支限界

回溯法 1.有许多问题,当需要找出它的解集或者要求回答什么解是满足某些约束条件的最佳解时,往往要使用回溯法. 2.回溯法的基本做法是搜索,或是一种组织得井井有条的,能避免不必要搜索的穷举式搜索法.这种方法适用于解一些组合数相当大的问题. 3.回溯法在问题的解空间树中,按深度优先策略,从根结点出发搜索解空间树.算法搜索至解空间树的任意一点时,先判断该结点是否包含问题的解.如果肯定不包含(剪枝过程),则跳过对该结点为根的子树的搜索,逐层向其祖先结点回溯:否则,进入该子树,继续按深度优先策略搜索. 问

学校测试-2015-03-01

记得以前做N皇后问题见到过二进制+位运算优化的方法, 今天的搜索题第三题和第四题都可以用到二进制和位运算. 就只做了这两个题目. 题目三 描述 传递游戏(pass) Description n个人在做传递物品的游戏,编号为1-n. 游戏规则是这样的:开始时物品可以在任意一人手上,他可把物品传递给其他人中的任意一位:下一个人可以传递给未接过物品的任意一人. 即物品只能经过同一个人一次,而且每次传递过程都有一个代价:不同的人传给不同的人的代价值之间没有联系: 求当物品经过所有n个人后,整个过程的最小

回溯法——n后问题

问题描述: 在n*n的棋盘上放置彼此不受攻击的n个皇后.按照国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子.n后问题等价于在n*n格的棋盘上放置n个皇后,任何2个皇后不放在同一行或同一列或同一斜线. 盲目的迭代枚举: 1 /* 2 *作者:xymaqingxiang 3 *说明:八皇后--盲目迭代法 4 *分析:通过8重循环模拟搜索空间中的8^8个状态,从中找出满足约束条件的可行性方案 5 *日期:2014-05-15 6 */ 7 #include <iostream>