题意:有个#字型的棋盘,2行2列,一共24个格。

如图:每个格子是1或2或3,一共8个1,8个2,8个3.

有A~H一共8种合法操作,比如A代表把A这一列向上移动一个,最上面的格会补到最下面。

求:使中心8个格子数字一致的最少步骤,要输出具体的操作步骤及最终中心区域的数字。如果有多个解,输出字典序最小的操作步骤。

分析 : 还是状态空间的搜索,对象就是一个数字序列,判断中心位置是否一样,可以看出如果使用BFS,每一层还是爆炸,所以使用IDA*,关键还是模拟操作和h函数,这里的h函数是这样定义的,可以看出每一次操作,最多给当前局面添加一个符合要求的数字,那就统计一下中心区域最多的相同数字有多少,然后如果8-h > max_depth - cur_depth的话代表最好的情况下都无法解决,剪枝。模拟操作应该就是很白痴的数组转移赋值了,代码很长,很烦,建议看看刘汝佳的代码。

#include<bits/stdc++.h>
using namespace std;
];
int ans_num;
];
bool Is_ok(int *arr)
{
    ];
    ] || temp!=arr[] || temp!=arr[] || temp!=arr[] || temp!=arr[] || temp!=arr[] || temp!=arr[])
        return false;
    return true;
}

inline int h(const int *now)
{
    ];
    memset(cnt, , sizeof(cnt));
    cnt[now[]]++,  cnt[now[]]++,  cnt[now[]]++,
    cnt[now[]]++, cnt[now[]]++, cnt[now[]]++,
    cnt[now[]]++, cnt[now[]]++;
    ], cnt[]);
    ret = max(ret, cnt[]);
    return ret;
}

inline void Change(int *tmp, int one, int two, int three, int four, int five, int six, int seven)
{
    ];
    index[] = one, index[] = two, index[] = three,
    index[] = four, index[] = five, index[] = six;
    index[] = seven;
    int temp = tmp[one];//!
    ; i<; i++)
        tmp[index[i]] = tmp[index[i+]];
    tmp[index[]] = temp;
}

bool DFS(int *now, int cur_depth, int max_depth, int per_dir)
{
     - h(now) > max_depth - cur_depth) return false;
    if(cur_depth >= max_depth) return false;//!?
    ; dir<=; dir++){//!
        ];
        ){
            &&dir==) || (dir==&&per_dir==)) continue;
            &&dir==) || (dir==&&per_dir==)) continue;
            &&dir==) || (dir==&&per_dir==)) continue;
            &&dir==) || (dir==&&per_dir==)) continue;
        }
        ; i<; i++) tmp[i] = now[i];
        int top = cur_depth;
        switch(dir){
            : ans[top]=,,,,,,);break;
            : ans[top]=,,,,,,);break;
            : ans[top]=,,,,,,);break;
            : ans[top]=,,,,,,);break;
            : ans[top]=,,,,,,);break;
            : ans[top]=,,,,,,);break;
            : ans[top]=,,,,,,);break;
            : ans[top]=,,,,,,);break;
        }
        if(Is_ok(tmp)){
            ans[top+] = '\0';
            ans_num = tmp[];
            return true;
        }
        , max_depth, dir)) return true;
    }
    return false;
}
int main(void)
{
    ]) && Init[]){
        ; i<=; i++){
            scanf("%d", &Init[i]);
        }
        if(Is_ok(Init)){
            puts("No moves needed");
            printf(]);
            continue;
        }
        ;
        ){
            , max_depth, )) break;
            max_depth++;
        }
        puts(ans);
        printf("%d\n", ans_num);
    }
    ;
}

刘汝佳代码:

// UVa1343 The Rotation Game
// Rujia Liu
// This solutions uses IDA* instead of BFS described in the book, because it's shorter 8-)
// It's shorter because no need for lookup tables and "automatically" lexicographically smallest solution.
#include<cstdio>
#include<algorithm>
using namespace std;

/*
      00    01
      02    03
04 05 06 07 08 09 10
      11    12
13 14 15 16 17 18 19
      20    21
      22    23
*/

// lines E~H are computed with the help of rev[]
][]={
  { , , ,,,,}, // A
  { , , ,,,,}, // B
  {, , , , , , }, // C
  {,,,,,,}, // D
};

] = {, , , , , , , }; // reverse lines of each line

// center squares
] = {, , , , , , , };

];
];

bool is_final() {
  ; i < ; i++)
    ]]) return false;
  return true;
}

int diff(int target) {
  ;
  ; i < ; i++)
    if(a[center[i]] != target) ans++;
  return ans;
}

inline int h() {
  ), diff()), diff());
}

inline void move(int i) {
  ]];
  ; j < ; j++) a[line[i][j]] = a[line[i][j+]];
  a[line[i][]] = tmp;
}

bool dfs(int d, int maxd) {
  if(is_final()) {
    ans[d] = '\0';
    printf("%s\n", ans);
    return true;
  }
  if(d + h() > maxd) return false;
  ; i < ; i++) {
    ans[d] = 'A' + i;
    move(i);
    , maxd)) return true;
    move(rev[i]);
  }
  return false;
}

int main() {
  ; i < ; i++)
    ; j < ; j++) line[i][j] = line[rev[i]][-j];
  ]) ==  && a[]) {
    ; i < ; i++) scanf("%d", &a[i]);
    ; i < ; i++) ;
    if(is_final()) {
      printf("No moves needed\n");
    } else {
      ; ; maxd++)
        , maxd)) break;
    }
    printf(]);
  }
  ;
}

瞎:遇到这种看起来很烦的题目,还是没有那种敏感性去试想状态空间搜索,一来就是想着如何模拟,然后脑袋一团shit,思路根本没有,所以总结应该很重要了,提供了一个思考的方向在那里,真正应该思考的是如何去实现这道题所对应的模型,而不是乱想。

UVa 1343 The Rotation Game (状态空间搜索 && IDA*)的更多相关文章

  1. UVA 1343 - The Rotation Game-[IDA*迭代加深搜索]

    解题思路: 这是紫书上的一道题,一开始笔者按照书上的思路采用状态空间搜索,想了很多办法优化可是仍然超时,时间消耗大的原因是主要是: 1)状态转移代价很大,一次需要向八个方向寻找: 2)哈希表更新频繁: ...

  2. UVA - 1343 The Rotation Game (BFS/IDA*)

    题目链接 紫书例题. 首先附上我第一次bfs+剪枝TLE的版本: #include<bits/stdc++.h> using namespace std; typedef long lon ...

  3. UVA 1343 The Rotation Game

    题意: 给出图,往A-H方向旋转,使中间8个格子数字相同.要求旋转次数最少,操作序列字典序尽量小. 分析: 用一维数组存24个方格.二维数组代表每个方向对应的7个方格.IDA*剪枝是当8-8个方格中重 ...

  4. UVa 11212 Editing a Book (IDA* && 状态空间搜索)

    题意:你有一篇n(2≤n≤9)个自然段组成的文章,希望将它们排列成1,2,…,n.可以用Ctrl+X(剪切)和Ctrl+V(粘贴)快捷键来完成任务.每次可以剪切一段连续的自然段,粘贴时按照顺序粘贴.注 ...

  5. UVa 1343 旋转游戏(dfs+IDA*)

    https://vjudge.net/problem/UVA-1343 题意:如图所示,一共有8个1,8个2和8个3,如何以最少的移动来使得中间8个格子都为同一个数. 思路:状态空间搜索问题. 用ID ...

  6. UVA - 10118Free Candies(记忆化搜索)

    题目:UVA - 10118Free Candies(记忆化搜索) 题目大意:给你四堆糖果,每一个糖果都有颜色.每次你都仅仅能拿随意一堆最上面的糖果,放到自己的篮子里.假设有两个糖果颜色同样的话,就行 ...

  7. 7-10Editing aBook uva11212(迭代加深搜索 IDA*)

    题意:  给出n( 2<=n<=9) 个乱序的数组  要求拍成升序  每次 剪切一段加上粘贴一段算一次  拍成1 2 3 4 ...n即可     求排序次数 典型的状态空间搜索问题   ...

  8. 埃及分数 迭代加深搜索 IDA*

    迭代加深搜索 IDA* 首先枚举当前选择的分数个数上限maxd,进行迭代加深 之后进行估价,假设当前分数之和为a,目标分数为b,当前考虑分数为1/c,那么如果1/c×(maxd - d)< a ...

  9. UVA - 11212 Editing a Book(IDA*算法+状态空间搜索)

    题意:通过剪切粘贴操作,将n个自然段组成的文章,排列成1,2,……,n.剪贴板只有一个,问需要完成多少次剪切粘贴操作可以使文章自然段有序排列. 分析: 1.IDA*搜索:maxn是dfs的层数上限,若 ...

随机推荐

  1. 【神经网络与深度学习】【Qt开发】【VS开发】从caffe-windows-visual studio2013到Qt5.7使用caffemodel进行分类的移植过程

    [神经网络与深度学习][CUDA开发][VS开发]Caffe+VS2013+CUDA7.5+cuDNN配置成功后的第一次训练过程记录<二> 标签:[神经网络与深度学习] [CUDA开发] ...

  2. Java内存溢出

    中间件应用Java内存溢出常见的三种情况: 1.OutOfMemoryError: Java heap space 2.OutOfMemoryError: PermGen space 3.OutOfM ...

  3. NFC读写器调试总结20191203

    以下为NFC读写器调试经验总结: 1.读写器部分,从TX1/TX2输出的13.56MHZ信号主要由L0/C0构成低通滤波器,用于滤除13.56MHZ的高次谐波,取值L0=1UH,C0=47PF时候,谐 ...

  4. spring + redis 实例(一)

    这一篇主要是redis操作工具类以及基本配置文本储存 首先我们需要定义一个redisUtil去操作底层redis数据库: package com.lcc.cache.redis; import jav ...

  5. js汉字转换为拼音

    片段 1 片段 2 gistfile1.txt /* --- description: Pinyin, to get chinese pinyin from chinese. license: MIT ...

  6. webpack入门学习手记(一)

    本人微信公众号:前端修炼之路,欢迎关注. 之前用过gulp.grunt,但是一直没有学习过webpack.这两天刚好有时间,学习了下webpack.webpack要想深入研究,配置的东西比较多,网上的 ...

  7. springboot热部署设置

    springboot提供了热部署,所谓热部署就是当你修改了文件代码,不用重新去启动服务器,而你只要重新build一下当前项目就可以重新编译了.而这就是热部署. 其实springboot热部署就是通过一 ...

  8. sqlite3中 timestamp使用

    timestamp使用 一. timestamp两种属性:自动初始化: 此行为只在第一次写入数据时,怎么把时间设为当前时间. (DEFAULT CURRENT_TIMESTAMP)自动更新: 此行为在 ...

  9. 当有多个相同的DIV时,我怎么判断我点击的是哪个

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. Node.js 最早 npm 包 request 将被废弃

    相信 Node.js 开发者对 Request 都不会陌生,这是一个 Node.js 模块,以 npm 包的形式提供,是一个简单的 HTTP 客户端,通过它可方便地实现 HTTP 请求. 可以看到,r ...