不带任何优化且使用了巨慢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\n",dist[ans]);
119         print_ans(ans);
120     }
121     else printf("-1\n");
122     return 0;
123 }

 

转载于:https://www.cnblogs.com/Asurudo/p/10055341.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值