HDU_3278

尽管如果考虑有三种颜色的话状态数会很多,但如果一旦确定我们要将哪个颜色移到中间,其余的两个颜色就可以视作一种颜色,因此就可以先从终态开始bfs一遍,记录下结果,每次读一个图,就枚举三种颜色当作移到中间的颜色,然后O(1)查询步数即可。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#define HASH 1000007
#define MAXD 1000010
#define INF 0x3f3f3f3f
struct Point
{int st, d;Point(){}Point(int _st, int _d) : st(_st), d(_d){}
};
struct HashMap
{int head[HASH], size, next[MAXD], st[MAXD], d[MAXD];void init(){memset(head, -1, sizeof(head)), size = 0;}int find(int state){int i, h = state % HASH;for(i = head[h]; i != -1; i = next[i])if(st[i] == state) return i;return -1;}void push(int state, int dis){int i, h = state % HASH;st[size] = state, d[size] = dis;next[size] = head[h], head[h] = size ++;}
}hm;
char b[5][10];
int ANS, code[5][10];
void init()
{int i, j;for(i = 0; i < 4; i ++) scanf("%s", b[i]);
}
int encode()
{int i, j, ans = 0;for(i = 0; i < 4; i ++)for(j = 0; j < 6; j ++) ans = ans << 1 | code[i][j];return ans;
}
void decode(int st)
{int i, j;for(i = 3; i >= 0; i --)for(j = 5; j >= 0; j --) code[i][j] = st & 1, st >>= 1;
}
void shr(int r, int k)
{int i;if(k == 0) for(i = 0; i < 5; i ++) std::swap(code[r][i], code[r][i + 1]);else for(i = 5; i >= 1; i --) std::swap(code[r][i], code[r][i - 1]);
}
void shc(int c, int k)
{int i;if(k == 0) for(i = 0; i < 3; i ++) std::swap(code[i][c], code[i + 1][c]);else for(i = 3; i >= 1; i --) std::swap(code[i][c], code[i - 1][c]);
}
void prepare()
{int i, j, c;std::queue <Point> q;hm.init();memset(code, 0, sizeof(code));for(i = 1; i <= 2; i ++)for(j = 1; j <= 4; j ++) code[i][j] = 1;c = encode();q.push(Point(c, 0)), hm.push(c, 0);while(!q.empty()){Point p = q.front();q.pop();decode(p.st);for(i = 0; i < 4; i ++)for(j = 0; j < 2; j ++){shr(i, j), c = encode();if(hm.find(c) == -1) q.push(Point(c, p.d + 1)), hm.push(c, p.d + 1);shr(i, j ^ 1);}for(i = 0; i < 6; i ++)for(j = 0; j < 2; j ++){shc(i, j), c = encode();if(hm.find(c) == -1) q.push(Point(c, p.d + 1)), hm.push(c, p.d + 1);shc(i, j ^ 1);    }}
}
void deal(char ch)
{int i, j, c;for(i = 0; i < 4; i ++)for(j = 0; j < 6; j ++) code[i][j] = b[i][j] == ch;c = encode();i = hm.find(c);ANS = std::min(ANS, hm.d[i]);
}
void solve()
{int i, j;ANS = INF;deal('B'), deal('W'), deal('G');printf("%d\n", ANS);
}
int main()
{int t, tt;prepare();scanf("%d", &t);for(tt = 1; tt <= t; tt ++){init();printf("Case %d: ", tt);solve();    }return 0;
}

HDU 3278 Puzzle相关推荐

  1. 杭电OJ分类题目(1)

    原题出处:HDOJ Problem Index by Type,http://acm.hdu.edu.cn/typeclass.php 杭电OJ分类题目(1) HDU Introduction HDU ...

  2. 杭电OJ分类题目(2)

    原题出处:HDOJ Problem Index by Type,http://acm.hdu.edu.cn/typeclass.php 杭电OJ分类题目(2) HDU Water~~~ HDU 100 ...

  3. 2021杭电多校补题——第一场

    2021杭电多校补题--第一场 文章目录 Mod, Or and Everything Rocket land(待补) Puzzle loop(待补) Another thief in a Shop( ...

  4. hdu 4662 MU Puzzle

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4662 MU Puzzle Time Limit: 2000/1000 MS (Java/Others) ...

  5. POJ 3278 / hdu 2717 Catch That Cow (广搜)

    POJ 3278 HDU 2717 广搜题,用一个数组标记就可以过,不标记的话会超内存. 另外,poj的数据要比hdu强一些,比如0 100,这种数据.不特判的话会RE.不过如果不特判,在poj上用C ...

  6. HDU 4708 Rotation Lock Puzzle(模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4708 题目大意:给定一个方形矩阵,边长为3-10的奇数.每一圈的数字可以沿着顺时针方向和逆时针方向旋转 ...

  7. 【HDU - 5456】Matches Puzzle Game(数位dp,思维)

    题干: As an exciting puzzle game for kids and girlfriends, the Matches Puzzle Game asks the player to ...

  8. HDU 5465 Clarke and puzzle (二维树状数组维护区间异或)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5465 解题思路: 因为要对大量的区间进行异或,所以考虑用二维树状数组就行维护. #include< ...

  9. HDU - 5411 CRB and Puzzle 矩阵快速幂

    HDU - 5411 考虑直接dp会T, 用矩阵优化一下就好了. #include<bits/stdc++.h> #define LL long long #define LD long ...

最新文章

  1. 第37篇 Asp.Net源码解析(二)--详解HttpApplication
  2. flutter 代码仓库_go-flutter开发桌面应用(二) 创建go-flutter插件
  3. 游戏服务器端引擎——DogSE的设计
  4. 技术创作101训练营:从0到1教你搭建自己的技术品牌
  5. matlab多径信道模型,基于matlab的无线多径信道建模与仿真分析
  6. python如何删除对象属性_如何优雅的删除对象中的指定属性?
  7. 一样入职的应届生工资不一样_为什么每月工资一样,但扣的个税不一样?
  8. android菜单详解四:子菜单
  9. html jwt权限控制,SpringBoot+SpringSecurity+JWT实RESTfulAPI权限控制
  10. HTML中常见元素及格式
  11. linux信号常用函数
  12. 回忆——我对方向的选择
  13. stm32F205程序移植到stm32F405片子,使用FPU时注意事项
  14. Python之网络数据采集入门常用模块初识
  15. 电脑鼠标右键菜单反应慢怎么办?右键菜单反应慢解决办法
  16. 综述 | 一文读懂自然语言处理NLP(附学习资料)
  17. Android 定制关机界面
  18. 安卓recovery系统详解
  19. 【长尾理论】的简单阐述
  20. 远程桌面连接的工具及使用方法

热门文章

  1. OpenCV准密集立体声Quasi dense Stereo
  2. QDoc分组事物Grouping Things
  3. C++lowest common ancestor最近公共祖先算法(附完整源码)
  4. QT实现Media Player(媒体播放器)
  5. C和C++线性表基本概念
  6. mes系统服务器要求,mes系统 服务器配置
  7. html 判断是苹果几个版本号,这是我第一次遇到判断ios系统版本的问题
  8. Elasticsearch中的document数据格式,简单的集群管理,商品的索引的CRUD操作(学习资料记录)
  9. SQLite Where 子句(http://www.w3cschool.cc/sqlite/sqlite-where-clause.html)
  10. jps,jstat,jinfo,jmap,jhat,jstack工具的使用/查看Linux磁盘信息