Problem D: Queens, Knights and Pawns
Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88443#problem/D

Description

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.) Q P Q K 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.

Sample Input

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 00 0 0

Sample Output

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

HINT

题意

给你一个棋盘,棋盘上面有士兵,有皇后,有马

士兵不会动,皇后攻击范围是八个方向的直线,马是日字

然后问你最后有多少个格子是安全的

题解

直接暴力就好了

代码:

  1. //qscqesze
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <ctime>
  6. #include <iostream>
  7. #include <algorithm>
  8. #include <set>
  9. #include <vector>
  10. #include <sstream>
  11. #include <queue>
  12. #include <typeinfo>
  13. #include <fstream>
  14. #include <map>
  15. #include <stack>
  16. typedef long long ll;
  17. using namespace std;
  18. //freopen("D.in","r",stdin);
  19. //freopen("D.out","w",stdout);
  20. #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
  21. #define maxn 200051
  22. #define mod 10007
  23. #define eps 1e-9
  24. int Num;
  25. //const int inf=0x7fffffff; //нчоч╢С
  26. const int inf=0x3f3f3f3f;
  27. inline ll read()
  28. {
  29. ll x=,f=;char ch=getchar();
  30. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  31. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  32. return x*f;
  33. }
  34. //**************************************************************************************
  35.  
  36. int n,m;
  37. int s[][];
  38. //queens, knights and pawns
  39. int ans=;
  40. void check(int x,int y)
  41. {
  42. if(x<||x>n||y<||y>m)
  43. return;
  44. if(s[x][y]==)
  45. {
  46. s[x][y]=;
  47. ans++;
  48. }
  49. }
  50. void at_queue(int x,int y)
  51. {
  52. check(x,y);
  53. for(int i=x;i<=n;i++)
  54. {
  55. if(s[i][y]==)
  56. break;
  57. check(i,y);
  58. }
  59. for(int i=x;i>=;i--)
  60. {
  61. if(s[i][y]==)
  62. break;
  63. check(i,y);
  64. }
  65. for(int i=y;i>=;i--)
  66. {
  67. if(s[x][i]==)
  68. break;
  69. check(x,i);
  70. }
  71. for(int i=y;i<=m;i++)
  72. {
  73. if(s[x][i]==)
  74. break;
  75. check(x,i);
  76. }
  77. for(int i=x,j=y;i<=n&&j<=m;i++,j++)
  78. {
  79. if(s[i][j]==)
  80. break;
  81. check(i,j);
  82. }
  83. for(int i=x,j=y;i>=&&j>=;j--,i--)
  84. {
  85. if(s[i][j]==)
  86. break;
  87. check(i,j);
  88. }
  89. for(int i=x,j=y;i>=&&j<=m;i--,j++)
  90. {
  91. if(s[i][j]==)
  92. break;
  93. check(i,j);
  94. }
  95. for(int i=x,j=y;i<=n&&j>=;i++,j--)
  96. {
  97. if(s[i][j]==)
  98. break;
  99. check(i,j);
  100. }
  101. }
  102. void at_knight(int x,int y)
  103. {
  104. check(x,y);
  105. check(x+,y+);
  106. check(x+,y+);
  107. check(x-,y+);
  108. check(x-,y+);
  109. check(x+,y-);
  110. check(x+,y-);
  111. check(x-,y-);
  112. check(x-,y-);
  113. }
  114. struct node
  115. {
  116. int x,y;
  117. };
  118. node que[];
  119. node kni[];
  120. int main()
  121. {
  122. int t=;
  123. while(scanf("%d%d",&n,&m)!=EOF)
  124. {
  125. if(n==&&m==)
  126. break;
  127. ans=;
  128. memset(que,,sizeof(que));
  129. memset(kni,,sizeof(kni));
  130. memset(s,,sizeof(s));
  131. int k1=read();
  132. for(int i=;i<k1;i++)
  133. {
  134. que[i].x=read();
  135. que[i].y=read();
  136. }
  137. int k2=read();
  138. for(int i=;i<k2;i++)
  139. {
  140. kni[i].x=read();
  141. kni[i].y=read();
  142. int x=kni[i].x,y=kni[i].y;
  143. if(s[x][y]==)
  144. {
  145. s[x][y]=;
  146. ans++;
  147. }
  148. }
  149. int k3=read();
  150. for(int i=;i<k3;i++)
  151. {
  152. int x=read();
  153. int y=read();
  154. if(s[x][y]==)
  155. {
  156. s[x][y]=;
  157. ans++;
  158. }
  159. }
  160. for(int i=;i<k1;i++)
  161. at_queue(que[i].x,que[i].y);
  162. for(int i=;i<k2;i++)
  163. at_knight(kni[i].x,kni[i].y);
  164. printf("Board %d has %d safe squares.\n",t++,n*m-ans);
  165. }
  166. }

Codeforces Gym 100650D Queens, Knights and Pawns 暴力的更多相关文章

  1. sicily 1172. Queens, Knights and Pawns

    Description You all are familiar with the famous 8-queens problem which asks you to place 8 queens o ...

  2. Codeforces Gym 100803F There is No Alternative 暴力Kruskal

    There is No Alternative 题目连接: http://codeforces.com/gym/100803/attachments Description ICPC (Isles o ...

  3. Codeforces Gym 100286J Javanese Cryptoanalysis 傻逼暴力

    原题地址:http://codeforces.com/gym/100286/attachments/download/2013/20082009-acmicpc-northeastern-europe ...

  4. Codeforces Gym 100342C Problem C. Painting Cottages 暴力

    Problem C. Painting CottagesTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1 ...

  5. Codeforces Gym 100513I I. Sale in GameStore 暴力

    I. Sale in GameStore Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/p ...

  6. Codeforces Gym 100203G G - Good elements 标记暴力

    G - Good elementsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...

  7. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  8. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

  9. Codeforces Gym 101623A - 动态规划

    题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...

随机推荐

  1. 浏览器兼容——jquery的html()不兼容IE

    在看着一个页面A,是一个弹出框,用的jquery中的ajax,然后弹出的内容是另一个Div的.而所出现的问题,是在浏览器中都有弹出框,但是只有谷歌和火狐中的弹出框中内容. 当时,我所想到的是另一个问题 ...

  2. window.history

    作者:zccst 旧版: forword() backword() go(number) HTML5中新增了 onhashchange  浏览器兼容性较好,用得较多 pushState / repla ...

  3. 使用carrierwave出现MiniMagick::Invalid错误的解决方法

    安装Imagemagick不能从源码安装,要从软件市场安装,否则会出现错误:MiniMagick::Invalid 使用make uninstall卸载后,重新在软件市场里安装,问题解决.

  4. Android 引用library project

    1.如何将一个android工程作为库工程(library project) library project是作为jar包被其它android工程使用的,首先它也是普通的android工程.然后: 1 ...

  5. 浅析基层检察院派驻乡镇检察室的健康发展 z

    时间:2011-03-22 10:08 作者:祝志方 新闻来源:正义网 一.前言 在我国,基层检察院派驻乡镇检察室的发展经过了一个曲折发展的历程,上世纪80年代,随着经济社会的发展,一批乡镇检察室应运 ...

  6. ili9341 横屏驱动代码

    void ili9341_Initializtion(void) { u16 i; RCC->APB2ENR|=<<; //使能PORTB时钟 GPIOB->CRH&= ...

  7. unity延时方法Invoke和InvokeRepeating

    MonoBehaviour里面有两个内置的延时方法 Invoke Invoke(methodName: string, time: float): void; methodName:方法名 time: ...

  8. Windows10输入法的切换

    Alt+Shift            中⇒あ,あ⇒中 Shift                  中⇒英,英⇒中 Alt+Caps Lock    あ⇒カ,A⇒あ⇒カ Ctrl+Caps Loc ...

  9. jar,war,ear区别及java基础杂七八

    jar,war,earqu区别 这三种文件都可以看作是java的压缩格式,其实质是实现了不同的封装: jar--封装类war--封装web站点ear--封装ejb.它们的关系具体为:jar:      ...

  10. Java设计模式---工厂方法模式(Factory-Method)

    一.普通工厂模式 建立一个工厂类,对实现了同一接口的一些类进行实例的创建 实例代码: 发送短信和邮件的例子,首先创建接口: public interface Sender { public void ...