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



华为Mate20手机原厂维修图纸 原理图 电路图 故障维修图(PDF版) 华为Mate20 位置图 点位图 位号图.pdf 华为Mate20 原理图 电路图.pdf 华为Mate20 注释图 故障标注 主板元器件位置图.pdf 华为Mate20 原厂图 维修流程图 AFC故障.pdf 华为Mate20 原厂图 维修流程图 FDD LTE 主集接收故障.pdf 华为Mate20 原厂图 维修流程图 FDD LTE分集接收故障.pdf 华为Mate20 原厂图 维修流程图 FDD LTE发射故障.pdf 华为Mate20 原厂图 维修流程图 GPS故障.pdf 华为Mate20 原厂图 维修流程图 GSM发射故障.pdf 华为Mate20 原厂图 维修流程图 GSM接收故障.pdf 华为Mate20 原厂图 维修流程图 HALL故障.pdf 华为Mate20 原厂图 维修流程图 LCD显示故障.pdf 华为Mate20 原厂图 维修流程图 NFC故障.pdf 华为Mate20 原厂图 维修流程图 OTG故障.pdf 华为Mate20 原厂图 维修流程图 PMU输出电压测量点与电压值.pdf 华为Mate20 原厂图 维修流程图 TD SCDMA 发射故障.pdf 华为Mate20 原厂图 维修流程图 TD SCDMA接收故障.pdf 华为Mate20 原厂图 维修流程图 TDD LTE主集接收故障.pdf 华为Mate20 原厂图 维修流程图 TDD LTE分集接收故障.pdf 华为Mate20 原厂图 维修流程图 TDD LTE发射故障.pdf 华为Mate20 原厂图 维修流程图 WCDMA发射故障.pdf 华为Mate20 原厂图 维修流程图 WCDMA接收故障.pdf 华为Mate20 原厂图 维修流程图 WIFI&BT故障.pdf 华为Mate20 原厂图 维修流程图 上电时序图及实物点测图.pdf 华为Mate20 原厂图 维修流程图 不开机(大电流).pdf 华为Mate20 原厂图 维修流程图 不开机(小电流).pdf 华为Mate20 原厂图 维修流程图 不开机(无电流).pdf 华为Mate20 原厂图 维修流程图 不识USB故障.pdf 华为Mate20 原厂图 维修流程图 不识主卡故障.pdf 华为Mate20 原厂图 维修流程图 不识副卡&SD卡.pdf 华为Mate20 原厂图 维修流程图 充电故障.pdf 华为Mate20 原厂图 维修流程图 前摄像打不开故障.pdf 华为Mate20 原厂图 维修流程图 前摄像打开黑屏花屏故障.pdf 华为Mate20 原厂图 维修流程图 后摄像1打开黑屏花屏故障.pdf 华为Mate20 原厂图 维修流程图 后摄像2打不开故障.pdf 华为Mate20 原厂图 维修流程图 后摄像2打开黑屏花屏故障.pdf 华为Mate20 原厂图 维修流程图 后摄像打不开故障.pdf 华为Mate20 原厂图 维修流程图 听筒故障.pdf 华为Mate20 原厂图 维修流程图 射频发射框架图.pdf 华为Mate20 原厂图 维修流程图 射频接收框架图.pdf 华为Mate20 原厂图 维修流程图 待机下电点测图.pdf 华为Mate20 原厂图 维修流程图 扬声器故障.pdf 华为Mate20 原厂图 维修流程图 指南针故障.pdf 华为Mate20 原厂图 维修流程图 指纹故障.pdf 华为Mate20 原厂图 维修流程图 按键故障.pdf 华为Mate20 原厂图 维修流程图 接近光故障.pdf 华为Mate20 原厂图 维修流程图 摄像头激光故障.pdf 华为Mate20 原厂图 维修流程图 气压计故障.pdf 华为Mate20 原厂图 维修流程图 红外故障.pdf 华为Mate20 原厂图 维修流程图 耳机故障.pdf 华为Mate20 原厂图 维修流程图 触摸故障.pdf 华为Mate20 原厂图 维修流程图 送话故障.pdf 华为Mate20 原厂图 维修流程图 重力感应故障.pdf 华为Mate20 原厂图 维修流程图 闪光灯故障.pdf 华为Mate20 原厂图 维修流程图 马达故障.pdf
华为Mate10手机原厂维修图纸 原理图 电路图 元件图 手机故障维修图 华为Mate10 位置图 位号图 元件图.exe 华为Mate10 原理图 电路图.exe 华为Mate10 注释图 故障标注 主板元器件位置图.exe 华为Mate10 高级维修手册.exe 华为Mate10 原厂图 维修流程图 CDMA发射故障.exe 华为Mate10 原厂图 维修流程图 CDMA接收故障.exe 华为Mate10 原厂图 维修流程图 FDD LTE主集接收故障1.exe 华为Mate10 原厂图 维修流程图 FDD LTE主集接收故障2.exe 华为Mate10 原厂图 维修流程图 FDD LTE分集接收故障1.exe 华为Mate10 原厂图 维修流程图 FDD LTE分集接收故障2.exe 华为Mate10 原厂图 维修流程图 FDD LTE发射故障1.exe 华为Mate10 原厂图 维修流程图 FDD LTE发射故障2.exe 华为Mate10 原厂图 维修流程图 GPS故障.exe 华为Mate10 原厂图 维修流程图 GSM发射故障.exe 华为Mate10 原厂图 维修流程图 GSM接收故障.exe 华为Mate10 原厂图 维修流程图 HALL感应故障(皮套模式).exe 华为Mate10 原厂图 维修流程图 LCD显示故障.exe 华为Mate10 原厂图 维修流程图 NFC故障.exe 华为Mate10 原厂图 维修流程图 PMU输出电压测量点与电压值.exe 华为Mate10 原厂图 维修流程图 TD SCDMA发射故障.exe 华为Mate10 原厂图 维修流程图 TD SCDMA接收故障.exe 华为Mate10 原厂图 维修流程图 TDD LTE主集接收故障.exe 华为Mate10 原厂图 维修流程图 TDD LTE分集接收故障.exe 华为Mate10 原厂图 维修流程图 TDD LTE发射故障.exe 华为Mate10 原厂图 维修流程图 VBATT、VBAT_SYS相关器件.exe 华为Mate10 原厂图 维修流程图 WCDMA发射故障.exe 华为Mate10 原厂图 维修流程图 WCDMA接收故障.exe 华为Mate10 原厂图 维修流程图 WIFI、BT故障.exe 华为Mate10 原厂图 维修流程图 上电时序图.exe 华为Mate10 原厂图 维修流程图 不开机(大电流).exe 华为Mate10 原厂图 维修流程图 不开机(小电流).exe 华为Mate10 原厂图 维修流程图 不开机(无电流).exe 华为Mate10 原厂图 维修流程图 不识SD卡.exe 华为Mate10 原厂图 维修流程图 不识USB.exe 华为Mate10 原厂图 维修流程图 不识主卡.exe 华为Mate10 原厂图 维修流程图 不识副卡.exe 华为Mate10 原厂图 维修流程图 充电故障.exe 华为Mate10 原厂图 维修流程图 前摄像故障.exe 华为Mate10 原厂图 维修流程图 受话故障.exe 华为Mate10 原厂图 维修流程图 后摄像故障.exe 华为Mate10 原厂图 维修流程图 射频发射框架图.exe 华为Mate10 原厂图 维修流程图 射频接收框架图.exe 华为Mate10 原厂图 维修流程图 常见维修案例.exe 华为Mate10 原厂图 维修流程图 常见维修案例2.exe 华为Mate10 原厂图 维修流程图 扬声器故障.exe 华为Mate10 原厂图 维修流程图 指南针故障.exe 华为Mate10 原厂图 维修流程图 指纹识别故障.exe 华为Mate10 原厂图 维修流程图 按键故障.exe 华为Mate10 原厂图 维修流程图 接近光故障.exe 华为Mate10 原厂图 维修流程图 老化维修.exe 华为Mate10 原厂图 维修流程图 耳机故障.exe 华为Mate10 原厂图 维修流程图 触摸故障.exe 华为Mate10 原厂图 维修流程图 送话故障.exe 华为Mate10 原厂图 维修流程图 重力感应故障.exe 华为Mate10 原厂图 维修流程图 闪光灯故障.exe 华为Mate10 原厂图 维修流程图 马达故障.exe
华为Mate10手机原厂维修图纸 原理图 电路图 故障维修图(PDF版)华为Mate10 位置图 点位图 位号图.pdf 华为Mate10 原理图 电路图.pdf 华为Mate10 注释图 故障标注 主板元器件位置图.pdf 华为Mate10 高级维修手册.pdf 华为Mate10 原厂图 维修流程图 CDMA发射故障.pdf 华为Mate10 原厂图 维修流程图 CDMA接收故障.pdf 华为Mate10 原厂图 维修流程图 FDD LTE主集接收故障1.pdf 华为Mate10 原厂图 维修流程图 FDD LTE主集接收故障2.pdf 华为Mate10 原厂图 维修流程图 FDD LTE分集接收故障1.pdf 华为Mate10 原厂图 维修流程图 FDD LTE分集接收故障2.pdf 华为Mate10 原厂图 维修流程图 FDD LTE发射故障1.pdf 华为Mate10 原厂图 维修流程图 FDD LTE发射故障2.pdf 华为Mate10 原厂图 维修流程图 GPS故障.pdf 华为Mate10 原厂图 维修流程图 GSM发射故障.pdf 华为Mate10 原厂图 维修流程图 GSM接收故障.pdf 华为Mate10 原厂图 维修流程图 HALL感应故障(皮套模式).pdf 华为Mate10 原厂图 维修流程图 LCD显示故障.pdf 华为Mate10 原厂图 维修流程图 NFC故障.pdf 华为Mate10 原厂图 维修流程图 PMU输出电压测量点与电压值.pdf 华为Mate10 原厂图 维修流程图 TD SCDMA发射故障.pdf 华为Mate10 原厂图 维修流程图 TD SCDMA接收故障.pdf 华为Mate10 原厂图 维修流程图 TDD LTE主集接收故障.pdf 华为Mate10 原厂图 维修流程图 TDD LTE分集接收故障.pdf 华为Mate10 原厂图 维修流程图 TDD LTE发射故障.pdf 华为Mate10 原厂图 维修流程图 VBATT、VBAT_SYS相关器件.pdf 华为Mate10 原厂图 维修流程图 WCDMA发射故障.pdf 华为Mate10 原厂图 维修流程图 WCDMA接收故障.pdf 华为Mate10 原厂图 维修流程图 WIFI、BT故障.pdf 华为Mate10 原厂图 维修流程图 上电时序图.pdf 华为Mate10 原厂图 维修流程图 不开机(大电流).pdf 华为Mate10 原厂图 维修流程图 不开机(小电流).pdf 华为Mate10 原厂图 维修流程图 不开机(无电流).pdf 华为Mate10 原厂图 维修流程图 不识SD卡.pdf 华为Mate10 原厂图 维修流程图 不识USB.pdf 华为Mate10 原厂图 维修流程图 不识主卡.pdf 华为Mate10 原厂图 维修流程图 不识副卡.pdf 华为Mate10 原厂图 维修流程图 充电故障.pdf 华为Mate10 原厂图 维修流程图 前摄像故障.pdf 华为Mate10 原厂图 维修流程图 受话故障.pdf 华为Mate10 原厂图 维修流程图 后摄像故障.pdf 华为Mate10 原厂图 维修流程图 射频发射框架图.pdf 华为Mate10 原厂图 维修流程图 射频接收框架图.pdf 华为Mate10 原厂图 维修流程图 常见维修案例.pdf 华为Mate10 原厂图 维修流程图 常见维修案例2.pdf 华为Mate10 原厂图 维修流程图 扬声器故障.pdf 华为Mate10 原厂图 维修流程图 指南针故障.pdf 华为Mate10 原厂图 维修流程图 指纹识别故障.pdf 华为Mate10 原厂图 维修流程图 按键故障.pdf 华为Mate10 原厂图 维修流程图 接近光故障.pdf 华为Mate10 原厂图 维修流程图 老化维修.pdf 华为Mate10 原厂图 维修流程图 耳机故障.pdf 华为Mate10 原厂图 维修流程图 触摸故障.pdf 华为Mate10 原厂图 维修流程图 送话故障.pdf 华为Mate10 原厂图 维修流程图 重力感应故障.pdf 华为Mate10 原厂图 维修流程图 闪光灯故障.pdf 华为Mate10 原厂图 维修流程图 马达故障.pdf
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值