题意:给定一个九宫格,然后能够选择某行或者是某列滚动,每个小方格分为上下左右四个块,每个块可以涂上4种不同的颜色。问最少使用多少步能够使得所有相同颜色相互联通。

分析:由于九宫格的所有的状态只有9!(362880)种,于是想起了先对每个格子进行标号,然后根据逆序对的数量进行一个递增进制数的hash处理,这样bfs搜索就能够去重了,接着只需要判断每个状态是否满足联通即可。这里的判定选择了先统计了不同颜色格子的数量然后dfs搜索,应该并查集也可以搞。滚动使用指针滚动即可,对于不能动的格子该格子所在行和列都不能够滚动。

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std; // 9! = 362880
const int LIM = ;
int fac[]; // 递增进制数的权值
int tot[]; // 四种颜色的个数
char vis[LIM]; // hash九宫格
char op[]; // 滚动的操作的限制
char hav[]; // 4*9个格子的标记 struct Block {
char color[];
int id;
}bk[][]; struct Node {
Block *ptr[][];
int key;
int ti;
Node() {}
Node(Block (*x)[]) {
ti = ;
for (int i = ; i < ; ++i) {
for (int j = ; j < ; ++j) {
ptr[i][j] = &x[i][j];
}
}
getkey();
}
void show() {
for (int i = ; i < ; ++i) {
for (int j = ; j < ; ++j) {
printf("%5d", ptr[i][j]->id);
}
puts("");
}
puts("");
}
void getkey() {
int cnt;
key = ;
for (int i = ; i < ; ++i) {
cnt = ;
for (int j = i+; j < ; ++j) {
if (ptr[j/][j%]->id < ptr[i/][i%]->id) ++cnt;
}
key += cnt * fac[-i];
}
}
int dfs(int x, int y, int z) {
if (hav[(x*+y)*+z]) return ;
hav[(x*+y)*+z] = ;
char ch = ptr[x][y]->color[z];
int ret = ;
if (z == ) {
if (ptr[x][y]->color[] == ch) ret += dfs(x, y, );
if (ptr[x][y]->color[] == ch) ret += dfs(x, y, );
if (x > && ptr[x-][y]->color[] == ch) ret += dfs(x-, y, );
} else if (z == ) {
if (ptr[x][y]->color[] == ch) ret += dfs(x, y, );
if (ptr[x][y]->color[] == ch) ret += dfs(x, y, );
if (x < && ptr[x+][y]->color[] == ch) ret += dfs(x+, y, );
} else if (z == ) {
if (ptr[x][y]->color[] == ch) ret += dfs(x, y, );
if (ptr[x][y]->color[] == ch) ret += dfs(x, y, );
if (y > && ptr[x][y-]->color[] == ch) ret += dfs(x, y-, );
} else {
if (ptr[x][y]->color[] == ch) ret += dfs(x, y, );
if (ptr[x][y]->color[] == ch) ret += dfs(x, y, );
if (y < && ptr[x][y+]->color[] == ch) ret += dfs(x, y+, );
}
return ret;
}
bool judge() {
memset(hav, , sizeof (hav));
for (int i = ; i < ; ++i) {
for (int j = ; j < ; ++j) {
for (int k = ; k < ; ++k) {
if (!hav[(i*+j)*+k]) {
int cnt = dfs(i, j, k);
switch(ptr[i][j]->color[k]) {
case 'R': if (cnt != tot[]) return false; break;
case 'G': if (cnt != tot[]) return false; break;
case 'B': if (cnt != tot[]) return false; break;
default : if (cnt != tot[]) return false;
}
}
}
}
}
return true;
}
}; void pre() {
fac[] = ;
for (int i = ; i < ; ++i) {
fac[i] = fac[i-] * i;
}
} void swapr(Block *x[], Block *y[], int dir) { // 横向的滚动
if (dir == ) { // 向左
y[] = x[], y[] = x[], y[] = x[];
} else { // 向右
y[] = x[], y[] = x[], y[] = x[];
}
} void swapc(Block *x[], Block *y[], int dir) { // 纵向的滚动
if (dir == ) { // 向上
y[] = x[], y[] = x[], y[] = x[];
} else { // 向下
y[] = x[], y[] = x[], y[] = x[];
}
} int bfs() {
queue<Node>q;
Node tmp = Node(bk);
Node pos;
memset(vis, , sizeof (vis));
q.push(tmp); // 初始化是没有逆序对的
vis[tmp.key] = ;
while (!q.empty()) {
pos = q.front();
q.pop();
if (pos.judge()) {
return pos.ti;
}
for (int i = ; i < ; ++i) {
if (op[i]) continue;
tmp = pos;
if (i < ) { // 横向的滚动
for (int j = ; j < ; ++j) {
swapr(&pos.ptr[i][], &tmp.ptr[i][], j);
tmp.getkey();
tmp.ti = pos.ti + ;
if (!vis[tmp.key]) {
vis[tmp.key] = ;
q.push(tmp);
}
}
} else { // 纵向的
for (int j = ; j < ; ++j) {
swapc(&pos.ptr[][i%], &tmp.ptr[][i%], j);
tmp.getkey();
tmp.ti = pos.ti + ;
if (!vis[tmp.key]) {
vis[tmp.key] = ;
q.push(tmp);
}
}
}
}
}
} int main() {
int T, ca = ;
pre();
scanf("%d", &T);
while (T--) {
memset(op, , sizeof (op));
memset(tot, , sizeof (tot));
for (int i = ; i < ; ++i) {
for (int j = ; j < ; ++j) {
scanf("%s", bk[i][j].color);
for (int k = ; k < ; ++k) {
switch(bk[i][j].color[k]) {
case 'R': ++tot[]; break;
case 'G': ++tot[]; break;
case 'B': ++tot[]; break;
default : ++tot[];
}
}
bk[i][j].id = i*+j; // 0-8
if (bk[i][j].color[] == '') {
op[i] = op[+j] = ; // 两种操作无法进行
}
}
}
printf("Case #%d: %d\n", ++ca, bfs());
}
return ;
}

HDU-4531 吉哥系列故事——乾坤大挪移 模拟的更多相关文章

  1. hdu 4512 吉哥系列故事——完美队形I【LCIS经典应用】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=4512 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  2. HDU 4513 吉哥系列故事――完美队形II(Manacher)

    题目链接:cid=70325#problem/V">[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher V - 吉哥系列故事――完美队形I ...

  3. HDU 4513 吉哥系列故事――完美队形II

    http://acm.hdu.edu.cn/showproblem.php?pid=4513 吉哥系列故事——完美队形II Time Limit: 3000/1000 MS (Java/Others) ...

  4. HDU 4513 吉哥系列故事——完美队形II manacher

    吉哥系列故事——完美队形II Problem Description 吉哥又想出了一个新的完美队形游戏! 假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希 ...

  5. hdu 4513 吉哥系列故事——完美队形II (manachar算法)

    吉哥系列故事——完美队形II Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) P ...

  6. HDU 4512——吉哥系列故事——完美队形I——————【LCIS应用】

    吉哥系列故事——完美队形I Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  7. HDU 4513 吉哥系列故事——完美队形II(Manacher)

    Problem Description 吉哥又想出了一个新的完美队形游戏! 假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出一些人,让这些人形成 ...

  8. HDU 4512 吉哥系列故事——完美队形(LCIS)

    Problem Description 吉哥这几天对队形比较感兴趣. 有一天,有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出一些人,让这些人形成一 ...

  9. hdu 4502吉哥系列故事——临时工计划 (简单DP)

    Problem Description 俗话说一分钱难倒英雄汉,高中几年下来,吉哥已经深深明白了这个道理,因此,新年开始存储一年的个人资金已经成了习惯,不过自从大学之后他不好意思再向大人要压岁钱了,只 ...

随机推荐

  1. xib连线出错,模型保存cell状态(最后个Cell隐藏分割线),

    一个.m文件中有好几个cell类,拖线,要看看该控件对应的是哪个类,否则点击事件不响应,因为归属的xib错了 拖不过来线,因为是view拖不动,加了个button就行了   使用模型属性记录是否隐藏c ...

  2. Portable Operating System Interface for uni-X

    https://kb.iu.edu/d/agjv Short for "Portable Operating System Interface for uni-X", POSIX ...

  3. yii框架分页

  4. Java微信公众号开发

    微信公众平台是腾讯为了让用户申请和管理微信公众账号而推出的一个web平台.微信公众账号的种类可以分为3种,并且一旦选定不可更改.按照功能的限制从小到大依次为:订阅号.服务号.企业号.个人只能注册订阅号 ...

  5. tableView主从表在storyboard连线是 Selcetion Segue和Accessory Action的区别

    当按住Ctorl这样连线时会出现,当选择Selection Segue下面的push时,点击cell的任何位置都会跳转到下一个tableView,当选择Accessory Action的下面的push ...

  6. [转]使用Gradle发布Android开源项目到JCenter

      转自:http://blog.csdn.net/maosidiaoxian/article/details/43148643 使用Gradle发布Android开源项目到JCenter 分类: G ...

  7. [转][Android][Android Studio] *.jar 与 *.aar 的生成与*.aar导入项目方法

     转自:http://blog.csdn.net/qiujuer/article/details/39754517?utm_source=tuicool [Android][Android Studi ...

  8. LeetCode Minimum Height Trees

    原题链接在这里:https://leetcode.com/problems/minimum-height-trees/ 题目: For a undirected graph with tree cha ...

  9. 安装docker-compose

    下载到合适的位置 curl -L https://github.com/docker/compose/releases/download/1.8.0/docker-compose-`uname -s` ...

  10. 十分钟学会python

    1.raw_input的使用 从键盘读取信息,返回字符串. 例: hrs = raw_input("Enter Hours:")pay=raw_input("Enter ...