标签:模拟 input ret case cout efi clu 运动 end
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 <iostream> #include <cstdio> #include <string> #include <cstring> #include <cmath> #include <queue> using namespace std; typedef long long LL; #define Mem0(x) memset(x, 0, sizeof(x)) #define MemI(x) memset(x, -1, sizeof(x)) #define MemM(x) memset(x, 0x3f, sizeof(x)) const int MAXN = 10005; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; //vis -> all of case of n and m int vis[150][150], s, n, m; struct Node { int s, n, m, cnt; }; queue<Node> q; int bfs() { while(!q.empty()) q.pop(); Mem0(vis); Node now, next; now.s = s, now.n = 0, now.m = 0, now.cnt = 0; q.push(now); vis[now.n][now.m] = 1; while(!q.empty()) { now = q.front(); q.pop(); // cout << now.s << " " << now.n << " " << now.m << " " << now.cnt << endl; int cnt = 0; if(2 * now.s == s) cnt++; if(2 * now.n == s) cnt++; if(2 * now.m == s) cnt++; if(cnt == 2) return now.cnt; // s -> n next.cnt = now.cnt + 1; if(now.s && now.n != n) { int d = n - now.n; next.s = max(0, now.s - d); next.n = min(n, now.n + now.s); next.m = now.m; if(!vis[next.n][next.m]) { vis[next.n][next.m] = 1; q.push(next); } } // s -> m if(now.s && now.m != m) { int d = m - now.m; next.s = max(0, now.s - d); next.m = min(m, now.m + now.s); next.n = now.n; if(!vis[next.n][next.m]) { vis[next.n][next.m] = 1; q.push(next); } } // n -> s if(now.n && now.s != s) { int d = s - now.s; next.n = max(0, now.n - d); next.s = min(s, now.s + now.n); next.m = now.m; if(!vis[next.n][next.m]) { vis[next.n][next.m] = 1; q.push(next); } } // n -> m if(now.n && now.m != m) { int d = m - now.m; next.n = max(0, now.n - d); next.m = min(m, now.m + now.n); next.s = now.s; if(!vis[next.n][next.m]) { vis[next.n][next.m] = 1; q.push(next); } } //m -> s if(now.m && now.s != s) { int d = s - now.s; next.m = max(0, now.m - d); next.s = min(s, now.s + now.m); next.n = now.n; if(!vis[next.n][next.m]) { vis[next.n][next.m] = 1; q.push(next); } } // m -> n if(now.m && now.n != n) { int d = n - now.n; next.m = max(0, now.m - d); next.n = min(n, now.n + now.m); next.s = now.s; if(!vis[next.n][next.m]) { vis[next.n][next.m] = 1; q.push(next); } } } return 0; } int main() { while(cin >> s >> n >> m) { if(!s && !n && !m) break; if(s % 2) { cout << "NO" << endl; continue; } else { int ans = bfs(); if(ans) cout << ans << endl; else cout << "NO" << endl; } } return 0; }
标签:模拟 input ret case cout efi clu 运动 end
原文地址:https://www.cnblogs.com/shuizhidao/p/9692467.html