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 See a friend, here with a friend's ideas Define a structure that records the number of sheep and wolves on a shore, and then every move, The number of shores No. 0 and 1th will vary, with many restrictions in the middle, #include <stdio.h> #include <string.h> #include <queue> #include <algorithm> using namespace std
;
struct ZZ {int A;
int b;
int l;//table count int w;//table position}p,d;
int N,m,mm,flag;
int v[2][210][210];
void BFs (int x,int y) {p.a=x;p.b=y;p.l=0;p.w=0;//initialize sheep and wolves all on shore No. 0 queue<zz>q;
Q.push (P);
int i,j;
while (!q.empty ()) {P=q.front ();
Q.pop ();
if (p.a==n&&p.b==m&&p.w==1)//satisfies the condition {flag=1;
printf ("%d\n", P.L);
return; } d.l=p.l+1;//times d.w=p.w==1?0:1;//change Shore for (i=0;i<=p.a;i++) {for (j=0;j<=p.b;j++) {if (i+j==0)//No Sheep
or wolf continue;
if (I+J>MM)//with animals exceeding the upper limit continue;
if (i<j&&i!=0)//with sheep less than wolves, note that at this time the number of sheep is not 0 continue;
if (p.a-i<p.b-j&&p.a-i!=0)//away after the number of sheep left less than the wolf continue;
d.a=n-p.a+i;//the number of sheep and wolves that will take the shore of the past d.b=m-p.b+j;
if (d.a<d.b&&d.a!=0)//If the number of sheep with the past is less than the wolf continue;
if (v[d.w][d.a][d.b])//If this scheme has used continue; v[d.w][d.a][d.b]=1;//Flag This scenario has been used Q.PUsh (d);
}}} printf (" -1\n");
} int main () {while (scanf ("%d%d%d", &n,&m,&mm)!=eof) {flag=0;
memset (v,0,sizeof (v));
V[0][n][m]=1;
BFS (N,M);
} return 0; } |