BNUOJ 4299 God Save the i-th Queen(想法题)

题目链接==》    http://www.bnuoj.com/v3/problem_show.php?pid=4299

题意:一个X*Y棋盘上放了N个queens,再放一个queen,有几个空格可以放,前提是和已经在棋盘上的queen不同行不同列不同斜线

思路:每个点所在四条直线上,不能再放queen,所以将有点的直线标记,再遍历所有点,如果点所在直线已被标记,则不选。(完了)

代码:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 #include<iostream>
 5
 6 using namespace std;
 7
 8 const int T=4e4+9;
 9
10 int h[T],l[T],xie_1[T],xie_2[T];///xie_1表示斜率大于0的直线
11 int sum,n,m;
12
13 void begin1( )
14 {
15     sum=0;
16     memset(h, 0 , sizeof( int )*T);
17     memset(l, 0 , sizeof( int )*T);
18     memset(xie_1, 0 , sizeof( int )*T);
19     memset(xie_2, 0 , sizeof( int )*T);
20 }
21
22 void judge( int x, int y)
23 {
24     int w;
25     h[x]=1; l[y]=1;
26     xie_1[x+y+1]=1;
27     w=m+1-y;  /// 标记斜率为负的棋盘,则将列标号倒置,从m-->1,所以这个点所在直线也是L(x+w+1)
28     xie_2[x+w+1]=1;
29 }
30
31 void judge_two( int x, int y )
32 {
33     int w=m+1-y;
34     if( h[x] || l[y] || xie_1[x+y+1] || xie_2[x+w+1] )
35         return ;
36     sum++;
37
38 }
39
40
41
42
43 int main( )
44 {
45     int k,a,b;
46     while(scanf("%d%d%d",&n,&m,&k),(n+m+k))
47     {
48         begin1( );
49         for(int i=1;i<=k;i++)
50         {
51             scanf("%d%d",&a,&b);
52             judge( a, b );
53         }
54         for(int i=1;i<=n;i++)
55             for(int j=1;j<=m;j++)
56                 judge_two( i, j );
57         printf("%d\n",sum);
58     }
59     return 0;
60 }

时间: 2024-07-29 00:00:54

BNUOJ 4299 God Save the i-th Queen(想法题)的相关文章

【组队赛#5】BNU 4299 God Save the i-th Queen (数组映射)

[题目链接]click here~~ [题目大意]给你一个N*M的棋盘,并且给定T个已经固定位置的皇后(横坐标,纵坐标已知),求下一个合法放置皇后的可能数 [解题思路] 因为只是求下一个皇后的位置,而不是求所有其他可能,因此可以按照如下思路进行:  对n*n的矩阵进行映射,将其转换为1*(n+m)的单行模式. 1.行列可以直接映射. 2.对于对角线有两种情况(画图验证) 1.1.映射为x+y模式(一三区间) 2..1映射为x-y+Y模式(二四区间)  (注意!前面的x是映射的行,Y后才是列) 代

B - Save the problem! CodeForces - 867B 构造题

B - Save the problem! CodeForces - 867B 这个题目还是很简单的,很明显是一个构造题,但是早训的时候脑子有点糊涂,想到了用1 2 来构造, 但是去算这个数的时候算错了... 用1 2 来构造 可以先枚举一些数来找找规律. 1 1 2 2 3 1 1 1    2 1 1 4 .... 可以发现每一个数都是 n/2+1 的可能, 所以反过来推过去就是 (s-1)*2  或者(s-1)*2+1 这个(s-1)*2+1的答案才是正确答案 因为 这个s可以==1 #i

(省事选拔系列---团体赛)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 avai

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 Com

Oralce的Save命令

使用save()命令可以将当前的缓冲区的内容保存到文件中,这样,即使缓冲区中的内容被覆盖, 也保留有当前的执行语句save命令的语法如下: Save [file] file_name[replace|create|append] 其中,file_name表示将SQL*PLUS缓冲区的内容保存到有file_name指定的文件中,create表示创 建一个file_name文件,并将缓冲区中的内容保存到该文件中,该选项为默认值.Replace表示如 果file_name已经存在,则覆盖file_na

uva 11195 Another queen (用状态压缩解决N后问题)

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2136 Problem A Another n-Queen Problem I guess the n-queen problem is known by every person who has studied backtracking. In this problem you s

Save your cat Aizu - 2224

Nicholas Y. Alford was a cat lover. He had a garden in a village and kept many cats in his garden. The cats were so cute that people in the village also loved them. One day, an evil witch visited the village. She envied the cats for being loved by ev

npm --save 选项作用

学习了:https://segmentfault.com/q/1010000000403629 -save和save-dev可以省掉你手动修改package.json文件的步骤. spm install module-name -save 自动把模块和版本号添加到dependencies部分 spm install module-name -save-dve 自动把模块和版本号添加到devdependencies部分 看了api,感觉好像不太用了: -P, --save-prod: Packag

redis中save和bgsave区别

转自:redis中save和bgsave区别 SAVE 和 BGSAVE 两个命令都会调用 rdbSave 函数,但它们调用的方式各有不同: SAVE 直接调用 rdbSave ,阻塞 Redis 主进程,直到保存完成为止.在主进程阻塞期间,服务器不能处理客户端的任何请求. BGSAVE 则 fork 出一个子进程,子进程负责调用 rdbSave ,并在保存完成之后向主进程发送信号,通知保存已完成. Redis 服务器在BGSAVE 执行期间仍然可以继续处理客户端的请求.