#include #include #include #include using namespace std; const int N = 110; struct node { int x, y, step; string str; }; int n, m, k; bool vis[N][N]; void bfs() { queue q; node temp; temp.x = 0; temp.y = 0; temp.step = 0; vis[0][0] = true; q.push(temp); while (!q.empty()) { node t = q.front(); q.pop(); if (t.x == k || t.y == k) { cout << t.step << endl; int len = t.str.length(); for (int i = 0; i < len; i++) { cout << t.str[i] << endl; } return; } if (!vis[n][t.y]) { node temp; temp.x = n; temp.y = t.y; temp.step = t.step + 1; temp.str = t.str + "FILL(1)"; vis[n][t.y] = true; q.push(temp); } if (!vis[t.x][m]) { node temp; temp.x = t.x; temp.y = m; temp.step = t.step + 1; temp.str = t.str + "FILL(2)"; vis[t.x][m] = true; q.push(temp); } if (!vis[0][t.y]) { node temp; temp.x = 0; temp.y = t.y; temp.step = t.step + 1; temp.str = t.str + "DROP(1)"; vis[0][t.y] = true; q.push(temp); } if (!vis[t.x][0]) { node temp; temp.x = t.x; temp.y = 0; temp.step = t.step + 1; temp.str = t.str + "DROP(2)"; vis[t.x][0] = true; q.push(temp); } if (!vis[max(t.x - (m - t.y), 0)][min(m, t.x + t.y)]) { node temp; temp.x = max(t.x - (m - t.y), 0); temp.y = min(m, t.x + t.y); temp.step = t.step + 1; temp.str = t.str + "POUR(1,2)"; vis[temp.x][temp.y] = true; q.push(temp); } if (!vis[min(n, t.x + t.y)][max(0, t.y - (n - t.x))]) { node temp; temp.x = min(n, t.x + t.y); temp.y = max(0, t.y - (n - t.x)); temp.step = t.step + 1; temp.str = t.str + "POUR(2,1)"; vis[temp.x][temp.y] = true; q.push(temp); } } cout << "impossible" << endl; } int main() { cin >> n >> m >> k; bfs(); return 0; }


原文地址: https://www.cveoy.top/t/topic/lBXl 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录