UPC9630 Mad Veterinarian

问题 G: Mad Veterinarian

时间限制: 1 Sec  内存限制: 128 MB  Special Judge
提交: 23  解决: 8
[提交] [状态] [命题人:admin]
题目描述
Mad Veterinarian puzzles have a mad veterinarian, who has developed several machines that can transform an animal into one or more animals and back again. The puzzle is then to determine if it is possible to change one collection of animals into another by applying the machines in some order (forward or reverse). For example:
Machine A turns one ant into one beaver.
Machine B turns one beaver into one ant, one beaver and one cougar.
Machine C turns one cougar into one ant and one beaver.
Can we convert a beaver and a cougar into 3 ants?
Yes. { b,c } C → { a,2b } A reversed → { 2a,b } A reversed → 3a
Can we convert one ant into 2 ants? NO
These puzzles have the properties that:
1. In forward mode, each machine converts one animal of a given species into a finite, non-empty collection of animals from the species in the puzzle.
2. Each machine can operate in reverse.
3. There is one machine for each species in the puzzle and that machine (in forward mode) takes as input one animal of that species.
Write a program to find the shortest solution (if any) to Mad Veterinarian puzzles. For this problem we will restrict to Mad Veterinarian puzzles with exactly three machines, A, B, C. 

 

输入
The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. Each data set consists of several lines of input. Each data set should be processed identically and independently.
The first line of each data set consists of two decimal integers separated by a single space. The first integer is the data set number. The second integer is the number, N, of puzzle questions. The next three input lines contain the descriptions of machines A, B and C in that order. Each machine description line consists of three decimal integers separated by spaces giving the number of animals of type a, b and c output for one input animal. The following N lines give the puzzle questions for the Mad Veterinarian puzzle. Each contains seven decimal digits separated by single spaces: the puzzle number, the three starting animal counts for animals a, b and c followed by the three desired ending animal counts for animals a, b and c.

 

输出
For each input data set there are multiple lines of output. The first line of output for each data set contains the data set number, a space and the number of puzzle questions (N). For each puzzle question, there is one line of output which consists of the puzzle question number followed by a space,followed by ‘NO SOLUTION’ (without the quotes) if there is no solution OR the puzzle question number followed by the shortest number of machine steps used, a space and a sequence of tetters [A B C a b c] with capital tetters indicating applying the machine in the forward direction and lower case tetters indicating applying the machine in the reverse direction.

 

样例输入

复制样例数据

2
1 2
0 1 0
1 1 1
1 1 0
1 0 1 1 3 0 0
2 1 0 0 2 0 0
2 2
0 3 4
0 0 5
0 0 3
1 2 0 0 0 0 5
2 2 0 0 0 0 4
样例输出
1 2
1 3 aCa
2 NO SOLUTION
2 2
1 NO SOLUTION
2 25 AcBcccBccBcccAccBccBcccBc

这题没有给数据范围,但是后来根据学长亲测数据范围很小,所以其实就是很水的bfs找到最短路然后记录路径输出就行了。
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int tmp=31;
struct node
{
    int a,b,c,pre;
    char op;
};
node f[5],que[1005],s,e;
bool vis[15][15][15];
int head,tail;
int bfs()
{
    while(head<tail)
    {
        node now=que[head++];//cout<<now.a<<" "<<now.b<<" "<<now.c<<endl;
        if(now.a>0)
        {
            que[tail].a=now.a-1+f[1].a;
            que[tail].b=now.b+f[1].b;
            que[tail].c=now.c+f[1].c;
            que[tail].pre=head-1;
            que[tail].op='A';
            if(!vis[que[tail].a][que[tail].b][que[tail].c]&&que[tail].a<10&&que[tail].b<10&&que[tail].c<10)
            {
                if(que[tail].a==e.a&&que[tail].b==e.b&&que[tail].c==e.c)
                {
                    e.pre=tail;
                    return tail;
                }
                vis[que[tail].a][que[tail].b][que[tail].c]=1;
                tail++;
            }
        }
        if(now.b>0)
        {
            que[tail].a=now.a+f[2].a;
            que[tail].b=now.b+f[2].b-1;
            que[tail].c=now.c+f[2].c;
            que[tail].pre=head-1;
            que[tail].op='B';
            if(!vis[que[tail].a][que[tail].b][que[tail].c]&&que[tail].a<10&&que[tail].b<10&&que[tail].c<10)
            {
                if(que[tail].a==e.a&&que[tail].b==e.b&&que[tail].c==e.c)
                {
                    e.pre=tail;
                    return tail;
                }
                vis[que[tail].a][que[tail].b][que[tail].c]=1;
                tail++;
            }
        }
        if(now.c>0)
        {
            que[tail].a=now.a+f[3].a;
            que[tail].b=now.b+f[3].b;
            que[tail].c=now.c+f[3].c-1;
            que[tail].pre=head-1;
            que[tail].op='C';
            if(!vis[que[tail].a][que[tail].b][que[tail].c]&&que[tail].a<10&&que[tail].b<10&&que[tail].c<10)
            {
                if(que[tail].a==e.a&&que[tail].b==e.b&&que[tail].c==e.c)
                {
                    e.pre=tail;
                    return tail;
                }
                vis[que[tail].a][que[tail].b][que[tail].c]=1;
                tail++;
            }
        }
        if(now.a>=f[1].a&&now.b>=f[1].b&&now.c>=f[1].c)
        {
            que[tail].a=now.a-f[1].a+1;
            que[tail].b=now.b-f[1].b;
            que[tail].c=now.c-f[1].c;
            que[tail].pre=head-1;
            que[tail].op='a';
            if(!vis[que[tail].a][que[tail].b][que[tail].c]&&que[tail].a<10&&que[tail].b<10&&que[tail].c<10)
            {
                if(que[tail].a==e.a&&que[tail].b==e.b&&que[tail].c==e.c)
                {
                    e.pre=tail;
                    return tail;
                }
                vis[que[tail].a][que[tail].b][que[tail].c]=1;
                tail++;
            }
        }
        if(now.a>=f[2].a&&now.b>=f[2].b&&now.c>=f[2].c)
        {
            que[tail].a=now.a-f[2].a;
            que[tail].b=now.b-f[2].b+1;
            que[tail].c=now.c-f[2].c;
            que[tail].pre=head-1;
            que[tail].op='b';
            if(!vis[que[tail].a][que[tail].b][que[tail].c]&&que[tail].a<10&&que[tail].b<10&&que[tail].c<10)
            {
                if(que[tail].a==e.a&&que[tail].b==e.b&&que[tail].c==e.c)
                {
                    e.pre=tail;
                    return tail;
                }
                vis[que[tail].a][que[tail].b][que[tail].c]=1;
                tail++;
            }
        }
        if(now.a>=f[3].a&&now.b>=f[3].b&&now.c>=f[3].c)
        {
            que[tail].a=now.a-f[3].a;
            que[tail].b=now.b-f[3].b;
            que[tail].c=now.c-f[3].c+1;
            que[tail].pre=head-1;
            que[tail].op='c';
            if(!vis[que[tail].a][que[tail].b][que[tail].c]&&que[tail].a<10&&que[tail].b<10&&que[tail].c<10)
            {
                if(que[tail].a==e.a&&que[tail].b==e.b&&que[tail].c==e.c)
                {
                    e.pre=tail;
                    return tail;
                }
                vis[que[tail].a][que[tail].b][que[tail].c]=1;
                tail++;
            }
        }
    }
    return 0;
}
void init()
{
    head=tail=0;
    memset(vis,0,sizeof vis);
}
int main()
{
    int _,k,n,kk,t,cnt=0;
    char ans[1005];
    scanf("%d",&_);
    while(_--)
    {
        scanf("%d%d",&k,&n);
        printf("%d %d\n",k,n);
        for(int i=1; i<=3; i++)
            scanf("%d%d%d",&f[i].a,&f[i].b,&f[i].c);
        while(n--)
        {
            init();
            cnt=0;
            scanf("%d%d%d%d%d%d%d",&kk,&s.a,&s.b,&s.c,&e.a,&e.b,&e.c);
            s.pre=-1;
            que[tail++]=s;
            t=bfs();
            if(t)
            {
                while(t>0)
                {
                    ans[++cnt]=que[t].op;
                    t=que[t].pre;
                    //cout<<t<<endl;
                }
                printf("%d %d ",kk,cnt);
                for(int i=cnt; i>=1; i--)
                    printf("%c",ans[i]);
                puts("");
            }
            else
                printf("%d NO SOLUTION\n",kk);
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/liuquanxu/p/11426431.html

(Telephone Number Word Generator) Standard telephone keypads contain the digits 0 through 9. The numbers 2 through 9 each have three letters associated with them, as is indicated by the following table: Many people find it difficult to memorize phone numbers, so they use the correspondence between digits and letters to develop seven-letter words that correspond to their phone numbers. For example, a person whose telephone number is 686-2377 might use the correspondence indi- cated in the above table to develop the seven-letter word “NUMBERS.” Businesses frequently attempt to get telephone numbers that are easy for their clients to remember. If a business can advertise a simple word for its customers to dial, then no doubt the business will receive a few more calls. Each seven-letter word corresponds to exactly one seven-digit telephone number. The restaurant wishing to increase its take-home business could surely do so with the number 825-3688 (i.e., “TAKEOUT”). Each seven-digit phone number corresponds to many separate seven-letter words. Unfortunately, most of these represent unrecognizable juxtaposi- tions of letters. It’s possible, however, that the owner of a barber shop would be pleased to know that the shop’s telephone number, 424-7288, corresponds to “HAIRCUT.” A veterinarian with the phone number 738-2273 would be happy to know that the number corresponds to “PETCARE.” Write a program that, given a seven-digit number, writes to a file every possible seven-letter word corresponding to that number. There are 2187 (3 to the seventh power) such words. Avoid phone numbers with the digits 0 and 1.
最新发布
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值