标签:ios algo bfs for pat node com 包含 ring
题目:
输入:
有且只有一行,包含3个数A,B,C(1<=A,B<=100,C<=max(A,B))
输出:
样例:
分析:简单的BFS,难点在于回溯,给每个状态用数组记录路径
#include<iostream> #include<sstream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<algorithm> #include<functional> #include<iomanip> #include<numeric> #include<cmath> #include<queue> #include<vector> #include<set> #include<cctype> #define PI acos(-1.0) const int INF = 0x3f3f3f3f; const int NINF = -INF - 1; typedef long long ll; using namespace std; int a, b, c; int used[105][105]; struct node { int x, y; int flag; int path[1005];//数组中0-5分别表示6种不同操作 }st; string print[6] = {"FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)", "POUR(2,1)"}; void bfs() { queue<node> q; for (int i = 0; i <= a; ++i) { for (int j = 0; j <= b; ++j) used[i][j] = INF; } memset(used, 0, sizeof(used)); st.x = 0, st.y = 0; st.flag = 0; memset(st.path, -1, sizeof(st.path)); q.push(st); used[st.x][st.y] = 1; while (q.size()) { node temp = q.front(); q.pop(); if (temp.x == c || temp.y == c) { cout << temp.flag << endl; for (int i = 0; i < temp.flag; ++i) cout << print[temp.path[i]] << endl; return; } for (int i = 0; i < 6; ++i) { node now = temp; now.flag++; if (i == 0 && now.x != a) { now.x = a; if (!used[now.x][now.y]) { used[now.x][now.y] = 1; now.path[temp.flag] = 0; q.push(now); } } else if (i == 1 && now.y != b) { now.y = b; if (!used[now.x][now.y]) { used[now.x][now.y] = 1; now.path[temp.flag] = 1; q.push(now); } } else if (i == 2 && now.x != 0) { now.x = 0; if (!used[now.x][now.y]) { used[now.x][now.y] = 1; now.path[temp.flag] = 2; q.push(now); } } else if (i == 3 && now.y != 0) { now.y = 0; if (!used[now.x][now.y]) { used[now.x][now.y] = 1; now.path[temp.flag] = 3; q.push(now); } } else if (i == 4) { if (now.x + now.y > b) { now.x -= b - now.y; now.y = b; } else { now.y += now.x; now.x = 0; } if (!used[now.x][now.y]) { used[now.x][now.y] = 1; now.path[temp.flag] = 4; q.push(now); } } else if (i == 5) { if (now.x + now.y > a) { now.y -= a - now.x; now.x = a; } else { now.x += now.y; now.y = 0; } if (!used[now.x][now.y]) { used[now.x][now.y] = 1; now.path[temp.flag] = 5; q.push(now); } } } } cout << "impossible" << endl; } int main() { cin >> a >> b >> c; bfs(); return 0; }
标签:ios algo bfs for pat node com 包含 ring
原文地址:https://www.cnblogs.com/veasky/p/10972694.html