wa了几次,至少要考虑4个方向:下、右、左下、右下。很像当年北航的机试题目。

 /* 2699 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
using namespace std; #define MAXN 16 typedef struct node_t {
char x, y, d, c;
bool f;
node_t() {}
node_t(char xx, char yy, char dd, char cc, bool ff) {
x = xx; y = yy; d = dd; c = cc; f = ff;
}
} node_t; char map[MAXN][MAXN];
char n = ;
char ch;
char dir[][] = {
// right, down, diag(2)
, , ,, , , ,-
}; bool check(char x, char y) {
return x< || x>=n || y< || y>=n;
} bool bfs(char xx, char yy) {
queue<node_t> Q;
node_t nd;
char i, j, k;
char x, y; for (i=; i<; ++i) {
nd.x = xx + dir[i][];
nd.y = yy + dir[i][];
nd.d = i;
nd.c = ;
if (check(nd.x, nd.y))
continue;
if (map[nd.x][nd.y] == ch) {
nd.f = false;
Q.push(nd);
x = xx - dir[i][];
y = yy - dir[i][];
if (!check(x, y) && map[x][y]=='.') {
nd.f = true;
nd.c = ;
Q.push(nd);
}
} else if (map[nd.x][nd.y] == '.') {
nd.f = true;
Q.push(nd);
}
} while (!Q.empty()) {
nd = Q.front();
Q.pop();
if (nd.c==) {
return true;
}
nd.x += dir[nd.d][];
nd.y += dir[nd.d][];
if (check(nd.x, nd.y))
continue;
--nd.c;
if (map[nd.x][nd.y] == ch) {
Q.push(nd);
} else if (map[nd.x][nd.y]=='.' && nd.f==false){
nd.f = true;
Q.push(nd);
}
} return false;
} int main() {
int t, tt=;
int w, b;
char i, j, k;
bool flag; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif scanf("%d", &t);
while (t--) {
w = b = ;
for (i=; i<n; ++i) {
scanf("%s", map[i]);
for (j=; j<n; ++j) {
if (map[i][j] == 'W')
++w;
else if (map[i][j] == 'B')
++b;
}
}
ch = (w==b) ? 'B' : 'W';
flag = false;
for (i=; i<n; ++i) {
for (j=; j<n; ++j) {
if (map[i][j]==ch && bfs(i,j)) {
flag = true;
goto _output;
}
}
}
_output:
if (flag)
puts("YES");
else
puts("NO");
} return ;
}

【HDOJ】2699 Five in a Row的更多相关文章

  1. 【HDOJ】4729 An Easy Problem for Elfness

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

  2. 【C#】【MySQL】C# 查询数据库语句@Row:=@Row+1

    如何实现数据库查询产生虚拟的一列序号的功能: ) )AS r; 该语句可以实现产生虚拟的一列数据在MySQL中运行没有问题. 但是在C#里面调用去出现了错误"Parameter '@ROW' ...

  3. 【HDOJ】【3506】Monkey Party

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

  4. 【HDOJ】【3516】Tree Construction

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

  5. 【HDOJ】【3480】Division

    DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...

  6. 【HDOJ】【2829】Lawrence

    DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...

  7. 【HDOJ】【3415】Max Sum of Max-K-sub-sequence

    DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...

  8. 【HDOJ】【3530】Subsequence

    DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...

  9. 【HDOJ】【3068】最长回文

    Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...

随机推荐

  1. 简单的代码实现的炫酷navigationbar

    动图 技术原理: 当你下拉scrollview的时候,会监听scrollview的contentOffset来调整头部背景图片的位置,通过CGAffineTransformMakeTranslatio ...

  2. HTML简单介绍及常见元素

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  3. Java(Android)线程池 总结

        JAVA的Executors源码:(可以看出底层都是通过ThreadPoolExecutor来具体设置的~) public static ExecutorService newCachedTh ...

  4. logic:present 和 logic:empty的用法 (转)

    logic:empty和logic:notEmpty logic:empty标签判断脚本变量是否为null,是否是一个空的字符串(长度为0),是否是一个空的collection或map(调用isEmp ...

  5. Android开发----权限大全

    一.添加权限格式:     示例:      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STOR ...

  6. 表单提交对chrome记住密码的影响

    在处理注册.登录等含有用户名,密码的元素的表单时,chrome会主动的提示记住密码,然而这个功能在用户名的选择上真是耐人寻味,它总是寻找离password input控件最近的那一个文本框的内容,作为 ...

  7. linux 命令学习(4)

    Linux中常用的关机和重新启动命令有shutdown.halt.reboot以及init,它们都可以达到关机和重新启动的目的,但是每个命令的内部工作过程是不同的,下面将逐一进行介绍. 1. shut ...

  8. 解决NSAttributedString与UILabel高度自适应计算问题

    两个类扩展方法: /** *  修改富文本的颜色 * *  @param str   要改变的string *  @param color 设置颜色 *  @param range 设置颜色的文字范围 ...

  9. mac 连接linux

    1.ssh 通过ssh 的方式直接连接linux ssh name@ip -22 例:ssh  zyc@192.168.1.100 -22 这个的前提是linux 要开启ssh 服务 先看一下linu ...

  10. AngularJS+NodeJS环境搭建

    需要安装的软件: node-v0.12.7-x64.msi python-2.7.10.amd64.msi Git-2.5.1-64-bit.exe (注意:Git安装时,需要选择的步骤)  安装位置 ...