QueenAttack

问题分析:
1.对于行和列,要求得每行或每列的棋子个数,只需要把横坐标或纵坐标相同的棋子数目相加即可,然后使用k*(k-1)/2就可求得攻击次数
2.对于对角线上的点,需要分析对角线上点的坐标关系。分析得到,右下对角线上的点横纵坐标之差相等,左下对角线上的点横纵坐标之和相等,分析出来后接下来做法就和处理行和列上点一样。
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner; public class QueenAttack{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
int n = scan.nextInt();
HashMap<Integer, Integer> mapRow = new HashMap<Integer, Integer>();
HashMap<Integer, Integer> mapCol = new HashMap<Integer, Integer>();
HashMap<Integer, Integer> mapLeft = new HashMap<Integer, Integer>();
HashMap<Integer, Integer> mapRight = new HashMap<Integer, Integer>();
int[][] array = new int[n][2];
// 输入放有棋子的横纵坐标,同时计算出每行有多少个棋子,每列有多少个棋子
for (int i = 0; i < n; i++) {
array[i][0] = scan.nextInt();
int x = array[i][0];
array[i][1] = scan.nextInt();
int y = array[i][1];
// 计算当前行有多少棋子
numChess(mapRow, x);
// 计算当前列有多少棋子
numChess(mapCol, y);
// 计算左下对角线上棋子数目,满足在同一左下对角线上的条件是横纵坐标之和相等,所以传入参数是横纵坐标之和
numChess(mapLeft, x + y);
// 计算右下对角线上棋子数目,满足在同一右下对角线上的条件是横纵坐标之差相等,所以传入参数是横纵坐标之差
numChess(mapRight, y - x);
}
// 计算每行有多少次攻击
int numRow = numAttack(mapRow);
// 计算每列有多少次攻击
int numCol = numAttack(mapCol);
// 计算左下对角线上攻击次数
int numLeft = numAttack(mapLeft);
// 计算右下对角线上攻击次数
int numRight = numAttack(mapRight);
System.out.println(numRow + numCol + numLeft + numRight);
}
} // 计算每行、每列或对角线上的棋子数目
public static void numChess(HashMap<Integer, Integer> map, int key) {
if (map.containsKey(key)) {
map.put(key, map.get(key) + 1);
} else {
map.put(key, 1);
}
} // 计算每行、每列或对角线上的攻击次数
public static int numAttack(HashMap<Integer, Integer> map) {
Iterator iter = map.entrySet().iterator();
int count = 0;
while (iter.hasNext()) {
Map.Entry<Integer, Integer> entry = (Map.Entry<Integer, Integer>) iter.next();
Integer val = entry.getValue();
if (val >= 2) {
count = count + val * (val - 1) / 2;
}
}
return count;
}
}
QueenAttack的更多相关文章
随机推荐
- AlertDialog的几种用法
xml代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro ...
- HashMap,Hashset,ArrayList以及LinkedList集合的区别,以及各自的用法
基础内容 容器就是一种装其他各种对象的器皿.java.util包 容器:Set, List, Map ,数组.只有这四种容器. Collection(集合) 一个一个往里装,Map 一对一对往里装. ...
- 07/29/2013 02:10:02 AM - CMDPHP: Poller[0] Host[6] DS[10] WARNING: Result from SNMP not valid. Partial Result: U
snmpwalk -c public -v2c 客户端ip地址 自定义的oid 能取到数据,但是服务器端就是图片一片空白 rrdtool fetch 文件名.rrd 查看到的全都是nan cac ...
- 关于maven source1.5报错
是因为maven 默认是1.5编译的 <build>//加上这个配置,把编译给改掉试试 <pluginManagement> <plugins> <plugi ...
- Android接入支付宝和微信支付
然后把下载下来的aar包,放到项目目录下面的libs目录下,通过下面的gradle依赖进来 // 支付宝 SDK AAR 包所需的配置compile(name: 'alipaySdk-15.6.0-2 ...
- billu_b0x靶场刷题
https://www.vulnhub.com/ 里面有很多安全环境,只要下载相关镜像,在虚拟机上面搭建运行就可以练习对应靶场了. 第一步.信息收集 nmap扫描内网开放80端口的存活主机 nmap ...
- C++中:点运算符和箭头运算符的区别
点运算符用于获取对象成员: 箭头运算符用于获取指针指向的对象的成员: 例如: std::string s1 = "string"; std::string *p = &s1 ...
- Archive for required library: 'D:/Program Files/Apache/maven-repository/dom4j/dom4j/1.6.1/dom4j-1.6.1.jar'
今天导入一个项目工程,发现报错:Archive for required library: 'D:/Program Files/Apache/maven-repository/dom4j/dom4j/ ...
- m3u8 格式转MP4
现在很多视频网站采用HLS流媒体的方式来提供视频直播,在HTML源代码中flash的播放地址为 http://xxxxxx/video/movie.m3u8 1.m3u8下载的格式大致如下: #EXT ...
- 找回Settings Sync的gist id和token
方法一:如果你本地有缓存参考:https://www.cnblogs.com/zhang1028/p/9514471.html 方法二:如果你电脑重装系统了 1.找回gist id 登陆你的githu ...