BFS,考虑一球进洞仅一球滚动以及两球重叠的情况即可。

 /* 3309 */
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std; #define MAXN 25 typedef struct {
int x[], y[];
bool in[];
int t;
} node_t; int n, m;
node_t beg;
bool visit[MAXN][MAXN][MAXN][MAXN];
char map[MAXN][MAXN];
int dir[][] = {
-,,,,,-,,
}; inline bool check(int x, int y) {
return x< || x>=n || y< || y>=m;
} int bfs() {
int i, j, k;
node_t d, nd;
queue<node_t> Q; memset(visit, false, sizeof(visit));
visit[beg.x[]][beg.y[]][beg.x[]][beg.y[]] = true;
Q.push(beg); while (!Q.empty()) {
nd = Q.front();
Q.pop();
if (!nd.in[]) {
if (nd.in[] == false) {
if (map[nd.x[]][nd.y[]] == 'H')
nd.in[] = true;
}
if (nd.in[]) {
if (map[nd.x[]][nd.y[]]=='H' && (nd.x[]!=nd.x[] || nd.y[]!=nd.y[]))
nd.in[] = true;
} else {
if (map[nd.x[]][nd.y[]]=='H') {
nd.in[] = true;
}
}
} else {
if (!nd.in[]) {
if (map[nd.x[]][nd.y[]]=='H' && (nd.x[]!=nd.x[] || nd.y[]!=nd.y[]))
nd.in[] = true;
}
}
if (nd.in[] && nd.in[])
return nd.t;
++nd.t;
for (i=; i<; ++i) {
d = nd;
if (nd.in[] == false) {
d.x[] += dir[i][];
d.y[] += dir[i][];
if (check(d.x[], d.y[]))
continue;
if (map[d.x[]][d.y[]] == '*') {
d.x[] = nd.x[];
d.y[] = nd.y[];
}
}
if (nd.in[] == false) {
d.x[] += dir[i][];
d.y[] += dir[i][];
if (check(d.x[], d.y[]))
continue;
if (map[d.x[]][d.y[]] == '*') {
d.x[] = nd.x[];
d.y[] = nd.y[];
}
}
if (d.in[]==false && d.in[]==false) {
if (d.x[]==d.x[] && d.y[]==d.y[])
continue;
}
if (visit[d.x[]][d.y[]][d.x[]][d.y[]])
continue;
visit[d.x[]][d.y[]][d.x[]][d.y[]] = true;
Q.push(d);
}
} return -;
} int main() {
int t;
int i, j, k; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif scanf("%d", &t);
beg.in[] = beg.in[] = false;
beg.t = ;
while (t--) {
scanf("%d %d", &n, &m);
k = ;
for (i=; i<n; ++i) {
scanf("%s", map[i]);
for (j=; j<m; ++j) {
if (map[i][j] == 'B') {
beg.x[k] = i;
beg.y[k] = j;
++k;
}
}
}
k = bfs();
if (k < )
puts("Sorry , sir , my poor program fails to get an answer.");
else
printf("%d\n", k);
} return ;
}

【HDOJ】3309 Roll The Cube的更多相关文章

  1. 【HDOJ】3220 Alice’s Cube

    状态压缩+逆向BFS.方向数组就是任意相邻的两点(初始化时减1),每个顶点均有4个相邻点.因此,共有16*4/2=32个方向.按序排列即可找到. /* 3220 */ #include <ios ...

  2. hdu 3309 Roll The Cube ( bfs )

    Roll The Cube Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  3. 【HDOJ】4801 Pocket Cube 的几种解法和优化

    1. 题目描述给定一个$2 \times 2 \times 2$的魔方,当某个面上的4个小块颜色均相同时,称这个面为complete.求对这个魔方进行$n \in [1,7]$次旋转(沿某个面顺时针或 ...

  4. 【HDOJ】3584 Cube

    三位树状数组. /* 3584 */ #include <iostream> #include <string> #include <map> #include & ...

  5. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  6. 【BZOJ】3309: DZY Loves Math 莫比乌斯反演优化

    3309: DZY Loves Math Description 对于正整数n,定义f(n)为n所含质因子的最大幂指数.例如f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10007) ...

  7. 【转】Kylin中的cube构建

    http://blog.csdn.net/yu616568/article/details/50365240 前言   在使用Kylin的时候,最重要的一步就是创建cube的模型定义,即指定度量和维度 ...

  8. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  9. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

随机推荐

  1. 自定义控件(视图)2期笔记10:自定义视图之View事件分发机制("瀑布流"的案例)

    1. Touch事件的传递:   图解Touch事件的传递,如下: 当我们点击子View 02内部的Button控件时候,我们就触发了Touch事件. • 这个Touch事件首先传递给了顶级父View ...

  2. href与src的区别

    src是source的缩写,指向外部资源的位置,指向的内容将会嵌入到文档中当前标签所在位置:在请求src资源时会将其指向的资源下载并应用到文档内,例如js脚本,img图片和frame等元素. href ...

  3. Red Hat Enterprise Linux 6安装步骤

    首先,准备安装环境,此次实验是在VMware Workstation虚拟机环境下来实现的,下面就开始安装: 点击Create a New Vitrual Machine来新建一个虚拟机,选择自定义安装 ...

  4. C++ hello world

    日文版本的vs 2008 , 在 < 新建 里面先创建一个项目 然后点击项目去创建一个C++的主启动文件 选择创建的文件类型 然后在文件里面写入代码 #include<iostream&g ...

  5. HTML5 canvas createRadialGradient()放射状/圆形渐变

    定义和用法 createLinearGradient() 方法创建放射状/圆形渐变对象. 渐变可用于填充矩形.圆形.线条.文本等等. 提示:请使用该对象作为 strokeStyle 或 fillSty ...

  6. 非常不错的ASP操作数据库类,支持多数据库MSSQL,ACCESS,ORACLE,MYSQL等

    可同时操作多个不同类型的数据库. 完全不用考虑数据类型的差别,再也不用想字符型字段加不加单引号. 调用非常简单,对数据库的主要操作一般只需要一行代码. 支持mssql事务回滚. 可自动生成和输出sql ...

  7. 用java写bp神经网络(三)

    孔子曰,吾日三省吾身.我们如果跟程序打交道,除了一日三省吾身外,还要三日一省吾代码.看代码是否可以更简洁,更易懂,更容易扩展,更通用,算法是否可以再优化,结构是否可以再往上抽象.代码在不断的重构过程中 ...

  8. 请教如何实现UITextField值变化的实时监视

    上网搜索以后发现基本的处理方法大概有三种1.KVO方式[textField addObserver:self forKeyPath:@"text" options:0 contex ...

  9. IIViewDeckController的使用,左右拖拉菜单效果实现

    博客园   IIViewDeckController的使用,左右拖拉菜单效果实现   很多应用里面都实现了对应的侧拉 显示隐藏的效果,看起来很符合用户体验的类似于这种   看起来很好看,今天去晚上搜下 ...

  10. Flightgear 编译

    一.FlightGear简介 FlightGear 始于1997年,是一个开源的多平台飞行模拟器. 二.FlightGear编译过程 FlightGear平台的说明文档见:http://wiki.fl ...