当前位置: 代码迷 >> 综合 >> HDU 1495 - 非常可乐
  详细解决方案

HDU 1495 - 非常可乐

热度:31   发布时间:2023-12-24 11:33:43.0

题目大意:中文题。。。三个整数 : S 可乐的体积 , N 和 M是两个杯子的容量,以"0 0 0"结束。如果能平分的话请输出最少要倒的次数,否则输出"NO"。

解题思路:用vis三维表示三个瓶子中的状态,六种倒水方式,A->B,A->C,B->C,C->B,C->A,B->A。bfs。标记数组也是步数数组,如果刚开始大杯为奇数,则直接NO。

ac代码:

#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
struct coke{int A;int B;int C;
};
coke co;
int a, b, c, m, temp;
int s, n, vis[105][105][105];
queue <coke>qu;
void judge()
{if (vis[co.A][co.B][co.C] == -1) qu.push(co), vis[co.A][co.B][co.C] = vis[a][b][c] + 1;
}
int bfs()
{memset(vis, -1, sizeof(vis));while (!qu.empty())qu.pop();int count=0;co.A = s, co.B = 0, co.C = 0;vis[s][0][0] = 0;qu.push(co);while (!qu.empty()){coke temp = qu.front();qu.pop(); a = temp.A, b = temp.B, c = temp.C;if (a == s/2 && b == s/2 || c == s/2 && a == s/2)return vis[a][b][c];if (a > 0 && b < n){if (a > (n-b))co.A = a - (n - b), co.B = n;elseco.A = 0, co.B = b + a;co.C = c;judge();		}if (a > 0 && c < m){if (a > (m-c))co.A = a - (m - c), co.C = m; elseco.A = 0, co.C = c + a;co.B = b;judge();}if (b > 0 && c < m){if (b > (m-c))co.B = b - (m - c), co.C = m;elseco.B = 0, co.C = c + b;co.A = a;judge();}if (b > 0 && a < s){if (b > (s-a))co.B = b - (s - a), co.A = s;elseco.B = 0, co.A = a + b;co.C = c; judge();}if (c > 0 && b < n){if (c > (n-b))co.C = c - (n - b), co.B = n;elseco.C = 0, co.B = b + c;co.A = a;judge();}if (c > 0 && a < s){if (c > (s-a))co.C = c - (s - a), co.A = s;elseco.C = 0, co.A = a + c;co.B = b;judge();}}
return 0;	
}
int main()
{while (scanf("%d%d%d", &s, &n, &m)!=EOF){if (!s && !n && !m)break;if (s % 2)temp = 0;elsetemp = bfs();if (temp)printf("%d\n", temp);elseprintf("NO\n"); }return 0;
}