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.
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.
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; }