Mad Veterinarian

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 249    Accepted Submission(s): 104
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
 
题意:有三个机器ABC,可以分别把a,b,c变成对应的某几个东西,也可以逆着变,求变到目标状态的最少步数

很水的题,不过我写了好长好长,太不符合我的风格了,刚开始调试的时候无解的情况表示不出来,因为它的数据可以一直往上涨,最后限制了一下,看学长的博客说的最多8个,加这个限制就可以了,好不容易调试出来了,结果跟示例对不上,过了好久才想到方法可能不止一种,于是提交了,结果就AC了。。。0ms

写的有点长,不过很多就是差不多的,看起来应该不费力吧,这题的输入数据很蛋疼要小心

#include<stdio.h>
#include<string.h>
#define M 8
struct node
{
int a,b,c,f;
char o;
}f[4],s[1000],start,end;
int visit[10][10][10];
int bfs()
{
int t=0,w=1;
memset(visit,0,sizeof(visit));
visit[s[t].a][s[t].b][s[t].c]=1;
s[0].f=-1;
while(t<w)
{
if(s[t].a>0)
{
s[w].a=s[t].a+f[0].a-1;
s[w].b=s[t].b+f[0].b;
s[w].c=s[t].c+f[0].c;
s[w].f=t;
s[w].o='A';
if(s[w].a<M&&s[w].b<M&&s[w].c<M&&!visit[s[w].a][s[w].b][s[w].c])
{
if(s[w].a==end.a&&s[w].b==end.b&&s[w].c==end.c)
{
end.f=w;
return w;
}
visit[s[w].a][s[w].b][s[w].c]=1;
w++;
}
}
if(s[t].b>0)
{
s[w].b=s[t].b+f[1].b-1;
s[w].a=s[t].a+f[1].a;
s[w].c=s[t].c+f[1].c;
s[w].f=t;
s[w].o='B';
if(s[w].a<M&&s[w].b<M&&s[w].c<M&&!visit[s[w].a][s[w].b][s[w].c])
{
if(s[w].a==end.a&&s[w].b==end.b&&s[w].c==end.c)
{
end.f=w;
return w;
}
visit[s[w].a][s[w].b][s[w].c]=1;
w++;
}
}
if(s[t].c>0)
{
s[w].c=s[t].c+f[2].c-1;
s[w].b=s[t].b+f[2].b;
s[w].a=s[t].a+f[2].a;
s[w].f=t;
s[w].o='C';
if(s[w].a<M&&s[w].b<M&&s[w].c<M&&!visit[s[w].a][s[w].b][s[w].c])
{
if(s[w].a==end.a&&s[w].b==end.b&&s[w].c==end.c)
{
end.f=w;
return w;
}
visit[s[w].a][s[w].b][s[w].c]=1;
w++;
}
}
if(s[t].a>=f[0].a&&s[t].b>=f[0].b&&s[t].c>=f[0].c)
{
s[w].a=s[t].a-f[0].a+1;
s[w].b=s[t].b-f[0].b;
s[w].c=s[t].c-f[0].c;
s[w].f=t;
s[w].o='a';
if(s[w].a<M&&s[w].b<M&&s[w].c<M&&!visit[s[w].a][s[w].b][s[w].c])
{
if(s[w].a==end.a&&s[w].b==end.b&&s[w].c==end.c)
{
end.f=w;
return w;
}
visit[s[w].a][s[w].b][s[w].c]=1;
w++;
}
}
if(s[t].a>=f[1].a&&s[t].b>=f[1].b&&s[t].c>=f[1].c)
{
s[w].b=s[t].b-f[1].b+1;
s[w].a=s[t].a-f[1].a;
s[w].c=s[t].c-f[1].c;
s[w].f=t;
s[w].o='b';
if(s[w].a<M&&s[w].b<M&&s[w].c<M&&!visit[s[w].a][s[w].b][s[w].c])
{
if(s[w].a==end.a&&s[w].b==end.b&&s[w].c==end.c)
{
end.f=w;
return w;
}
visit[s[w].a][s[w].b][s[w].c]=1;
w++;
}
}
if(s[t].a>=f[2].a&&s[t].b>=f[2].b&&s[t].c>=f[2].c)
{
s[w].c=s[t].c-f[2].c+1;
s[w].b=s[t].b-f[2].b;
s[w].a=s[t].a-f[2].a;
s[w].f=t;
s[w].o='c';
if(s[w].a<M&&s[w].b<M&&s[w].c<M&&!visit[s[w].a][s[w].b][s[w].c])
{
if(s[w].a==end.a&&s[w].b==end.b&&s[w].c==end.c)
{
end.f=w;
return w;
}
visit[s[w].a][s[w].b][s[w].c]=1;
w++;
}
}
t++;
}
return 0;
}
int main()
{
int t,i,j,n,num,tt;
char cou[100];
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&num,&n);
printf("%d %d\n",num,n);
for(i=0;i<3;i++)
scanf("%d%d%d",&f[i].a,&f[i].b,&f[i].c);
while(n--)
{
scanf("%d%d%d%d%d%d%d",&tt,&s[0].a,&s[0].b,&s[0].c,&end.a,&end.b,&end.c);
printf("%d ",tt);
if(i=bfs())
{
for(j=98;i>0;j--)
{
cou[j]=s[i].o;
i=s[i].f;
}
cou[99]='\0';
printf("%d ",98-j);
puts(cou+j+1);
}
else puts("NO SOLUTION");
}
}
return 0;
}

hdu4490 Mad Veterinarian(bfs)的更多相关文章

  1. Regionals 2012, North America - Greater NY 解题报告

    这套题..除了几何的都出了 完全没时间学几何.杯具 A,B,J 水题不解释 C.Pen Counts 这题的话 写几个不等式限制边得范围就行了 然后枚举最小边 D.Maximum Random Wal ...

  2. Lesson 21 Mad or not?

    Text Aeroplanes are slowly driving me mad. I live near an airport and passing planes can be heard ni ...

  3. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  4. 【BZOJ-1656】The Grove 树木 BFS + 射线法

    1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 186  Solved: 118[Su ...

  5. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  6. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  7. Sicily 1215: 脱离地牢(BFS)

    这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...

  8. Sicily 1048: Inverso(BFS)

    题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...

  9. Sicily 1444: Prime Path(BFS)

    题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...

随机推荐

  1. BZOJ 4448: [Scoi2015]情报传递 树链剖分 主席树

    4448: [Scoi2015]情报传递 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4448 Description 奈特公司是一个巨 ...

  2. python学习笔记5.1-核心类型-集合set类型[转]

    转自:http://blog.csdn.net/business122/article/details/7541486 python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系 ...

  3. CentOS 7 下编译安装lnmp之nginx篇详解

    一.安装环境 宿主机=> win7,虚拟机 centos => 系统版本:CentOS Linux release 7.5.1804 (Core),ip地址 192.168.1.168   ...

  4. 关闭IE8的首次运行自定义设置

    方法一:顺着IE8的提示,一步一步的了解看完或设置完等的,它“推荐”的你应该做的事,然后重新设置首页就行了. 方法二:开始->运行->输入:gpedit.msc->用户配置-> ...

  5. cocos2d-x项目101次相遇:使用触摸事件移动 精灵

    cocos2d-x 101次相遇 / 文件夹  1   安装和环境搭建 -xcode  2   Scenes , Director, Layers, Sprites 3   建立图片菜单  4   在 ...

  6. IIS7.5 配置 PHP 5.3.5

    本机环境:IIS7.5 windows2008 64位 首先确认IIS中启用了CGI功能: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWVmaWdod ...

  7. JS经常使用字符串处理方法总结

    1.indexOf()方法.从前往后查找字符串位置.大写和小写敏感,从0開始计数.同理,lastIndexOf() 方法从后往前.两个方法对于同样的检索条件输出的结果是一样的 比如: <scri ...

  8. sql语句分组/排序/计算总数/连接等sql语句书写

    1.什么是表连接? 答:比如两张表,要获取的信息来自两张表,就需要通过外键的形式进行两张表的连接.最后产后组合信息. 表连接是通过join连接的.表连接说白了就是产生一个大表.表连接也都是用于查询上的 ...

  9. web开发常见bug汇总

    1.在做使用struts2进行文件上传时总是出现 java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOu ...

  10. 更改mysql字段的编码类型为utf8mb4

    ALTER TABLE tb_case MODIFY COLUMN content VARCHAR(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode ...