http://acm.hdu.edu.cn/showproblem.php?pid=1495
大家一定覺的運動以後喝可樂是一件很愜意的事情,但是seeyou卻不這麼認爲。因爲每次當seeyou買了可樂以後,阿牛就要求和seeyou一起分享這一瓶可樂,而且一定要喝的和seeyou一樣多。但seeyou的手中只有兩個杯子,它們的容量分別是N 毫升和M 毫升 可樂的體積爲S (S<101)毫升 (正好裝滿一瓶) ,它們三個之間可以相互倒可樂 (都是沒有刻度的,且 S==N+M,101>S>0,N>0,M>0) 。聰明的ACMER你們說他們能平分嗎?如果能請輸出倒可樂的最少的次數,如果不能輸出"NO"。
Input
三個整數 : S 可樂的體積 , N 和 M是兩個杯子的容量,以"0 0 0"結束。
Output
如果能平分的話請輸出最少要倒的次數,否則輸出"NO"。
Sample Input
7 4 3 4 1 3 0 0 0
Sample Output
NO 3
也就是給出三個杯子,最後的狀態是其中兩個杯子中水一樣多,且爲總量的一半,求最少倒水次數。
首先奇數肯定不能平分,因爲沒有刻度
模擬倒水n-m,n-s;m-n,m-s;s-n,s-m;分滿與不滿兩種情況倒水
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
int ts, tn, tm;
int vis[101][101][101];
struct node {
int s, n, m, step;
};
void bfs(int ts, int tn, int tm){
memset(vis, 0 ,sizeof(vis));
queue<node>q;
node st, ed;
st.s = ts;
st.n = 0;
st.m = 0;
st.step = 0;//初始的情況
q.push(st);
while(!q.empty()){
st = q.front();
q.pop();//一般寫法
vis[st.s][st.n][st.m] = 1;//標記數組
if((st.s == ts/2 && st.n == ts/2)||(st.s == ts/2 && st.m == ts/2)||(st.n == ts/2 && st.m == ts/2)){
printf("%d\n", st.step);//終止條件
return;
}
// s -> n || s -> m
if(st.s){//此時s沒有滿
// s -> n
// 可以倒滿
if(st.s > tn - st.n){
ed.s = st.s - (tn - st.n);//剩下的s
ed.n = tn;//相當於把n那個杯子倒滿了;
ed.m = st.m;
ed.step = st.step + 1;
}
// 不能倒滿
else{
ed.s = 0;//清空了s瓶中的
ed.n = st.n + st.s;
ed.m = st.m;
ed.step = st.step + 1;
}
if(!vis[ed.s][ed.n][ed.m]){
vis[ed.s][ed.n][ed.m] = 1;
q.push(ed);//狀態合法
}
//s - >m
// 可以倒滿
if(st.s > tm - st.m){
ed.s = st.s - (tm - st.m);
ed.m = tm;
ed.n = st.n;
ed.step = st.step + 1;
}
// 不能倒滿
else{
ed.s = 0;
ed.m = st.m + st.s;
ed.n = st.n;
ed.step = st.step + 1;
}
if(!vis[ed.s][ed.n][ed.m]){
vis[ed.s][ed.n][ed.m] = 1;
q.push(ed);
}
}
// n -> s || n -> m
if(st.n){
// n -> s
// 可以倒滿
if(st.n > ts - st.s){
ed.n = st.n - (ts - st.s);
ed.s = ts;
ed.m = st.m;
ed.step = st.step + 1;
}
// 不能倒滿
else{
ed.n = 0;
ed.s = st.s + st.n;
ed.m = st.m;
ed.step = st.step + 1;
}
if(!vis[ed.s][ed.n][ed.m]){
vis[ed.s][ed.n][ed.m] = 1;
q.push(ed);
}
//n - >m
// 可以倒滿
if(st.n > tm - st.m){
ed.n = st.n - (tm - st.m);
ed.m = tm;
ed.s = st.s;
ed.step = st.step + 1;
}
// 不能倒滿
else{
ed.n = 0;
ed.m = st.n + st.m;
ed.s = st.s;
ed.step = st.step + 1;
}
if(!vis[ed.s][ed.n][ed.m]){
vis[ed.s][ed.n][ed.m] = 1;
q.push(ed);
}
}
// m -> s || m -> n
if(st.m){
//m -> s
// 可以倒滿
if(st.m > ts - st.s){
ed.m = st.m - (ts - st.s);
ed.s = ts;
ed.n = st.n;
ed.step = st.step + 1;
}
// 不能倒滿
else{
ed.m = 0;
ed.s = st.s + st.m;
ed.n = st.n;
ed.step = st.step + 1;
}
if(!vis[ed.s][ed.n][ed.m]){
vis[ed.s][ed.n][ed.m] = 1;
q.push(ed);
}
//m -> n
// 可以倒滿
if(st.m > tn - st.n){
ed.m = st.m - (tn - st.n);
ed.n = tn;
ed.s = st.s;
ed.step = st.step + 1;
}
// 不能倒滿
else{
ed.m = 0;
ed.n = st.n + st.m;
ed.s = st.s;
ed.step = st.step + 1;
}
if(!vis[ed.s][ed.n][ed.m]){
vis[ed.s][ed.n][ed.m] = 1;
q.push(ed);
}
}
}
printf("NO\n");
return ;
}
int main()
{
while(scanf("%d%d%d", &ts, &tn, &tm), ts || tn || tm){
if(ts & 1)
printf("NO\n");//奇數的話是不行的
else
bfs(ts, tn, tm);
}
return 0;
}
數論也可以,且十分簡便,但還沒有理解
#include<bits/stdc++.h>
using namespace std;
int main(){
int s,n,m;
while(cin>>s>>n>>m,s+n+m){
s/=__gcd(n,m);
if(s&1)//奇數
cout<<"NO\n";
else cout<<s-1<<endl;
}
return 0;
}