Fzu 2188 cross river I, fzu2188 cross river I

Source: Internet
Author: User

Fzu 2188 cross river I, fzu2188 cross river I

 

Http://acm.fzu.edu.cn/problem.php? Pid = 1, 2188

Crossing the river I Time Limit:3000 MS Memory Limit:32768KB 64bit IO Format:% I64d & % I64uSubmit Status Practice FZU 2188

Description

One day, James needs to transport x sheep and y Wolf to the river. The ship can accommodate n animals and James. Every time James goes boating, he must have at least one animal to accompany him. Otherwise, he will get bored and upset. If the number of wolves on the ship or on the shore exceeds that of the sheep, the wolf will eat the sheep. How many times does James need to send all the animals across the river without the sheep being eaten?

Input

There are multiple groups of data. Input three integers in the first row of each group: x, y, n (0 ≤ limit x, limit y, n ≤ limit 200)

Output

If you can send all animals across the river without the death of sheep, an integer is output: the minimum number of times. Otherwise, output-1.

Sample Input

3 3 2 33 3

Sample Output

11-1

Hint

Example 1

Number of times ship to left bank right bank (Wolf sheep)

0: 0 0 3 0 0

1: 2 0> 1 3 2 0

2: 1 0 <2 3 1 0

3: 2 0> 0 3 3 0

4: 1 0 <1 3 2 0

5: 0 2> 1 1 2 2

6: 1 1 <2 2 1

7: 0 2> 2 0 1 3

8: 1 0 <3 0 3

9: 2 0> 1 0 2 3

10:1 0 <2 0 1 3

11; 2 0> 0 0 3 3

 

Analysis:

 

Number of times

0; 3 3 0 0 0

1; 3 1 1 0 2

2; 3 2 0 0 1

3; 3 0 1 0 3

4; 3 1 0 0 2

5: 1 1 1 2 2

6: 2 2 0 1 1

7; 0 2 1 3 1

8; 0 3 0 3 0

9; 0 1 1 3 2

10; 0 2 0 3 1

11; 0 0 1 3 3

 

There are multiple branch description in the question;

 

"A ship can accommodate n animals" & "at least one animal can accompany him" & "if the number of wolves on the ship or on the shore exceeds that of the sheep, the wolf will eat the sheep "&" 0 ≤ limit x, limit y, n ≤ limit 200 ".

 

AC code:

1 # include <cstdio> 2 # include <iostream> 3 # include <cstring> 4 # include <algorithm> 5 # include <queue> 6 using namespace std; 7 bool vis [210] [210] [2]; 8 int sx, sy, n; 9 struct node10 {11 int x, y; 12 int c; 13 int cnt; 14}; 15 node cur, nxt; 16 queue <node> que; 17 int main () 18 {19 while (~ Scanf ("% d", & sx, & sy, & n) 20 {21 memset (vis, 0, sizeof (vis); 22 while (! Que. empty () que. pop (); 23 cur. x = sx, cur. y = sy; 24 cur. c = 0, cur. cnt = 0; // cur. c = 0 represents the left bank, cur. c = 1 indicates the right bank, cur. cnt indicates the number of steps. 25 vis [sx] [sy] [0] = 1; 26 // vis [x] [y] [2] indicates that after the ship reaches 0 or 1, the number of sheep x on the shore, and the number y on the wolf, marking the array. 27 que. push (cur); 28 bool flag = 0; 29 while (! Que. empty () 30 {31 cur = que. front (); 32 que. pop (); 33 if (cur. c = 1 & cur. x = sx & cur. y = sy) 34 {35 flag = true; 36 printf ("% d \ n", cur. cnt); 37 break; 38} 39 nxt. c =! Cur. c; 40 nxt. cnt = cur. cnt + 1; 41 for (int I = 0; I <= cur. x; I ++) // I indicates the number of sheep on the ship. 42 for (int j = 0; j <= cur. y; j ++) // j indicates the number of wolves on the ship. 43 {44 if (I + j = 0) continue; 45 if (I + j> n) continue; 46 if (I <j & I! = 0) continue; 47 if (cur. x-I <cur. y-j & cur. x-I! = 0) continue; 48 nxt. x = sx-cur.x + I, nxt. y = sy-cur.y + j; 49 if (nxt. x <nxt. y & nxt. x! = 0) continue; 50 if (vis [nxt. x] [nxt. y] [nxt. c]) continue; 51 vis [nxt. x] [nxt. y] [nxt. c] = 1; 52 que. push (nxt); 53} 54} 55 if (! Flag) puts ("-1"); 56} 57 return 0; 58}View Code

 

 

Related Article

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.