• Queen Attack -- 微软2017年预科生计划在线编程笔试第二场


    #!/usr/bin/env python
    # coding:utf-8
    # Queen Attack       
    # https://hihocoder.com/problemset/problem/1497
    # Author: kngxscn
    # Date: 2017-04-22
    
    """
    时间限制:10000ms
    单点时限:1000ms
    内存限制:256MB
    描述
    There are N queens in an infinite chessboard. We say two queens may attack each other if they are in the same vertical line, horizontal line or diagonal line even if there are other queens sitting between them.
    
    Now given the positions of the queens, find out how many pairs may attack each other?
    
    输入
    The first line contains an integer N.
    
    Then N lines follow. Each line contains 2 integers Ri and Ci indicating there is a queen in the Ri-th row and Ci-th column.  
    
    No two queens share the same position.  
    
    For 80% of the data, 1 <= N <= 1000
    
    For 100% of the data, 1 <= N <= 100000, 0 <= Ri, Ci <= 1000000000
    
    输出
    One integer, the number of pairs may attack each other.
    
    样例输入
    5  
    1 1  
    2 2  
    3 3   
    1 3
    3 1
    样例输出
    10
    """
    
    # 统计出每行、每列、每条对角线的 Queen 数量,然后分别求组合个数
    
    def C2(n):
        return n*(n-1) / 2
    
    if __name__ == '__main__':
        n = int(raw_input())
        horizontal = {}
        vertical = {}
        diagonal1 = {}
        diagonal2 = {}
        for i in range(n):
            r, c = [int(x) for x in raw_input().split(' ')]
            if r not in horizontal:
                horizontal[r] = 0
            horizontal[r] += 1
            if c not in vertical:
                vertical[c] = 0
            vertical[c] += 1
            if r-c not in diagonal1:
                diagonal1[r-c] = 0
            diagonal1[r-c] += 1
            if r+c not in diagonal2:
                diagonal2[r+c] = 0
            diagonal2[r+c] += 1
    
        attack_count = 0
        for i in horizontal:
            attack_count += C2(horizontal[i])
        for i in vertical:
            attack_count += C2(vertical[i])
        for i in diagonal1:
            attack_count += C2(diagonal1[i])
        for i in diagonal2:
            attack_count += C2(diagonal2[i])
        print attack_count
    
  • 相关阅读:
    docker的基本操作
    docker和虚拟化技术的区别
    项目命名规则
    Javascript IE 内存释放
    关于ie的内存泄漏与javascript内存释放
    Java遍历HashMap并修改(remove)
    java 中类的加载顺序
    java类的加载以及初始化顺序 .
    JavaScript也谈内存优化
    JavaScript 的垃圾回收与内存泄露
  • 原文地址:https://www.cnblogs.com/renzongxian/p/6747085.html
Copyright © 2020-2023  润新知