【HDOJ】3309 Roll The Cube
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的更多相关文章
- 【HDOJ】3220 Alice’s Cube
状态压缩+逆向BFS.方向数组就是任意相邻的两点(初始化时减1),每个顶点均有4个相邻点.因此,共有16*4/2=32个方向.按序排列即可找到. /* 3220 */ #include <ios ...
- hdu 3309 Roll The Cube ( bfs )
Roll The Cube Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- 【HDOJ】4801 Pocket Cube 的几种解法和优化
1. 题目描述给定一个$2 \times 2 \times 2$的魔方,当某个面上的4个小块颜色均相同时,称这个面为complete.求对这个魔方进行$n \in [1,7]$次旋转(沿某个面顺时针或 ...
- 【HDOJ】3584 Cube
三位树状数组. /* 3584 */ #include <iostream> #include <string> #include <map> #include & ...
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- 【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) ...
- 【转】Kylin中的cube构建
http://blog.csdn.net/yu616568/article/details/50365240 前言 在使用Kylin的时候,最重要的一步就是创建cube的模型定义,即指定度量和维度 ...
- 【HDOJ】【3506】Monkey Party
DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...
- 【HDOJ】【3516】Tree Construction
DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...
随机推荐
- mysql内核源代码深度解析 缓冲池 buffer pool 整体概述
http://blog.csdn.net/cjcl99/article/details/51063078
- [Android 中级]Voip之CSipSimple类库的编绎
CSipSimple是什么?是一款基于pjsip的Android客户端,相信想要研究VOIP通讯的朋友一定不会陌生,这里我就把如何编译CSipSimple写下来. 首先从CSipSimple官方网站上 ...
- mysql 异常处理实例
1. 语法: DECLARE handler_action HANDLER FOR condition_value [, condition_value] ... statement handler_ ...
- iOS UIKit:App
1.App生命周期 IOS架构是由许多设计模式实现,如model-view-controller 和 delegation模式. 1.1 main函数 与其它框架类似,IOS框架的入口也是从main函 ...
- jQuery 获取文件后缀的方法
var location=$("input[name='file']").val(); var point = location.lastIndexOf("." ...
- webservice 发布到外网的时候
在web.config的<system.web></system.web>中间加入如下配置节内容<webServices> <protoco ...
- 关于this 的一个问题
var name = "the window"; var object = { name:"my object"; getName:function(){ re ...
- ASP.NET如何发布更新
如果一个应用程序迭代开发后,我们如何来进行发布更新呢? 这里,我用Visual Studio 2008和SQL server来演示. 一.程序修改完毕后,我们要进行以下操作: 1.1 点击解决方案,重 ...
- 01.WPF中制作无边框窗体
[引用:]http://blog.csdn.net/johnsuna/article/details/1893319 众所周知,在WinForm中,如果要制作一个无边框窗体,可以将窗体的FormB ...
- MySQL的C++简单封装
/* *介绍:MySQL的简单封装,支持流操作输入输出MySQL语句,然而并没有什么软用,大二学生自娱自乐,有不足求指点 *作者:MrEO *日期:2016.3.26 */ 头文件 my_sql.h ...