hdu 1430 魔板 (BFS+预处理)
2013-06-28 21:55
429 查看
Problem - 1430
跟八数码相似的一题搜索题。做法可以是双向BFS或者预处理从"12345678"开始可以到达的所有状态,然后等价转换过去直接回溯路径即可。
代码如下:
View Code
——written by Lyon
跟八数码相似的一题搜索题。做法可以是双向BFS或者预处理从"12345678"开始可以到达的所有状态,然后等价转换过去直接回溯路径即可。
代码如下:
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <map> #include <stack> #include <string> using namespace std; char q[44444][10], op[44444], tmp[10]; int qh, qt, last[44444]; map<string, int> pos; void op1(char *s) { reverse(s, s + 8);} void op2(char *s, bool op) { if (op) { rotate(s, s + 3, s + 4); rotate(s + 4, s + 5, s + 8); } else { rotate(s, s + 1, s + 4); rotate(s + 4, s + 7, s + 8); } } void op3(char *s, bool op) { char t; if (op) { t = s[1]; s[1] = s[6]; s[6] = s[5]; s[5] = s[2]; s[2] = t; } else { t = s[1]; s[1] = s[2]; s[2] = s[5]; s[5] = s[6]; s[6] = t; } } void PRE() { for (int i = 0; i < 8; i++) tmp[i] = i + '0'; tmp[8] = 0; pos.clear(); qh = qt = 0; strcpy(q[qt], tmp); pos[tmp] = qt; last[qt] = -1; op[qt++] = 0; while (qh < qt) { strcpy(tmp, q[qh]); op1(tmp); if (pos.find(tmp) == pos.end()) { strcpy(q[qt], tmp); pos[tmp] = qt; last[qt] = qh; op[qt++] = 'A'; } strcpy(tmp, q[qh]); op2(tmp, true); if (pos.find(tmp) == pos.end()) { strcpy(q[qt], tmp); pos[tmp] = qt; last[qt] = qh; op[qt++] = 'B'; } strcpy(tmp, q[qh]); op3(tmp, true); if (pos.find(tmp) == pos.end()) { strcpy(q[qt], tmp); pos[tmp] = qt; last[qt] = qh; op[qt++] = 'C'; } qh++; } } int main() { // freopen("in", "r", stdin); PRE(); char bg[10], ed[10], con[10]; while (cin >> bg >> ed) { for (int i = 0; i < 8; i++) { int t = 0; while (bg[i] != ed[t]) t++; con[t] = i + '0'; } con[8] = 0; int cur = pos[con]; stack<char> path; while (!path.empty()) path.pop(); while (cur) { path.push(op[cur]); cur = last[cur]; } while (!path.empty()) { putchar(path.top()); path.pop(); } puts(""); } return 0; }
View Code
——written by Lyon
相关文章推荐
- hdu 1430 魔板(bfs+预处理+康托展开)
- HDU - 1430 魔板 (bfs预处理 + 康托)
- ACM-康托展开+预处理BFS之魔板——hdu1430
- HDU_1430——魔板,预处理,康托展开,置换,string类的+操作
- Hdoj 1430 魔板 【BFS】+【康拓展开】+【预处理】
- HDU-1430 魔板
- HDU-1430 魔板
- hdu 1430 魔板
- hdu 1430+hdu 3567(预处理)
- hdu 1430 魔板 康托展开 + 很好的映射
- HDU 1430 魔板 搜索
- HDU 1430 魔板
- HDU 1430 魔板
- HDU 1430 魔板 (BFS)
- [HDU 1430] 魔板
- HDU 1430 魔板 康托展开或字典树 + BFS
- hdu 1430 魔板
- hdu 1430+hdu 3567(预处理)
- HDU 1430 魔板
- HDU - 3533----Escape(BFS+预处理)