非常可樂(bfs)

2021-10-02 22:20:27 字數 3924 閱讀 8710

problem description

大家一定覺的運動以後喝可樂是一件很愜意的事情,但是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

#include #include using namespace std;

int s; // 一罐可樂

int a, b; // 2個杯子

int half;

typedef struct now now;

queueq;

bool mark[110][110][110];

int atob(int& a, int& amax, int& b, int& bmax)

if (a == 0)

// 倒了, 有改動

// b缺口非常大,a全到進去了

if (a <= bmax - b) else

return 1;

}void printnow(now& a)

bool judge(now& n) else

}/*操作:或將目標容器倒滿,或將源容器倒空。*/

int bfs(int s, int a, int b)

now start;

start.s = s;

start.a = 0;

start.b = 0;

start.step = 0;

mark[s][0][0] = 1;

now father;

now child;

q.push(start);

while(!q.empty())

int ns;

int na;

int nb;

int nstep;

//s to a

ns = father.s;

na = father.a;

nb = father.b;

nstep = father.step;

if (atob(ns, s, na, a) == 1)

}//s to b

ns = father.s;

na = father.a;

nb = father.b;

if (atob(ns, s, nb, b) == 1)

}//a to s

ns = father.s;

na = father.a;

nb = father.b;

if (atob(na, a, ns, s) == 1)

}//a to b

ns = father.s;

na = father.a;

nb = father.b;

if (atob(na, a, nb, b) == 1)

}//b to s

ns = father.s;

na = father.a;

nb = father.b;

if (atob(nb, b, ns, s) == 1)

}//b to a

ns = father.s;

na = father.a;

nb = father.b;

if (atob(nb, b, na, a) == 1) }}

return -1;

}int main()

}if (s % 2 == 1)

int res = bfs(s, a, b);

if (res == -1) else

}}

注意:千萬不要 a == b == c   //1 == c

需要 a == b && b == c

#include #include #include using namespace std;

int smax; //可樂的體積

int amax,bmax; //兩個杯子

int half;

typedef struct now now;

bool mark[101][101][101];

queueq;

// ok

void s2a(now& n) else

}// ok

void s2b(now& n) else

}// ok

void a2s(now& n) else

}// ok

void a2b(now& n) else

}// ok

void b2s(now& n) else

}// ok

void b2a(now& n) else

}bool check(now& n) else if (n.s == half && n.b == half) else if (n.a == half && n.b == half)

return 0;

}int bfs()

now chu;

chu.s = smax;

chu.a = chu.b =0;

chu.cnt = 0;

mark[smax][0][0] = 1;

now father;

now child;

q.push(chu);

while(!q.empty())

//s to a

child = father;

s2a(child);

if (mark[child.s][child.a][child.b] == 0)

//s to b

child = father;

s2b(child);

if (mark[child.s][child.a][child.b] == 0)

//a to s

child = father;

a2s(child);

if (mark[child.s][child.a][child.b] == 0)

//a to b

child = father;

a2b(child);

if (mark[child.s][child.a][child.b] == 0)

//b to s

child = father;

b2s(child);

if (mark[child.s][child.a][child.b] == 0)

//b to a

child = father;

b2a(child);

if (mark[child.s][child.a][child.b] == 0)

} return -1;

}int main()

for (int i = 0; i <= 100; i++)

}} half = smax / 2;

if (smax % 2 == 1) else else

} }}

非常可樂 bfs

大家一定覺的運動以後喝可樂是一件很愜意的事情,但是seeyou卻不這麼認為。因為每次當seeyou買了可樂以後,阿牛就要求和seeyou一起分享這一瓶可樂,而且一定要喝的和seeyou一樣多。但seeyou的手中只有兩個杯子,它們的容量分別是n 毫公升和m 毫公升 可樂的體積為s s 101 毫公升...

非常可樂 BFS

大家一定覺的運動以後喝可樂是一件很愜意的事情,但是seeyou卻不這麼認為。因為每次當seeyou買了可樂以後,阿牛就要求和seeyou一起分享這一瓶可樂,而且一定要喝的和seeyou一樣多。但seeyou的手中只有兩個杯子,它們的容量分別是n 毫公升和m 毫公升 可樂的體積為s s 101 毫公升...

非常可樂(bfs

problem 非常可樂 description 大家一定覺的運動以後喝可樂是一件很愜意的事情,但是seeyou卻不這麼認為。因為每次當seeyou買了可樂以後,阿牛就要求和seeyou一起分享這一瓶可樂,而且一定要喝的和seeyou一樣多。但seeyou的手中只有兩個杯子,它們的容量分別是n 毫公...