hdu4490 Mad Veterinarian(简单bfs)

Mad Veterinarian

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 168    Accepted Submission(s): 58
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

题目大意:有3种机器,A,B,C,分别可以将动物a,b,c转换成其他的动物。给定初始的a,b,c三种动物的数量,问能否换成给定数量的a,b,c三种动物,注意机器的转化是可逆的。

题目分析:求最少的步数,想到bfs,一共也就是6种操作。不过此题的ws之处在于没有给数据范围。所以搜索的时候判重就有问题。比赛的时候抱着试一试的心态,一狠心把50以上的数据全部掐掉,一提交竟然神奇的过掉了。比完又提交几次,经过验证,里面的数据是不超过8的!!

这套题的样例输出比较烦人,小心即可。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<cctype>
#include<map>
#include<string>
#include<set>
#include<queue>
#include<vector>

using namespace std;
const int N = 100005;
const int M = 8;
const int INF = 0x3f3f3f3f;
const double eps = 1e-5;
const double PI = acos(-1.0);
typedef __int64 ll;
map<int,int> mp[M][M];

struct node
{
    int num[3];
    string ans;
}ss,now;

int lcm[3][3];
int sa,sb,sc,ea,eb,ec;
struct wocao
{
    struct node t[N];
    int head,tail;
    void init()
    {
        head = tail = 0;
    }
    bool empty()
    {
        return head == tail;
    }
    void push(struct node a)
    {
        t[tail] = a;
        tail ++;
        if(tail >= N)
            tail -= N;
    }
    struct node front()
    {
        return t[head];
    }
    void pop()
    {
        head ++;
        if(head >= N)
            head -= N;
    }
}q;
void bfs()
{
    ss.ans.clear();
    ss.num[0] = sa;ss.num[1] = sb;ss.num[2] = sc;
    for(int i = 0;i < M;i ++)
        for(int j = 0;j < M;j ++)
            mp[i][j].clear();
    mp[sa][sb][sc] = 1;
    q.init();
    q.push(ss);
    while(!q.empty())
    {
        now = q.front();
        //now = q.top();
        q.pop();
        int i;
        for(i = 0;i < 3;i ++)
        {
            ss = now;
            if(ss.num[i])
            {
                ss.num[i] --;
                ss.num[0] += lcm[i][0];
                ss.num[1] += lcm[i][1];
                ss.num[2] += lcm[i][2];
                if(ss.num[0] >= M || ss.num[1] >= M || ss.num[2] >= M)
                    continue;
                if(mp[ss.num[0]][ss.num[1]].find(ss.num[2]) == mp[ss.num[0]][ss.num[1]].end())
                {
                    mp[ss.num[0]][ss.num[1]][ss.num[2]] = 1;
                    ss.ans += 'A' + i;
                    if(ss.num[0] == ea && ss.num[1] == eb && ss.num[2] == ec)
                    {
                        cout<<ss.ans.length()<<" "<<ss.ans<<endl;
                        return;
                    }
                    q.push(ss);
                }
            }
        }
        for(i = 0;i < 3;i ++)
        {
            ss = now;
            if(ss.num[0] >= lcm[i][0] && ss.num[1] >= lcm[i][1] && ss.num[2] >= lcm[i][2])
            {
                ss.num[0] -= lcm[i][0];
                ss.num[1] -= lcm[i][1];
                ss.num[2] -= lcm[i][2];
                ss.num[i] ++;
                if(ss.num[0] >= M || ss.num[1] >= M || ss.num[2] >= M)
                    continue;
                if(mp[ss.num[0]][ss.num[1]].find(ss.num[2]) == mp[ss.num[0]][ss.num[1]].end())
                {
                    mp[ss.num[0]][ss.num[1]][ss.num[2]] = 1;
                    ss.ans += 'a' + i;
                    if(ss.num[0] == ea && ss.num[1] == eb && ss.num[2] == ec)
                    {
                        cout<<ss.ans.length()<<" "<<ss.ans<<endl;
                        return;
                    }
                    q.push(ss);
                }
            }
        }
    }
    puts("NO SOLUTION");
}

int main()
{
    int cas,t,n;
    int i,j;
    scanf("%d",&t);
    while(t --)
    {
        scanf("%d%d",&cas,&n);
        for(i = 0;i < 3;i ++)
        {
            for(j = 0;j < 3;j ++)
            {
                scanf("%d",&lcm[i][j]);
            }
        }
        int ca;
        printf("%d %d\n",cas,n);
        while(n --)
        {
            scanf("%d%d%d%d%d%d%d",&ca,&sa,&sb,&sc,&ea,&eb,&ec);
            printf("%d ",ca);
            bfs();
        }
    }
    return 0;
}
//500MS	4548K   M取50
//0MS	436K    M取8



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值