hihocode——#1497 : Queen Attack

版權聲明:本文爲博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/gxh27954/article/details/69788288
http://hihocoder.com/problemset/problem/1497
時間限制: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


題意:給你很多皇后的座標,問有多少對會互相攻擊~~

思路:只要用4個map記錄行列和兩個斜邊出現的次數就行~~


#include<bits/stdc++.h>
using namespace std;

map<int ,int > m1;
map<int ,int > m2;
map<int ,int > m3;
map<int ,int > m4;
typedef long long LL;
int main()
{
    int n;
    LL ans=0;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        ans+=m1[a];
        m1[a]++;
        ans+=m2[b];
        m2[b]++;
        ans+=m3[a+b];
        m3[a+b]++;
        ans+=m4[b-a];
        m4[b-a]++;
    }
    cout<<ans<<endl;
    return 0;
}
/*
5
1 1
2 2
3 3
1 3
3 1
*/


10
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章