时间限制: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
题意:同一对角线,水平线,垂线的皇后之间会相互攻击,给出n个皇后问有几对会相互攻击
题解:同一水平线的皇后y相等,同一垂线的皇后X相等,同一斜对角线的X+Y(/) 同一对角线的X-Y相等(\)
#include <bits/stdc++.h> using namespace std; map<int ,long long>mp[4]; int main() { int T,i,x,y; long long ans = 0; cin>>T; while(T--){ cin>>x>>y; mp[0][x]++; mp[1][y]++; mp[2][x+y]++; mp[3][y-x]++; } for(i=0;i<4;i++) for(map<int, long long>::iterator it = mp[i].begin();it != mp[i].end();it++) ans += (it->second)*(it->second-1)/2; cout<<ans<<endl; return 0; }