HDU 4531
很容易判断是BFS,可是,呵呵呵呵呵呵。。。。。。。。。
HASH判重吧,判连通可以用并查集。
以下代码是转别人的,我码了一下午,发觉越码越丑,呵呵了。
http://www.cnblogs.com/Lyush/p/3416507.html
#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的更多相关文章
- hdu 4531 bfs(略难)
题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...
- HDU 4531 bfs/康拓展开
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4531 吉哥系列故事——乾坤大挪移 Time Limit: 2000/1000 MS (Java/Othe ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
随机推荐
- Java中继承,类的高级概念的知识点
1. 继承含义 在面向对象编程中,可以通过扩展一个已有的类,并继承该类的属性和行为,来创建一个新的类,这种方式称为继承(inheritance). 2. 继承的优点 A.代码的可重用性 B.子类可以扩 ...
- 自己动手丰衣足食,为Zepto添加Slide动画效果
一.缘由 公司的移动端项目,采用zepto为主要框架,但是zepto毕竟是精简版的jquery,体积小了,功能自然没有这么强大,特别是动画和选择器这两块,需要我们自己去拓展. 在项目开发过程中,很多页 ...
- Rocky(模拟)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2718 题意:如果没有障碍就按原方向直走,否则就 ...
- selenium3 + python - alert_is_present定位
from selenium import webdriverfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium ...
- B - Link/Cut Tree
Problem description Programmer Rostislav got seriously interested in the Link/Cut Tree data structur ...
- 5.20 mybatis反向生成的映射文件xml(如果需要自己定义其他sql语句时如下)
解决mybatis-generator 生成的mapper.xml覆盖自定义sql的问题 mybatis-generator是个好工具,一建即可生成基本增删改成功能的mapper.xml.但这些是不够 ...
- Java 中静态变量和实例变量区别
Java 中静态变量和实例变量区别 静态变量属于类,该类不生产对象,通过类名就可以调用静态变量. 实例变量属于该类的对象,必须产生该类对象,才能调用实例变量. 在程序运行时的区别: 实例变量属于某个对 ...
- 安装pywinauto的步骤
team准备搞自动化测试(桌面WPF系统),这几天一直在找自动化测试工具.发现了pywinauto这款工具,许多网友反应很好用,于是下载下来试用.不得不说遇到的坑真不少,记录下来以备不时之需. 前段时 ...
- 安卓桌布显示的dip和px
安卓程序设计界面显示设置图像大小,在layout.xml里面有dip和px选项,dip为 什么 暂时还不知道,或许是设计桌布的设定像素比率,px为像素值: 比如我的手机是 Lenovo K920,屏幕 ...
- [读书笔记]-技术学习-Redis
1:Redis概览 Remote Dictionary Server 远程字典服务 Redis是基于内存的存储 在一台普通的笔记本上,Redis每秒的读取速度可以达到10万 内存读取数据,断电的时候数 ...