POJ - 3021 e-Coins (BFS)

At the Department for Bills and Coins, an extension of today's monetary system has newly been proposed, in order to make it fit the new economy better. A number of new so called e-coins will be produced, which, in addition to having a value in the normal sense of today, also have an InfoTechnological value. The goal of this reform is, of course, to make justice to the economy of numerous dotcom companies which, despite the fact that they are low on money surely have a lot of IT inside. All money of the old kind will keep its conventional value and get zero InfoTechnological value. 

To successfully make value comparisions in the new system, something called the e-modulus is introduced. This is calculated as SQRT(X*X+Y*Y), where X and Y hold the sums of the conventional and InfoTechnological values respectively. For instance, money with a conventional value of $3 altogether and an InfoTechnological value of $4 will get an e-modulus of $5. Bear in mind that you have to calculate the sums of the conventional and InfoTechnological values separately before you calculate the e-modulus of the money. 

To simplify the move to e-currency, you are assigned to write a program that, given the e-modulus that shall be reached and a list of the different types of e-coins that are available, calculates the smallest amount of e-coins that are needed to exactly match the e-modulus. There is no limit on how many e-coins of each type that may be used to match the given e-modulus.
Input
A line with the number of problems n (0 < n<=100), followed by n times:
  • A line with the integers m (0 < m<=40) and S (0 < S<=300), where m indicates the number of different e-coin types that exist in the problem, and S states the value of the e-modulus that shall be matched exactly.
  • m lines, each consisting of one pair of non-negative integers describing the value of an e-coin. The first number in the pair states the conventional value, and the second number holds the InfoTechnological value of the coin.
When more than one number are present on a line, they will be separated by a space. Between each problem, there will be one blank line.
Output
The output consists of n lines. Each line contains either a single integer holding the number of coins necessary to reach the specified e-modulus S or, if S cannot be reached, the string "not possible".
Sample Input
3 
2 5 
0 2 
2 0 

3 20 
0 2 
2 0 
2 1 

3 5 
3 0 
0 4 
5 5 
Sample Output
not possible 
10 
2
Hint
 
The illustration examplifies adding 8 coins of conventional value 2 and InfoTechnological value 1, and 2 coins with pure InfoTechnological value 2. The e-modulus is of course 20 as SQRT((8*2+2*0)^2+(8*1+2*2)^2)=SQRT(16^2+12^2)=20 
        题目大意:给你几组a,b;任取几个a,b,使其满足a*a+b*b=s*s;问所取得最少的个数。a,b,可取多次。

     题意大概就是这个样子,但是我一直纠结第二个测试中10是怎么的来的快哭了,谁看出来了麻烦告诉我哈。计数方式感觉没看懂--

思路:最简单也是最笨的方法当然是搜索啦,,看了好多别人写的,有用动归的,还有贪心的,甚至推公式的。虽然代码简短,但不好想啊。像我这种一问三不知的人,肯定不会,还是老老实实的·用搜索搜吧害羞

#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;

int n,s,m,sum;
int vis[2100][2100];//注意数组不能太小,也不能太大,500~4000多就好了.少了会runtime,多了会MLE....

struct work
{
    int x,y;
}st[2100];
struct node
{
    int x,y,step;
};

int bfs()
{
    queue<node>Q;
    node p,q;
    p.x=0;
    p.y=0;
    vis[0][0]=1;
    p.step=0;
    Q.push(p);
    while(!Q.empty())
    {
        p=Q.front();
        Q.pop();
        if(p.x*p.x+p.y*p.y==s*s)
        {
            return p.step;
        }
        for(int i=0;i<m;i++)
        {
            q.x=p.x+st[i].x;
            q.y=p.y+st[i].y;
            if(q.x*q.x+q.y*q.y<=s*s&&!vis[q.x][q.y])//注意一下这里!!
            {
                q.step=p.step+1;
                vis[q.x][q.y]=1;
                Q.push(q);
            }
        }
    }
    return -1;
}

int main()
{
     scanf("%d",&n);
     while(n--)
     {
         memset(vis,0,sizeof(vis));
         scanf("%d%d",&m,&s);
         for(int i=0;i<m;i++)
            scanf("%d%d",&st[i].x,&st[i].y);
        int f=bfs();
         if(f>=0)printf("%d\n",f);
         else printf("not possible\n");
     }
     return 0;
}



转载于:https://www.cnblogs.com/zitian246/p/9123644.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值