hdu 4490

48 篇文章 0 订阅

Mad Veterinarian

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 267 Accepted Submission(s): 109
Special Judge


Problem Description
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?


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.

Input
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.

Output
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 letters [A B C a b c] with capital letters indicating applying the machine in the forward direction and lower case letters indicating applying the machine in the reverse direction.

Sample Input
  
  
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

Sample Output
  
  
1 2 1 3 Caa 2 NO SOLUTION 2 2 1 NO SOLUTION 2 25 AcBcccBccBcccAccBccBcccBc

Source

Recommend
liuyiding
水题,不解释
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
struct node {
    int a[3],step,last,dir;
};
int dir[6][3],prime[6][3];
node queue[1000];
int sx,sy,ex,ey,sz,ez;
int visit[10][10][10];
node qfront,temp;
int a[7];
char str[100];
bool canbfs(int d)
{
    int i;
    for(i=0;i<3;i++)
    {
        if(d>=3&&temp.a[i]<prime[d][i])
        {
            return false;
        }
        if(temp.a[i]<=0&&d==i)
        return false;
        if(a[i]<0||a[i]>10)
        return false;
    }
    return true;
}
int bfs()
{
    int s,e,i,j;

    s=e=0;
    queue[0].a[0]=sx,queue[0].a[1]=sy,queue[0].a[2]=sz,queue[0].step=0,queue[0].last=-1;
    visit[sx][sy][sz]=1;
    while(s<=e)
    {
        qfront=queue[s];

        for(i=0;i<6;i++)
        {
            temp=qfront;
            for(j=0;j<3;j++)
            {
                 a[j]=temp.a[j]+dir[i][j];
            }

            if(canbfs(i)&&!visit[a[0]][a[1]][a[2]])
            {
                visit[a[0]][a[1]][a[2]]=1;
                e++;
                queue[e].step=queue[s].step+1;
                for(j=0;j<3;j++)
                    queue[e].a[j]=a[j];
                queue[e].last=s;
                queue[e].dir=i;
                if(queue[e].a[0]==ex&&queue[e].a[1]==ey&&queue[e].a[2]==ez)
                {
                    printf("%d ",queue[e].step);
                    int step=queue[e].step;
                    str[step]='\0';
                    step--;
                    while(e!=-1)
                    {
                        if(queue[e].dir>=3)
                            str[step--]='a'+queue[e].dir-3;
                        else
                            str[step--]='A'+queue[e].dir;
                        e=queue[e].last;
                    }
                    printf("%s\n",str);
                    return 1;
                }
            }
        }
        s++;
    }
    return -1;
}
int main()
{
    int tcase,tt,ask,i,j;
    scanf("%d",&tcase);
    while(tcase--)
    {
        scanf("%d%d",&tt,&ask);
        printf("%d %d\n",tt,ask);
        for(i=0;i<3;i++)
        {
            for(j=0;j<3;j++)
            {
                scanf("%d",&dir[i][j]);
                prime[i][j]=dir[i][j];
                prime[i+3][j]=dir[i][j];

                dir[i+3][j]=-dir[i][j];
            }
            dir[i][i]--;
            dir[i+3][i]++;
        }
    /*
        {
            printf("\n");
            for(i=0;i<6;i++)
            {

                for(j=0;j<3;j++)
                {
                    printf("%d ",dir[i][j]);
                }
                 printf("\n");
            }
        }*/

        for(i=0;i<ask;i++)
        {
            scanf("%d%d%d%d%d%d%d",&tt,&sx,&sy,&sz,&ex,&ey,&ez);
            printf("%d ",tt);
            memset(visit,0,sizeof(visit));
            if(bfs()==-1)
            {
                printf("NO SOLUTION\n");
            }
        }

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值