HDU 4490 mad veterinarian (BFS + linked list record path)

Source: Internet
Author: User

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4490

Mad veterinarian

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 279 accepted submission (s): 111
Special Judge

Problem description Mad veterinarianPuzzles 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.

 

Inputthe 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 shoshould 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, B and C.

 

Outputfor 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] with capital letters indicating applying the machine in the forward ction and lower case letters indicating applying the machine in the reverse direction.

 

Sample Input
21 20 1 01 1 11 1 01 0 1 1 3 0 02 1 0 0 2 0 02 20 3 40 0 50 0 31 2 0 0 0 0 52 2 0 0 0 0 4

 

Sample output
1 21 3 Caa2 NO SOLUTION2 21 NO SOLUTION2 25 AcBcccBccBcccAccBccBcccBc

 

Sourcegreater New York 2012

 

Recommendliuyiding

 

I don't want to read it again, but I want to vomit about it .... Tomorrow, let's talk about how I missed it all the way .....

No, it's not water, it's more water than me !!!!!!

3 machines for small animals. Each machine has a type of operation A, B, and C. You can also perform A, B, and C inverse operations ABC on small animals. Given the initial status and end status, ask if six operations can be performed to reach the initial status.

End status.

Analysis: Very bare BFS + record path.

Thoughts:

Many wrong and wrong locations -------------

1. a fatal error is that, instead of determining whether a can be changed, A, B, and C can be directly input and then corresponding to-1, which is processed as a secondary array and then searched. But this does not work. For example, the C operation is 0 0 2,

In my previous practice, when Z is 0, it can be changed to 1, but there are no small animals. How can this happen. There is no such magic in nature! Therefore, if this error occurs, you must first judge and then subtract the consumption.

Small animals.

2. Use macro definition in disorder. Define abcabc as a macro, and use EA, EB, and EC to record the final state. This is very dangerous, because macro is a direct replacement, and ea eb ec is not E1 E2 E3. No wonder debugging

The final state suddenly changed, but I didn't have a statement to change it.

3. The linked list pointer is disordered, leading to an endless loop. My understanding of linked list and memory is really too thin. I started to open only state cur and next. At last, we only changed the memory value corresponding to cur, and pointed the pointer to cur every time.

This memory space cannot be stopped at the end of the output. Now think about it. It's also called a linked list. Later, I opened a large array of State snode [100000] and put the State pointer in the queue. Traverse

Each time a pointer is obtained from the queue. Then, the new status is saved to the snode array, and the corresponding address is saved to the queue.

4. I started to make a very BT mistake. Why do I think it is okay if it is greater than-1? If it is less than-1, continue. It's been a long time since I forgot what I thought at first. In short, next. A. What do I think can be-1?

Next. A. What is vis [] [] [] to determine the subscript of the heavy array? As a result, the array is out of bounds.

5. In fact, set can be used to determine the weight. When the subscript may be negative, VIS cannot control the large data. For example, if it weren't for any unexpected 10 data, use if .. Continue

The VIS array cannot be controlled. (Set is slower than vis)

6. It is said that the hdu oj array is very small and can be used directly for table creation. In fact, this question can also be written using bitwise operations, because the status is only 12 at most (only for this question), using an integer int, the first 24 bits record three numbers of aa bb cc, and

The path selected for the 8-bit record. The addition or subtraction is the shift of the integer.

 

Code:

# Include <cstdio> # include <iostream> # include <cstring> # include <queue> using namespace STD; struct f {int X; int y; int Z ;} FF [3]; struct state {int AA; int BB; int cc; char step; State * pre;} s; int EA, EB, EC; char solve [1100]; bool vis [12] [12] [12]; int rear; struct state snode [100000]; bool BFS () {int r = 0; queue <state *> q; while (! Q. empty () Q. pop (); S. pre = NULL; q. push (& S); vis [S. AA] [S. BB] [S. CC] = true; while (! Q. empty () {struct state * pnow = Q. front (); q. pop (); If (pnow-> AA = EA & pnow-> BB = EB & pnow-> cc = EC) {rear = 0; // state * head; For (; pnow! = NULL; pnow = pnow-> pre) {solve [rear ++] = pnow-> step;} solve [rear] = '\ 0'; return true ;} for (INT I = 0; I <3; I ++) {if (I = 0 & pnow-> AA <1 | I = 1 & pnow-> BB <1 | I = 2 & pnow-> CC <1) continue; // The condition does not meet snode [R]. AA = pnow-> AA + FF [I]. x; snode [R]. BB = pnow-> BB + FF [I]. y; snode [R]. cc = pnow-> CC + FF [I]. z; // the animal that is consumed if (I = 0) -- snode [R]. AA; else if (I = 1) -- snode [R]. BB; else -- snode [R]. CC; If (snode [R]. AA> 10 | snode [R]. bb> 10 | snode [R]. CC> 10) continue; If (vis [snode [R]. AA] [snode [R]. BB] [snode [R]. CC] = false) {snode [R]. step = 'A' + I; snode [R]. pre = pnow; q. push (& snode [R]); vis [snode [R]. AA] [snode [R]. BB] [snode [R]. CC] = true; ++ R ;}// reverse order for (INT I = 0; I <3; ++ I) {If (pnow-> BB <FF [I]. Y | pnow-> CC <FF [I]. z | pnow-> AA <FF [I]. x) continue; // The condition does not meet snode [R]. AA = pnow-> AA-FF [I]. x; snode [R]. BB = pnow-> BB-FF [I]. y; snode [R]. cc = pnow-> CC-FF [I]. z; // The added animal if (I = 0) ++ snode [R]. AA; else if (I = 1) ++ snode [R]. BB; else ++ snode [R]. CC; If (snode [R]. AA> 10 | snode [R]. bb> 10 | snode [R]. CC> 10) continue; If (vis [snode [R]. AA] [snode [R]. BB] [snode [R]. CC] = false) {snode [R]. step = 'A' + I; snode [R]. pre = pnow; q. push (& snode [R]); vis [snode [R]. AA] [snode [R]. BB] [snode [R]. CC] = true; ++ R ;}}return false;} int main () {int T, I, T1, T2, T3, Cs, Cas, FUC, J, d; scanf ("% d", & T); While (t --) {memset (FF, 0, sizeof (ff); scanf ("% d ", & CAS, & FUC); for (I = 0; I <3; I ++) {scanf ("% d", & T1, & T2, & T3); FF [I]. X = T1; FF [I]. y = t2; FF [I]. z = T3;} printf ("% d \ n", Cas, FUC); For (D = 0; D <FUC; D ++) {rear = 0; memset (VIS, 0, sizeof (VIS); scanf ("% d", & CS, & S. AA, & S. BB, & S. CC, & EA, & EB, & EC); If (BFS () {printf ("% d", Cs, strlen (solve )); if (strlen (solve)> 0) printf (""); For (INT Len = strlen (solve)-1; Len> = 0; -- Len) printf ("% C", solve [Len]); printf ("\ n");} else printf ("% d no solution \ n", CS );}} return 0 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.