题解
2025-11-22 16:01:18
发布于:广东
2阅读
0回复
0点赞
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int N = 105;
struct Node{
int x, y, t;
string step;
};
int n, m, k;
bool vis[N] [N];
queue<Node> q;
int main() {
cin >> n >> m >> k;
q.push({ 0, 0, 0, "" });
vis[0][0] = true;
while( !q.empty() ) {
Node now = q.front(), nt;
if( now.x == k || now.y == k ){
cout << now.t << '\n' << now.step;
return 0;
}
q.pop();
nt.x = n, nt.y = now.y, nt.t = now.t + 1, nt.step = now.step + "FILL(1)\n";
if( !vis[nt.x][nt.y] ) vis[nt.x][nt.y] = true, q.push(nt);
nt.x = now.x, nt.y = m, nt.t = now.t + 1, nt.step = now.step + "FILL(2)\n";
if( !vis[nt.x][nt.y] ) vis[nt.x][nt.y] = true, q.push(nt);
nt.x = 0, nt.y = now.y, nt.t = now.t + 1, nt.step = now.step + "DROP(1)\n";
if( !vis[nt.x][nt.y] ) vis[nt.x][nt.y] = true, q.push(nt);
nt.x = now.x, nt.y = 0, nt.t = now.t + 1, nt.step = now.step + "DROP(2)\n";
if( !vis[nt.x][nt.y] ) vis[nt.x][nt.y] = true, q.push(nt);
nt.x = max( 0, now.x + now.y - m ), nt.y = min( m, now.x + now.y ), nt.t = now.t + 1, nt.step = now.step + "POUR(1,2)\n";
if( !vis[nt.x][nt.y] ) vis[nt.x][nt.y] = true, q.push(nt);
nt.x = min( n, now.x + now.y ), nt.y = max( 0, now.x + now.y - n ), nt.t = now.t + 1, nt.step = now.step + "POUR(2,1)\n";
if( !vis[nt.x][nt.y] ) vis[nt.x][nt.y] = true, q.push(nt);
}
cout << "impossible";
}
这里空空如也




有帮助,赞一个