行业知识
蓝桥杯C++大学B组一个月冲刺记录2024/3/18
2026-11-30 01:24:43
来源:互联网转载
蓝桥杯C++大学B组一个月冲刺记录2024/3/18
规则:每日三题
()昨天因为前妻姐emo上了,静下来思考了点东西,就没做题啦.今日补上!
另外:博客浏览量破万了,写的东西有人看还是很开心的
()1.母亲的牛奶
农夫约翰有三个容量分别为 A,B,C升的挤奶桶。
最开始桶 A和桶 B都是空的,而桶 C里装满了牛奶。
有时,约翰会将牛奶从一个桶倒到另一个桶中,直到被倒入牛奶的桶满了或者倒出牛奶的桶空了为止。
这一过程中间不能有任何停顿,并且不会有任何牛奶的浪费。
请你编写一个程序判断,当 A桶是空的时候,C桶中可能包含多少升牛奶,找出所有的可能情况。
bfs + 状态转移
主要思想:
(1)是如何记录被遍历的状态,任何字符串(或是多数据)的状态,都可以通过各元素元素编码得到一个longlong值.然后通过哈希进行记录
(2)状态转移:穷举即可
#include#include#include#include#includeusing namespace std;const int N = 25;int A,B,C;struct state{ int a,b,c;};bool st[N * N * N];vectorans;int query_k(int a, int b, int c){ return a * 21 * 21 + b * 21 + c;}void bfs(int a,int b,int c){ int k = query_k(a,b,c); st[k] = true; queueq; q.push({a,b,c}); while(q.size() != 0){ auto t = q.front(); q.pop(); if(t.a == 0) ans.push_back(t.c); if(t.a != 0){ state New; New.a = t.a - min(t.a,B - t.b); New.b = t.b + min(t.a,B - t.b); New.c = t.c; k = query_k(New.a,New.b,New.c); if(!st[k]) q.push(New),st[k] = true; New.a = t.a - min(t.a,C - t.c); New.b = t.b; New.c = t.c + min(t.a,C - t.c);; k = query_k(New.a,New.b,New.c); if(!st[k]) q.push(New),st[k] = true; } if(t.b != 0){ state New; New.a = t.a + min(t.b,A - t.a); New.b = t.b - min(t.b,A - t.a); New.c = t.c; k = query_k(New.a,New.b,New.c); if(!st[k]) q.push(New),st[k] = true; New.a = t.a; New.b = t.b - min(t.b,C - t.c); New.c = t.c + min(t.b,C - t.c);; k = query_k(t.a,t.b,t.c); if(!st[k]) q.push(New),st[k] = true; } if(t.c != 0){ state New; New.a = t.a + min(t.c,A - t.a); New.b = t.b; New.c = t.c - min(t.c,A - t.a); k = query_k(New.a,New.b,New.c); if(!st[k]) q.push(New),st[k] = true; New.a = t.a; New.b = t.b + min(t.c,B - t.b); New.c = t.c - min(t.c,B - t.b);; k = query_k(New.a,New.b,New.c); if(!st[k]) q.push(New),st[k] = true; } } return; }int main(){ cin >> A >> B >> C; bfs(0,0,C); sort(ans.begin(),ans.end()); for(int i = 0;i > n; for(int i = 1;i for(int j = 1;j cin p[i][j]; } } bfs(1,1); cout 0,1,0,-1},dy[4] = {1,0,-1,0};LL query_k(string s){ LL t = 0; for(int i = 0;i = 0 && yy c; if(c == 'x') first += '9'; else first += c; } cout 1, 0, -1, 0};int dy[4] = {0, 1, 0, -1};int n,cnt;void bfs(int i,int j){ st[i][j] = true; cnt ++; queuei,j}); while(q.size() != 0){ auto t = q.front(); q.pop(); bool flag = false; for(int k = 0;k > n; for(int i = 0;i > p[i]; for(int i = 0;i