点击打开链接

Queen Collisions

Time Limit: 1000MS Memory limit: 65536K

题目描述


Lots of time has been spent by computer science students dealing with queens on a chess board. Two queens on a chessboard collide if they lie on the same row, column or diagonal, and there is no piece between them. Various sized square boards and numbers of queens are considered. For example,
Figure 1, with a 7 x 7 board, contains 7 queens with no collisions. In Figure 2 there is a 5 x 5 board with 5 queens and 4 collisions. In Figure 3, a traditional 8 x 8 board, there are 7 queens and 5 collisions.
On an n x n board, queen positions are given in Cartesian coordinates (x, y) where x is a column number, 1 to n, and y is a row number, 1 to n. Queens at distinct positions (x1, y1) and (x2, y2) lie on the same diagonal if (x1- x2) and (y1- y2) have the same magnitude. They lie on the same row or column if x1= x2 or y1= y2, respectively. In each of these cases the queens have a collision if there is no other queen directly between them on the same diagonal, row, or column, respectively. For example, in Figure 2, the collisions are between the queens at (5, 1) and (4, 2), (4, 2) and (3, 3), (3, 3) and (2, 4), and finally (2, 4) and (1, 5). In Figure 3, the collisions are between the queens at (1, 8) and (4, 8), (4, 8) and (4, 7), (4, 7) and (6, 5), (7, 6) and (6, 5), and finally (6, 5) and (2, 1). Your task is to count queen collisions.
In many situations there are a number of queens in a regular pattern. For instance in Figure 1 there are 4 queens in a line at (1,1), (2, 3), (3, 5), and (4, 7). Each of these queens after the first at (1, 1) is one to the right and 2 up from the previous one. Three queens starting at (5, 2) follow a similar pattern. Noting these patterns can allow the positions of a large number of queens to be stated succinctly.

输入

The input will consist of one to twenty data sets, followed by a line containing only 0.

The first line of a dataset contains blank separated positive integers n g, where n indicates an n x n board size, and g is the number of linear patterns of queens to be described, where n < 30000, and g < 250.
The next g lines each contain five blank separated integers, k x y s t, representing a linear pattern of k queens at locations (x + i*s, y +i*t), for i = 0, 1, ..., k-1. The value of k is positive. If k is 1, then the values of s and t are irrelevant, and they will be given as 0. All queen positions will be on the board.
The total number of queen positions among all the linear patterns will be no more than n, and all these queen positions will be distinct.

输出

There is one line of output for each data set, containing only the number of collisions between the queens.

The sample input data set corresponds to the configuration in the Figures.
Take some care with your algorithm, or else your solution may take too long.

示例输入

7 2
4 1 1 1 2
3 5 2 1 2
5 1
5 5 1 -1 1
8 3
1 2 1 0 0
3 1 8 3 -1
3 4 8 2 -3
0

示例输出

0
4
5

提示

来源

#include<stdio.h>
#include<string.h>
using namespace std;
int h[30007],l[30007],x1[30007*2],x2[30007*2];
int main()
{int n,g;while(scanf("%d",&n)&&n!=0){scanf("%d",&g);memset(h,0,sizeof(h));memset(l,0,sizeof(l));memset(x1,0,sizeof(x1));memset(x2,0,sizeof(x2));int k,x,y,s,t,cnt=0;while(g--){scanf("%d%d%d%d%d",&k,&x,&y,&s,&t);int xx,yy;for(int a=0; a<k; a++){xx=x+a*s;h[xx]++;if(h[xx]>=2)cnt++;yy=y+a*t;l[yy]++;if(l[yy]>=2)cnt++;x1[xx+yy]++;if(x1[xx+yy]>=2)cnt++;x2[xx-yy+n]++;if(x2[xx-yy+n]>=2)cnt++;}}printf("%d\n",cnt);}return 0;
}

Queen Collisions相关推荐

  1. Queen Collisions(分行列模拟)

    Queen Collisions Time Limit: 1000MS Memory limit: 65536K 题目描述 Lots of time has been spent by compute ...

  2. zoj 2576 Queen Collisions

    1.http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2576 2.比赛时只想到了暴搜,殊不知有这么 ...

  3. hdu 2576 Queen Collisions

    Queen Collisions Time Limit: 1000MS Memory limit: 65536K 题目描述 Lots of time has been spent by compute ...

  4. 【SDUT第11周周赛Problem A】SDUT2576——Queen Collisions

    来源:点击打开链接 由于一些原因,需要在短短的一段时间内速成图论和搜索了=  =,希望能够有一个不错的结果. 这个题是著名八皇后问题的变种,大意就是问在一个棋盘中,照面的皇后有几组(横着竖着斜着都算) ...

  5. empress和queen区别_queen与empress

    (1)「queen」和「empress」不仅可以用来指称「king」和「emperor」的妻子,也能指代握有正式权力的女性君主. (2) 英国的君主(queen或king)之所以曾经有过empress ...

  6. uva 10401 Injured Queen Problem(dp)

    题目链接:10401 - Injured Queen Problem 题目大意:给出一个字符串,要求在n * n(n为字符串的长度)的棋盘上摆放n个受伤的皇后,受伤的皇后只能攻击到同一列和它周围8个格 ...

  7. uva10401Injured Queen Problem(递推)

    题目:uva10401Injured Queen Problem(递推) 题目大意:依然是在棋盘上放皇后的问题,这些皇后是受伤的皇后,攻击范围缩小了.攻击范围在图中用阴影表示(题目).然后给出棋盘的现 ...

  8. Chess Queen【数学】

    Chess Queen UVA - 11538 题目传送门 题目大意:输入两个整数n,m,在n行m列的棋盘中放入白黑两个棋子,棋子在同一行.同一列或同一对角线上能相互进攻,问有多少种摆放方案. AC代 ...

  9. 回溯算法n皇后问题_使用回溯算法的N Queen问题和解决方案

    回溯算法n皇后问题 N-皇后问题 (N - Queen's problem) The n – queen problem is the generalized problem of 8-queens ...

最新文章

  1. 解决pip使用异常No module named 'pip'
  2. TCL:花开刹那还是浴火重生
  3. Facebook 游戏开发更新文档 API 参考文档 v6.0
  4. 输入数字存入数组C语言,//从键盘上输入若干整数,并将其存入数组中,并统计输入数据的个...
  5. 圣诞海报设计没有思路,素材技巧都来了!
  6. 如何使用HttpContext对象
  7. [置顶]常用存储过程集锦
  8. 气泡shader_仿蚂蚁森林气泡
  9. 最新大数据案例分享:2019微信数据报告(图集)
  10. JAVA钓鱼游戏_java如何实现纸牌游戏之小猫钓鱼算法
  11. android 一种键盘不能调起的解决方法
  12. 手机移动开发大作业 -- 仿淘宝app
  13. mezzanine安装(python2.7+nginx+mysql+supervisor)
  14. 爆笑囧人囧事 2009 大合集!
  15. docker,containerd,runc,docker-shim
  16. 怎么用手机拍摄制作视频
  17. 2021年危险化学品经营单位安全管理人员作业考试题库及危险化学品经营单位安全管理人员操作证考试
  18. oracle数据库的安装及配置方法
  19. Storj白皮书v3最全面解读,Docker创始人的加入能否扳倒AWS S3
  20. DNS云学堂 | 权威DNS那些事儿(上)

热门文章

  1. css 商城 两列_你需掌握的CSS知识都在这了(长文建议收藏,文末有福利)
  2. 船长的error笔记
  3. 拿蚂蚁头条快手offer怎么选?网友:第一次见头条比快手offer低
  4. linux修改dns不生效,Linux 临时修改和永久修改DNS的方法
  5. 性能优化之节流(throttling)与防抖(debounce)
  6. 计算机基础知识教程excel函数计算,计算机基础知识:Excel中的函数操作(四)...
  7. 第七届科技节电子设计大赛须知
  8. 该设备正在使用中.请关闭可能使用该设备的所
  9. banner生成图像的网站有哪些?
  10. 链表(提高)-数据结构(二)