• hdu 1430 魔板 (BFS+预处理)


    Problem - 1430

      跟八数码相似的一题搜索题。做法可以是双向BFS或者预处理从"12345678"开始可以到达的所有状态,然后等价转换过去直接回溯路径即可。

    代码如下:

      1 #include <cstdio>
      2 #include <iostream>
      3 #include <algorithm>
      4 #include <cstring>
      5 #include <map>
      6 #include <stack>
      7 #include <string>
      8 
      9 using namespace std;
     10 
     11 char q[44444][10], op[44444], tmp[10];
     12 int qh, qt, last[44444];
     13 map<string, int> pos;
     14 
     15 void op1(char *s) { reverse(s, s + 8);}
     16 
     17 void op2(char *s, bool op) {
     18     if (op) {
     19         rotate(s, s + 3, s + 4);
     20         rotate(s + 4, s + 5, s + 8);
     21     } else {
     22         rotate(s, s + 1, s + 4);
     23         rotate(s + 4, s + 7, s + 8);
     24     }
     25 }
     26 
     27 void op3(char *s, bool op) {
     28     char t;
     29     if (op) {
     30         t = s[1];
     31         s[1] = s[6];
     32         s[6] = s[5];
     33         s[5] = s[2];
     34         s[2] = t;
     35     } else {
     36         t = s[1];
     37         s[1] = s[2];
     38         s[2] = s[5];
     39         s[5] = s[6];
     40         s[6] = t;
     41     }
     42 }
     43 
     44 void PRE() {
     45     for (int i = 0; i < 8; i++) tmp[i] = i + '0';
     46     tmp[8] = 0;
     47     pos.clear();
     48     qh = qt = 0;
     49 
     50     strcpy(q[qt], tmp);
     51     pos[tmp] = qt;
     52     last[qt] = -1;
     53     op[qt++] = 0;
     54     while (qh < qt) {
     55         strcpy(tmp, q[qh]);
     56         op1(tmp);
     57         if (pos.find(tmp) == pos.end()) {
     58             strcpy(q[qt], tmp);
     59             pos[tmp] = qt;
     60             last[qt] = qh;
     61             op[qt++] = 'A';
     62         }
     63 
     64         strcpy(tmp, q[qh]);
     65         op2(tmp, true);
     66         if (pos.find(tmp) == pos.end()) {
     67             strcpy(q[qt], tmp);
     68             pos[tmp] = qt;
     69             last[qt] = qh;
     70             op[qt++] = 'B';
     71         }
     72 
     73         strcpy(tmp, q[qh]);
     74         op3(tmp, true);
     75         if (pos.find(tmp) == pos.end()) {
     76             strcpy(q[qt], tmp);
     77             pos[tmp] = qt;
     78             last[qt] = qh;
     79             op[qt++] = 'C';
     80         }
     81 
     82         qh++;
     83     }
     84 }
     85 
     86 int main() {
     87 //    freopen("in", "r", stdin);
     88     PRE();
     89     char bg[10], ed[10], con[10];
     90     while (cin >> bg >> ed) {
     91         for (int i = 0; i < 8; i++) {
     92             int t = 0;
     93             while (bg[i] != ed[t]) t++;
     94             con[t] = i + '0';
     95         }
     96         con[8] = 0;
     97         int cur = pos[con];
     98         stack<char> path;
     99         while (!path.empty()) path.pop();
    100         while (cur) {
    101             path.push(op[cur]);
    102             cur = last[cur];
    103         }
    104         while (!path.empty()) {
    105             putchar(path.top());
    106             path.pop();
    107         }
    108         puts("");
    109     }
    110     return 0;
    111 }
    View Code

    ——written by Lyon

  • 相关阅读:
    python -- twisted初探
    python -- redis连接与使用
    redis使用
    python -- 异步编程
    python
    python
    福大软工 · 最终作业
    福大软工 · 第十二次作业
    Beta 冲刺(7/7)
    Beta 冲刺(6/7)
  • 原文地址:https://www.cnblogs.com/LyonLys/p/hdu_1430_Lyon.html
Copyright © 2020-2023  润新知