【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[ ...
随机推荐
- Qt 学习之路 :文件
文件操作是应用程序必不可少的部分.Qt 作为一个通用开发库,提供了跨平台的文件操作能力.从本章开始,我们来了解下 Qt 的文件以及输入输出的功能,也就是 I/O 系统. Qt 通过QIODevice提 ...
- iOS-UITableCell详情
iOS-UITableCell详情 表示UITableViewCell风格的常量有: UITableViewCellStyleDefault UITableViewCellStyleSubtitle ...
- Vector/Arraylist与Linklist的区别
SDK提供了有序集合接口java.util.List的几种实现,其中三种最为人们熟知的是Vector.ArrayList和 LinkedList.有关这些List类的性能差别是一个经常被 ...
- HDFS的Java客户端操作代码(查看HDFS下所有的文件或目录)
1.查看HDFS下所有的文件或目录 package Hdfs; import java.io.IOException; import java.net.URI; import org.apache.h ...
- 安卓数据存储(2):SharedPreferences
SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的就是一个key-value(键值对)在读取数据时可以通过这个键把相应的值取出 ...
- XE6 /XE8 & IOS开发之免证书真机调试三步走,生成iPA文件并安装到其它苹果设备上
XE6 & IOS开发之免证书真机调试(1):颁发属于自己的App签名证书(有图有真相) XE6 & IOS开发之免证书真机调试(2):连接真机并运行App(有图有真相) XE6 &a ...
- 设置 textField.placeholder的颜色和大小
textField.placeholder = @"请输入手机号码"; [textField setValue:[UIColor blue] forKeyPath:@"_ ...
- iOS6以后的单个控制器横竖屏显示以及旋转屏控制技巧,附带iOS8以后显示电池状态栏
一.在应用中从竖屏模式强制转换为横屏模式 第一种方法:通过模态弹出视图的方式,使得特定ViewController坚持特定的interfaceOrientation(1)iOS6之后提供了这样一个方法 ...
- csv文本编辑引号问题
今天发现一个csv的一个问题,csv工具类对于引号默认有特殊的处理.我希望写出来的结果是 1,"1",1 原来的代码是 CsvWriter cw=new CsvWriter(&qu ...
- 【POJ2985】【Treap + 并查集】The k-th Largest Group
Description Newman likes playing with cats. He possesses lots of cats in his home. Because the numbe ...