zoukankan      html  css  js  c++  java
  • [cdoj1380] Xiper的奇妙历险(3) (八数码问题 bfs + 预处理)

    快要NOIP 2016 了,现在已经停课集训了。计划用10天来复习以前学习过的所有内容。首先就是搜索。

    八数码是一道很经典的搜索题,普通的bfs就可求出。为了优化效率,我曾经用过康托展开来优化空间,甚至还用过A*来优化时间。不过这道题懒得写了,就一个普普通通的bfs,再加上一个stl 的map就水过了。

    首先题目要求有多达10000组数据,依次搜索肯定是不行的,我试过用A*来写,第2组数据就会T掉,所以我们考虑用一个预处理。从末尾状态搜索所有可行的状态,并用一个map来存储答案。然后就很好写了。

    不过最智障的是,我用getchar读入数据,手测数据都对,交上去wa了无数次,改了快一晚上才发现是读入挂了。辣鸡getchar

     1 #include <iostream>
     2 #include <map>
     3 #include <queue>
     4 #include <cstdio> 
     5 using namespace std;
     6 typedef pair <int ,int> pis;
     7 #define OK(a,b) (a < 3 && a >= 0 && b < 3 && b >= 0)
     8 
     9 int t;
    10 map <long long, int > MAP;
    11 queue <pis > q;
    12 
    13 int mx[] = {1,-1,0,0};
    14 int my[] = {0,0,-1,1};
    15 
    16 inline int E(int x){return x==0 ? 1 : 10 * E(x-1);}
    17 
    18 inline void bfs(){
    19     int u = 123456780;
    20     int p;
    21     MAP[u] = 1;
    22     q.push(make_pair(u,8));
    23     while(!q.empty()){
    24         u = q.front().first; p = q.front().second;q.pop();
    25         int x = p / 3, y = p % 3;
    26         for(int i = 0;i < 4;i++){
    27             int nx = x + mx[i];
    28             int ny = y + my[i];
    29             if(OK(nx,ny)){
    30                 int nu = u;
    31                 int xx = (nu % E(9-(nx * 3 + ny))) / E(8-(nx * 3 + ny));
    32                 nu -= xx * E(8-(nx * 3 + ny));
    33                 nu += xx * E(8-p);
    34                 if(!MAP[nu]){
    35                     MAP[nu] = MAP[u] + 1;
    36                     q.push(make_pair(nu,nx*3+ny));
    37                 }
    38             }
    39         }
    40     }
    41     
    42 }
    43 
    44 int main(){
    45     bfs();
    46     
    47     scanf("%d",&t);
    48     char s[9];
    49     while(t--){
    50         int m = 0;
    51         for(int i = 0;i < 9;i++){
    52             scanf("%s",s);
    53             m = m * 10 + (s[0] == 'x' ? 0 : (s[0] - '0'));
    54         }
    55         printf("%d
    ",MAP[m] - 1);
    56     }
    57     
    58     return 0; 
    59 }
    View Code
  • 相关阅读:
    java基础部分的一些有意思的东西。
    antdvue按需加载插件babelpluginimport报错
    阿超的烦恼 javaScript篇
    .NET E F(Entity Framework)框架 DataBase First 和 Code First 简单用法。
    JQuery获得input ID相同但是type不同的方法
    gridview的删除,修改,数据绑定处理
    jgGrid数据格式
    Cannot read configuration file due to insufficient permissions
    Invoke action which type of result is JsonResult on controller from view using Ajax or geJSon
    Entity model数据库连接
  • 原文地址:https://www.cnblogs.com/ZegWe/p/5940397.html
Copyright © 2011-2022 走看看