标签:lib 地址 bottom 知识 cst 要求 ... 两种 eps
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 23273 Accepted Submission(s): 9485
思路:可以进行bfs搜索(kuangbin也把这道题放进了搜索模块里面)
有六种状态,从a瓶到b瓶,a-->c
b-->a b-->c
c-->a c-->b
然后每种状态里面又分两种不同情况,可以将此瓶的水全部清空,不能清空......
然后广搜就可以了........
#include<iostream> #include<stdio.h> #include<string.h> #include<queue> using namespace std; int vist[105][105][105],a,b,c; struct node { int a,b,c; int step; }s[105]; int sum=0; void bfs() { queue<node>q; memset(vist,0,sizeof(vist)); node p1; p1.a=a; p1.b=0; p1.c=0; p1.step=0; q.push(p1); vist[p1.a][0][0]=1; while(!q.empty()) { p1=q.front(); q.pop(); if((p1.a==a/2&&p1.b==a/2)||(p1.a==a/2&&p1.c==a/2)||(p1.b==a/2&&p1.c==a/2)) { printf("%d\n",p1.step); return; } node p2; if(p1.a!=0) { if(p1.a>b-p1.b) { p2.a=p1.a-(b-p1.b); p2.b=b; p2.c=p1.c; p2.step=p1.step+1; } else { p2.a=0; p2.b=p1.b+p1.a; p2.c=p1.c; p2.step=p1.step+1; } if(!vist[p2.a][p2.b][p2.c]) { vist[p2.a][p2.b][p2.c]=1; q.push(p2); } } if(p1.a!=0) { if(p1.a>c-p1.c) { p2.a=p1.a-(c-p1.c); p2.b=p1.b; p2.c=c; p2.step=p1.step+1; } else { p2.a=0; p2.b=p1.b; p2.c=p1.c+p1.a; p2.step=p1.step+1; } if(!vist[p2.a][p2.b][p2.c]) { vist[p2.a][p2.b][p2.c]=1; q.push(p2); } } if(p1.b!=0) { if(p1.b>a-p1.a) { p2.b=p1.b-(a-p1.a); p2.a=a; p2.c=p1.c; p2.step=p1.step+1; } else { p2.b=0; p2.a=p1.a+p1.b; p2.c=p1.c; p2.step=p1.step+1; } if(!vist[p2.a][p2.b][p2.c]) { vist[p2.a][p2.b][p2.c]=1; q.push(p2); } } if(p1.b!=0) { if(p1.b>c-p1.c) { p2.b=p1.b-(c-p1.c); p2.a=p1.a; p2.c=c; p2.step=p1.step+1; } else { p2.b=0; p2.a=p1.a; p2.c=p1.c+p1.b; p2.step=p1.step+1; } if(!vist[p2.a][p2.b][p2.c]) { vist[p2.a][p2.b][p2.c]=1; q.push(p2); } } if(p1.c!=0) { if(p1.c>a-p1.a) { p2.c=p1.c-(a-p1.a); p2.a=a; p2.b=p1.b; p2.step=p1.step+1; } else { p2.c=0; p2.a=p1.a+p1.c; p2.b=p1.b; p2.step=p1.step+1; } if(!vist[p2.a][p2.b][p2.c]) { vist[p2.a][p2.b][p2.c]=1; q.push(p2); } } if(p1.c!=0) { if(p1.c>b-p1.b) { p2.c=p1.c-(b-p1.b); p2.a=p1.a; p2.b=b; p2.step=p1.step+1; } else { p2.c=0; p2.a=p1.a; p2.b=p1.b+p1.c; p2.step=p1.step+1; } if(!vist[p2.a][p2.b][p2.c]) { vist[p2.a][p2.b][p2.c]=1; q.push(p2); } } } printf("NO\n"); } int main() { while(scanf("%d%d%d",&a,&b,&c)>0&&(a+b+c)) { if(a%2==1) { printf("NO\n"); continue; } bfs(); } return 0; }
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #include<cstdlib> #include<queue> #include<set> #include<vector> using namespace std; #define INF 0x3f3f3f3f #define eps 1e-10 #define PI acos(-1.0) #define _e exp(1.0) #define ll long long int const maxn = 105; const int mod = 1e9 + 7; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { int a,b,c; while(scanf("%d %d %d",&a,&b,&c)&&(a+b+c)) { a/=gcd(b,c); if(a%2==1) puts("NO"); else cout<<a-1<<endl; } }
标签:lib 地址 bottom 知识 cst 要求 ... 两种 eps
原文地址:https://www.cnblogs.com/smallhester/p/9569201.html