zoukankan      html  css  js  c++  java
  • 不带任何优化且使用了巨慢STL容器set来查重的随便在哪个OJ上提交都会TLE的八数码(基本是从刘汝佳抄的)

    虽说这份代码的无能已经从题目看出来了,但是,他能打印步骤,他能打印步骤,他能打印步骤,重要的事情说三遍!(然并卵)

      1 #include <iostream>
      2 #include <set>
      3 #include <cstring>
      4 #include <vector>
      5 #pragma GCC optimize(2)
      6 #define _for(i,a,b) for(int i = (a);i < (b);i ++)
      7 using namespace std;
      8 typedef int State[9];
      9 const int maxstate = 1000000;
     10 State st[maxstate],goal;
     11 int dist[maxstate] {0};
     12 int fa[maxstate];
     13 
     14 const int dx[] = {1,-1,0,0};
     15 const int dy[] = {0,0,1,-1};
     16 
     17 set<int> vis;
     18 void init_lookup_table()
     19 {
     20     vis.clear();
     21 }
     22 int try_to_insert(int s)
     23 {
     24     int v = 0;
     25     _for(i,0,9) v = v*10+st[s][i];
     26     if(vis.count(v)) return 0;
     27     vis.insert(v);
     28     return 1;
     29 }
     30 
     31 bool limit(int newx,int newy)
     32 {
     33     return newx>=0&&newx<3 && newy>=0&&newy<3;
     34 }
     35 
     36 int bfs()
     37 {
     38     init_lookup_table();
     39     int front = 1,rear = 2;
     40     while(front<rear)
     41     {
     42         State &s = st[front];
     43         if(memcmp(goal,s,sizeof(s))==0)    return front;
     44         int z;
     45         for(z = 0; z < 9; z ++)
     46             if(!s[z]) break;//find zero
     47         int x = z/3,y = z%3;
     48         _for(d,0,4)
     49         {
     50             int newx = x+dx[d];
     51             int newy = y+dy[d];
     52             int newz = newx*3+newy;
     53             if(limit(newx,newy))
     54             {
     55                 State &t = st[rear];//new node
     56                 memcpy(&t,&s,sizeof(s));
     57                 t[newz] = s[z];//0 move
     58                 t[z] = s[newz];//move to where ori is 0
     59                 dist[rear] = dist[front]+1;
     60                 if(try_to_insert(rear))
     61                 {
     62                     fa[rear] = front;
     63                     rear ++;
     64                 }
     65             }
     66         }
     67         front ++;
     68     }
     69     return 0;
     70 }
     71 
     72 void print_ans(int u)
     73 {
     74     vector<vector<int>> nodes;
     75     vector<int> tmp;
     76     _for(j,0,9)
     77     {
     78         tmp.push_back(st[u][j]);
     79     }
     80     nodes.push_back(tmp);
     81     while(1)
     82     {
     83         tmp.clear();
     84         if(fa[u]==0) break;
     85         _for(j,0,9)
     86         {
     87             tmp.push_back(st[fa[u]][j]);
     88         }
     89         nodes.push_back(tmp);
     90         u = fa[u];
     91     }
     92     int cnt = 0;
     93     int kase = 0;
     94     for(int i = nodes.size()-1; i >= 0; i --)
     95     {
     96         if(!kase)
     97         {
     98             cout << "Ori:" << endl;
     99             kase ++;
    100         }
    101         else
    102             cout << "Step:" << kase++ << endl;
    103         cout << nodes[i][0] << " " << nodes[i][1] << " " << nodes[i][2] << endl;
    104         cout << nodes[i][3] << " " << nodes[i][4] << " " << nodes[i][5] << endl;
    105         cout << nodes[i][6] << " " << nodes[i][7] << " " << nodes[i][8] << endl;
    106         cout << endl << endl;
    107     }
    108 }
    109 
    110 int main()
    111 {
    112     //freopen("output.txt", "w", stdout);
    113     _for(i,0,9)    scanf("%d",&st[1][i]);
    114     _for(i,0,9) scanf("%d",&goal[i]);
    115     int ans = bfs();
    116     if(ans>0)
    117     {
    118         printf("Step Needed:%d times
    ",dist[ans]);
    119         print_ans(ans);
    120     }
    121     else printf("-1
    ");
    122     return 0;
    123 }
  • 相关阅读:
    java实现第五届蓝桥杯出栈次序
    java实现第五届蓝桥杯年龄巧合
    java实现第五届蓝桥杯年龄巧合
    java实现第五届蓝桥杯年龄巧合
    java实现第五届蓝桥杯年龄巧合
    java实现第五届蓝桥杯年龄巧合
    Java使用RandomAccessFile读写文件
    Java 实现文件随机读写-RandomAccessFile
    NIO 中文乱码自我解决的简单DEMO
    NIO 中文乱码问题的解决代码实现
  • 原文地址:https://www.cnblogs.com/Asurudo/p/10055341.html
Copyright © 2011-2022 走看看