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.
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.
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.
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".
3 2 5 0 2 2 0 3 20 0 2 2 0 2 1 3 5 3 0 0 4 5 5
not possible 10 2
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
题意:
传统货币和信息技术货币兑换为新型货币。兑换方式为:传统货币和^2+信息技术货币和^2=新型货币^2.
每一行的两个货币分别为传统货币和信息技术货币,且必须一起使用,每行货币的使用次数不限。求转换到所给新型货币所需的次数。
题解:
运用广搜,给出几行货币就有几个方向,注意标记出现的各种情况,不然内存超限。
我看了几位大神的博客,都是用的完全背包做的,有兴趣的可以在网上搜下。
代码如下:
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
int vis[1100][1100];
struct note
{
int x,y;
} q[1100];
struct node
{
int x,y,step;
}Q;
int n,m,flag,sum;
void bfs(int x,int y)
{
queue<node>Q;
node now,tmp;
now.x=x;
now.y=y;
now.step=0;
vis[x][y]=1;
Q.push(now);
while(!Q.empty())
{
now=Q.front();
Q.pop();
for(int i=0; i<n; i++)
{
tmp=now;
int tx=tmp.x+q[i].x;
int ty=tmp.y+q[i].y;
if(tx>m || ty>m || vis[tx][ty])
continue;
tmp.step++;
tmp.x=tx;
tmp.y=ty;
vis[tx][ty]=1;
if(tx*tx+ty*ty==m*m)
{
sum=tmp.step;
flag=1;
return;
}
Q.push(tmp);
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int i,j;
scanf("%d%d",&n,&m);
for(i=0; i<n; i++)
scanf("%d%d",&q[i].x,&q[i].y);
flag=0;
sum=0;
memset(vis,0,sizeof(vis));
bfs(0,0);
if(flag)
printf("%d\n",sum);
else
printf("not possible\n");
}
}