题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1495
Sample Input 7 4 3 4 1 3 0 0 0 Sample Output NO 3
分析:虽然这里是作为搜索题出现的,但同时也算是道数学规律题吧,这写出来可就容易多了
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 #include <stdlib.h> 6 #include<queue> 7 #include<math.h> 8 using namespace std; 9 10 #define N 1250 11 #define maxn 115200 12 #define INF 0x3f3f3f3f7 4 3 13 14 int gcd(int a,int b) 15 { 16 return b==0?a:gcd(b,a%b); 17 } 18 19 int main() 20 { 21 int s,n,m,t; 22 23 while(scanf("%d%d%d",&s,&n,&m),n+m+s) 24 { 25 if(n<m) 26 t=n,n=m,m=t; 27 s/=gcd(n,m); 28 29 if(s%2 != 0) 30 printf("NO\n"); 31 else 32 printf("%d\n",s-1); 33 } 34 return 0; 35 }
附上学长广搜代码:
共6种倒法,开个数组用下标表示~
1 #include<stdio.h> 2 #include<string.h> 3 using namespace std; 4 5 const int maxn = 105; 6 const int oo = 0xfffffff; 7 8 struct node{int cup[3], step;}; 9 node q; 10 int Full[3]; 11 int dir[6][2] = { {0,1},{0,2},{1,0},{1,2},{2,0},{2,1} }; 12 int v[maxn][maxn][maxn]; 13 14 void Turn(int &a, int &b, int FullB)//把瓶子a里面的水倒入瓶子B 15 { 16 if(a+b <= FullB) 17 b += a, a=0; 18 else 19 { 20 a -= (FullB-b); 21 b = FullB; 22 } 23 } 24 int OK(node s)//判断是否已经分好 25 { 26 if(s.cup[0]+s.cup[1] == Full[0] && s.cup[0] == s.cup[1]) 27 return 1; 28 if(s.cup[0]+s.cup[2] == Full[0] && s.cup[0] == s.cup[2]) 29 return 1; 30 if(s.cup[1]+s.cup[2] == Full[0] && s.cup[1] == s.cup[2]) 31 return 1; 32 33 return 0; 34 } 35 int Bfs(node s) 36 { 37 queue<node> Q; 38 Q.push(s); 39 40 while(Q.size()) 41 { 42 s = Q.front();Q.pop(); 43 44 if(OK(s)) 45 return s.step; 46 47 for(int i=0; i<6; i++) 48 { 49 q = s; 50 Turn(q.cup[ dir[i][0] ], q.cup[ dir[i][1] ], Full[dir[i][1]]); 51 52 if(v[q.cup[0]][q.cup[1]][q.cup[2]] == 0) 53 { 54 v[q.cup[0]][q.cup[1]][q.cup[2]] = 1; 55 q.step++; 56 Q.push(q); 57 } 58 } 59 } 60 61 return -1; 62 } 63 64 int main() 65 { 66 node s; 67 68 while(scanf("%d%d%d", &Full[0], &Full[1], &Full[2]), Full[0]+Full[1]+Full[2]) 69 { 70 memset(v, 0, sizeof(v)); 71 72 s.cup[0] = Full[0]; 73 s.cup[1]=s.cup[2]=s.step=0; 74 int ans = Bfs(s); 75 76 if(ans == -1) 77 printf("NO\n"); 78 else 79 printf("%d\n", ans); 80 } 81 82 return 0; 83 }