FZU-2188 River I "BFS"

Source: Internet
Author: User
Tags ming

FZU-2188 Across the river I
Time Limit: 3000MS Memory Limit: 32768KB 64bit IO Format: %i64d &%i64u

Submit Status

Description one day, Xiaoming needs to transport x sheep and y wolves to the opposite river. The ship can accommodate n animals and xiaoming. Every time Xiao Ming boating, must have at least one animal to accompany him, otherwise he will feel bored, uneasy. Whether on board or on shore, the wolf will eat the sheep if it exceeds the sheep. Xiao Ming needs to send all the animals to the opposite, and no sheep to be eaten, at least how many times he can cross the river.

Input has multiple sets of data, the first line of each group entered 3 integers want x, y, N (0≤x, y,n≤200)

Output If all animals can be sent across the river, and no sheep die, an integer is exported: the fewest number of times. Otherwise output-1.

Sample Input 3 3 233 33 3

Sample Output 11-1

Hint

The first example

Number of ships direction left bank right bank (Wolf sheep)

0:0 0 3 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 1

7:0 2 > 2 0 1 3

8:1 0 < 3 0 0 3

9:2 0 > 1 0 2 3

10:1 0 < 2 0 1 3

11:2 0 > 0 0 3 3

Source FOJ Prize Month-March 2015

#include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <cmath
> #include <queue> using namespace std;
int x,y,n;
int vis[210][210];
	struct node{int w,s;
int step;
}s,e;
	int BFS () {memset (vis,0,sizeof (VIS));
	queue<node>q;vis[0][0]=1; S.s=s.w=s.step=0; 
	Q.push (s); while (! Q.empty ()) {Node now=q.front ();
		Q.pop ();
				for (int i=0;i<=x-now.s;++i) {for (int. j=0;j<=y-now.w;++j) {if (i&&j>i) break;
				if (i+j==0) continue;
				if (i+j>n) break;
				Node u;
				U.s=now.s+i;u.w=now.w+j;
				if (u.s<u.w&&u.s!=0) continue;
				if (x-u.s<y-u.w&&x-u.s!=0) continue;
					if (!VIS[U.S][U.W]) {u.step=now.step+1;
					if (u.s==x&&u.w==y) return u.step;
							for (int a=0;a<=u.s;++a) {for (int. b=0;b<=u.w;++b) {if (a&&a<b) break;
							if (a+b==0) continue;
							if (a+b>n) break;
							Node v;
							V.s=u.s-a;v.w=u.w-b; if (v.s<v.w&&v.s!=0) contInue;
							if (x-v.s<y-v.w&&x-v.s!=0) continue;
							if (VIS[V.S][V.W]) continue;
							Vis[v.s][v.w]=1;
							v.step=u.step+1;
						Q.push (v);
}}}}} return-1;
	} int main () {while (scanf ("%d%d%d", &x,&y,&n)!=eof) {printf ("%d\n", BFS ());
} 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.