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

【题目链接】:http://hihocoder.com/problemset/problem/1497

【题意】

给你n个皇后;
然后问你其中能够互相攻击到的皇后的对数;
皇后的攻击可以穿透;

【题解】

对角线的话
就把x+y和x-y占据;
然后把x行和y列占据了;
每次对输入的数据看看对角线和行和列有没有棋子;
有的话就加上相应的数目;
这个用map就能搞定;
可能会爆int

【Number Of WA

0

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 2e4+100;

int n;
map <int,int> dic1,dic2,dic3,dic4;
LL ans = 0;

int main()
{
    //freopen("F:\rush.txt","r",stdin);
    ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
    cin >> n;
    rep1(i,1,n)
    {
        int x,y;
        cin >> x >> y;
        ans+=dic1[x],ans+=dic2[y],ans+=dic3[x-y],ans+=dic4[x+y];
        dic1[x]++,dic2[y]++,dic3[x-y]++,dic4[x+y]++;
    }
    cout << ans << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626379.html