1709. [Usaco2007 Oct]Super Paintball超级弹珠

传送门

直接枚举显然是不行的,考虑减少枚举

显然第一头牛一定要被打到,所以合法位置一定在第一头牛的八个方向

因为牛多位置少,所以枚举能打到第一头牛的所有位置,在枚举那个位置能打到的牛的数量,如果全都可以打到就记入答案

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
inline int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
    return x*f;
}
const int N=507,xx[8]={0,1,1,1,0,-1,-1,-1},yy[8]={1,1,0,-1,-1,-1,0,1};
int n,m,mp[N][N],ans;
inline void check(int x,int y)
{
    int cnt=mp[x][y];
    for(int k=0;k<8;k++)
        for(int i=1;i<=n;i++)
        {
            int tx=x+xx[k]*i,ty=y+yy[k]*i;
            if(tx<1||tx>n||ty<1||ty>n) continue;
            cnt+=mp[tx][ty];
        }
    if(cnt==m) ans++;
}
int main()
{
    n=read(),m=read();
    int a,b;
    for(int i=1;i<=m;i++)
        a=read(),b=read(),mp[a][b]++;
    check(a,b);
    for(int k=0;k<8;k++)
        for(int i=1;i<=n;i++)
        {
            int tx=a+xx[k]*i,ty=b+yy[k]*i;
            if(tx<1||tx>n||ty<1||ty>n) continue;
            check(tx,ty);
        }
    printf("%d
",ans);
}
原文地址:https://www.cnblogs.com/LLTYYC/p/11410990.html