【ACM】HDU 6639 Faraway 2019杭电多校第六场1006(枚举)

本文详细解析了HDU 6639题目的算法解决方案,该题目要求根据士兵们提供的信息,计算可能的目标位置数量。通过使用模运算和坐标遍历的方法,文章提供了一段C++代码实现,展示了如何高效地解决这一复杂问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6639

Faraway

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 268    Accepted Submission(s): 137


 

Problem Description

n soldiers are dispatched to somewhere in Byteland. These soldiers are going to set off now, but the target location is not so clear.

Assume the target location is at (xe,ye), it is clear that xe,ye are both non-negative integers within [0,m]. For the i-th soldier, the only thing he knows is that (|xi−xe|+|yi−ye|)modki=ti.

To find the correct target location, these soldiers are working on the information they have now. Please write a program to figure out the number of possible target locations.

 

 

Input

The first line of the input contains an integer T(1≤T≤10), denoting the number of test cases.

In each test case, there are two integers n,m(1≤n≤10,1≤m≤109) in the first line, denoting the number of soldiers and the upper bound of xe,ye.

For the next n lines, each line contains four integers xi,yi,ki,ti(0≤xi,yi≤m,2≤ki≤5,0≤ti<ki), denoting what each soldier knows.

 

 

Output

For each test case, print a single line containing an integer, denoting the number of possible target locations.

 

 

Sample Input

 

2

2 5

1 2 4 2

3 1 2 1

2 5

1 2 4 2

1 2 4 3

 

 

Sample Output

 

10

0

题目大意:

有n个形如 |x-a|+|y-b| mod k =t 的等式,给出a,b,k,t,求同时满足所有的等式的x,y有多少种

思路:

代码:

#include <bits/stdc++.h>
using namespace std;
int a[15],b[15],x[15],y[15],xe[15],ye[15];
int check(int xx,int yy,int n)
{
    for(int i=1;i<=n;i++)
    {
        if((abs(xx-xe[i])+abs(yy-ye[i]))%a[i]!=b[i])
            return 0;
    }
    return 1;
}
int cal(int l,int r)
{
    r-=l;
    if(r<0)
        return 0;
    else
        return r/60+1;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int t,n,m;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        long long ans=0;
        for(int i=1;i<=n;i++)
            scanf("%d%d%d%d",&xe[i],&ye[i],&a[i],&b[i]);
        x[0]=0,y[0]=0;//边界
        x[n+1]=m+1,y[n+1]=m+1;
        for(int i=1;i<=n;i++)
            x[i]=xe[i],y[i]=ye[i];
        sort(x,x+n+2);
        sort(y,y+n+2);
//        for(int i=0;i<=n+1;i++)
//            printf("%d\n",x[i]);
        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<=n;j++)
            {
                for(int k=0;k<60;k++)
                {
                    for(int h=0;h<60;h++)
                    {
                        int xx=x[i]+k,yy=y[j]+h;
                        if(check(xx,yy,n))
                            ans+=(long long)cal(xx,x[i+1]-1)*cal(yy,y[j+1]-1);
                    }
                }
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值