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<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{int n,g;int k,x,y,s,t;int row[60005];int col[60005];int da[60005];int db[60005];while(scanf("%d",&n)&&n!=0){memset(row,0,sizeof(row));memset(col,0,sizeof(col));memset(da,0,sizeof(da));memset(db,0,sizeof(db));scanf("%d",&g);for(int i=0; i<g; i++){scanf("%d%d%d%d%d",&k,&x,&y,&s,&t);for(int j=0; j<k; j++){int tmpa=x+j*s;int tmpb=y+j*t;row[tmpa]++;col[tmpb]++;da[tmpa+tmpb]++;db[tmpa-tmpb+n]++;}}int ans=0;for(int i=1; i<=2*n; i++){if(row[i]>1) ans+=row[i]-1;if(col[i]>1) ans+=col[i]-1;if(da[i]>1) ans+=da[i]-1;if(db[i]>1) ans+=db[i]-1;}printf("%d\n",ans);}return 0;
}

hdu 2576 Queen Collisions相关推荐

  1. zoj 2576 Queen Collisions

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

  2. Queen Collisions(分行列模拟)

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

  3. Queen Collisions

    点击打开链接 Queen Collisions Time Limit: 1000MS Memory limit: 65536K 题目描述 Lots of time has been spent by ...

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

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

  5. HDU 4930 Fighting the Landlords(扯淡模拟题)

    Fighting the Landlords 大意: 斗地主....   分别给出两把手牌,肯定都合法.每张牌大小顺序是Y (i.e. colored Joker) > X (i.e. Blac ...

  6. HDU——1106排序(istringstream的使用、STLvector练习)

    排序 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submiss ...

  7. hdu 5438 Ponds 拓扑排序

    Ponds Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/contests/contest_showproblem ...

  8. HDU 1248 寒冰王座(全然背包:入门题)

    HDU 1248 寒冰王座(全然背包:入门题) http://acm.hdu.edu.cn/showproblem.php?pid=1248 题意: 不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票 ...

  9. hdu 1312 Red and Black 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 第二条深搜,题目并不难,但是做了我好久好久,由于一个细节,让我赌上了一个晚上的时间. 题目大意: ...

最新文章

  1. PHP中对象的深拷贝与浅拷贝
  2. float、double(浮点数)区别还有和decimal(定点数)的比较
  3. python慢在哪里_求大神分析一下我的python脚本慢在哪里?
  4. 从壹开始 [ Ids4实战 ] 之三║ 详解授权持久化 用户数据迁移
  5. linux 查询oracle情况,在Linux下使用SQLPlus查看Oracle数据库表空间及使用率
  6. SPOJ OTOCI 动态树 LCT
  7. Google退出中国,谁的压力最大?
  8. 嵌入式开发 ARM Cortex-M3处理器技术优势分析
  9. cad2004教程_AutoCAD视频教程!0基础到大神,室内建筑机械电气家具土木工程园林景观,七大行业方向助你成为精英!...
  10. 联想笔记本小新V2000怎么进BIOS设置
  11. 步进电机控制器编程实例C语言,步进电机控制器原理_步进电机控制器编程实例...
  12. 注塑模具设计的技术知识汇总
  13. 淘客部分功能实现源码
  14. 模拟器怎么安装xposed框架
  15. 2022电大国家开放大学网上形考任务-普通心理学非免费(非答案)
  16. 刘泽云《计量经济学实验教程》笔记
  17. Ant Design删除操作弹出对话框
  18. Tensorflow2.0的简单GCN代码(使用cora数据集)
  19. Android 蓝牙Hid开发
  20. 大连理工大学计算机组成原理实验,大连理工大学计算机组成原理实验报告(二).docx...

热门文章

  1. FFMpeg TS转成mp4命令
  2. 【dbeaver】发生了错误。请参阅日志文件
  3. EXCEL中多行多列数据与一行或一列数据的互相转换
  4. NCRE公共基础知识(一) 计算机系统
  5. AI绘画与虚拟人生成实践(二):智能不智障!用chatgpt自动写爆款内容
  6. java ext_ext下载及使用
  7. 一个简单的jxl文件上传功能
  8. 利用CompletableFuture集齐7龙珠
  9. Android软件开发用什么语言?
  10. 电商路演投资计划融资报告PPT模板