https://code.google.com/codejam/contest/544101/dashboard#s=p0
 
 

Problem

In the exciting game of Join-K, red and blue pieces are dropped into an N-by-N table. The table stands up vertically so that pieces drop down to the bottom-most empty slots in their column. For example, consider the following two configurations:

    - Legal Position -

          .......
.......
.......
....R..
...RB..
..BRB..
.RBBR..
   - Illegal Position -

          .......
.......
.......
.......
Bad -> ..BR...
...R...
.RBBR..

In these pictures, each '.' represents an empty slot, each 'R' represents a slot filled with a red piece, and each 'B' represents a slot filled with a blue piece. The left configuration is legal, but the right one is not. This is because one of the pieces in the third column (marked with the arrow) has not fallen down to the empty slot below it.

A player wins if they can place at least K pieces of their color in a row, either horizontally, vertically, or diagonally. The four possible orientations are shown below:

      - Four in a row -

     R   RRRR    R   R
R R R
R R R
R R R
 
In the "Legal Position" diagram at the beginning of the problem statement, both players had lined up two pieces in a row, but not three.

As it turns out, you are right now playing a very exciting game of Join-K, and you have a tricky plan to ensure victory! When your opponent is not looking, you are going to rotate the board 90 degrees clockwise onto its side. Gravity will then cause the pieces to fall down into a new position as shown below:

    - Start -

     .......
.......
.......
...R...
...RB..
..BRB..
.RBBR..
   - Rotate -

     .......
R......
BB.....
BRRR...
RBB....
.......
.......
   - Gravity -

     .......
.......
.......
R......
BB.....
BRR....
RBBR...
Unfortunately, you only have time to rotate once before your opponent will notice.

All that remains is picking the right time to make your move. Given a board position, you should determine which player (or players!) will have K pieces in a row after you rotate the board clockwise and gravity takes effect in the new direction.

Notes

  • You can rotate the board only once.
  • Assume that gravity only takes effect after the board has been rotated completely.
  • Only check for winners after gravity has finished taking effect.

Input

The first line of the input gives the number of test cases, TT test cases follow, each beginning with a line containing the integers N and K. The next N lines will each be exactly N characters long, showing the initial position of the board, using the same format as the diagrams above.

The initial position in each test case will be a legal position that can occur during a game of Join-K. In particular, neither player will have already formed K pieces in a row.

Output

For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1), and y is one of "Red", "Blue", "Neither", or "Both". Here, y indicates which player or players will have K pieces in a row after you rotate the board.

Limits

1 ≤ T ≤ 100.
3 ≤ K ≤ N.

Small dataset

3 ≤ N ≤ 7.

Large dataset

3 ≤ N ≤ 50.

Sample

Input 
 
Output 
 
4
7 3
.......
.......
.......
...R...
...BB..
..BRB..
.RRBR..
6 4
......
......
.R...R
.R..BB
.R.RBR
RB.BBB
4 4
R...
BR..
BR..
BR..
3 3
B..
RB.
RB.
Case #1: Neither
Case #2: Both
Case #3: Red
Case #4: Blue

 

Solution

int N = ;
int K = ;
char *inp = NULL;
char *inp_cpy = NULL;
int curr_row_cnt = ; bool rd(int r, int c, int ro, int co, char chr) { if (r >= N || r < || c >= N || c < ) return false; if (inp_cpy[r * N + c] == chr) {
curr_row_cnt++; if (curr_row_cnt >= K) return true;
} else {
return false;
} return rd(r + ro, c + co, ro, co, chr);
} char *solve()
{ // Rotate
if (inp_cpy) free(inp_cpy);
inp_cpy = (char *)malloc(sizeof(char) * N * N); for (int i = ; i < N; i++) {
for (int j = ; j < N; j++) {
inp_cpy[N * i + j] = inp[N * (N - j - ) + i];
}
} // G
for (int i = N - ; i >= ; i--) {
for (int j = ; j < N; j++) {
if (inp_cpy[N * i + j] != '.') {
// Can drop
int vp = i + ;
while () {
if (vp < N) {
if (inp_cpy[N * vp + j] == '.') {
vp++;
continue;
}
}
inp_cpy[N * (vp - ) + j] = inp_cpy[N * i + j];
if (vp - != i) inp_cpy[N * i + j] = '.';
break;
}
}
}
} // Determine
bool R = false;
bool B = false; for (int i = ; i < N; i++) {
for (int j = ; j < N; j++) {
if (inp_cpy[N * i + j] == 'R') {
curr_row_cnt = ; if (!R) R = rd(i, j, -, -, 'R');
curr_row_cnt = ; if (!R) R = rd(i, j, -, , 'R');
curr_row_cnt = ; if (!R) R = rd(i, j, -, , 'R'); curr_row_cnt = ; if (!R) R = rd(i, j, , -, 'R');
curr_row_cnt = ; if (!R) R = rd(i, j, , , 'R'); curr_row_cnt = ; if (!R) R = rd(i, j, , -, 'R');
curr_row_cnt = ; if (!R) R = rd(i, j, , , 'R');
curr_row_cnt = ; if (!R) R = rd(i, j, , , 'R'); } else if (inp_cpy[N * i + j] == 'B') {
curr_row_cnt = ; if (!B) B = rd(i, j, -, -, 'B');
curr_row_cnt = ; if (!B) B = rd(i, j, -, , 'B');
curr_row_cnt = ; if (!B) B = rd(i, j, -, , 'B'); curr_row_cnt = ; if (!B) B = rd(i, j, , -, 'B');
curr_row_cnt = ; if (!B) B = rd(i, j, , , 'B'); curr_row_cnt = ; if (!B) B = rd(i, j, , -, 'B');
curr_row_cnt = ; if (!B) B = rd(i, j, , , 'B');
curr_row_cnt = ; if (!B) B = rd(i, j, , , 'B');
} if (R && B)
return "Both";
}
} if (R && !B) {
return "Red";
} else if(B && !R) {
return "Blue";
} else {
return "Neither";
}
} int main()
{
freopen("in.in", "r", stdin);
if (WRITE_OUT_FILE)
freopen("out.out", "w", stdout); int T;
scanf("%d\n", &T);
if (!T) {
cerr << "Check input!" << endl;
exit();
} for (int t = ; t <= T; t++) {
if (WRITE_OUT_FILE)
cerr << "Solving: #" << t << " / " << T << endl; scanf("%d %d\n", &N, &K); if (inp) free(inp);
inp = (char *)malloc(sizeof(char) * N * N);
memset(inp, , sizeof(char) * N * N); for (int i = ; i < N; i++) {
for (int j = ; j < N; j++) {
char tmp;
scanf("%c", &tmp);
inp[i * N + j] = tmp;
}
getchar();
} auto result = solve(); printf("Case #%d: %s\n", t, result);
} fclose(stdin);
if (WRITE_OUT_FILE)
fclose(stdout); return ;
}

Google Code Jam 2010 Round 1A Problem A. Rotate的更多相关文章

  1. Google Code Jam 2010 Round 1C Problem A. Rope Intranet

    Google Code Jam 2010 Round 1C Problem A. Rope Intranet https://code.google.com/codejam/contest/61910 ...

  2. Google Code Jam 2010 Round 1C Problem B. Load Testing

    https://code.google.com/codejam/contest/619102/dashboard#s=p1&a=1 Problem Now that you have won ...

  3. Google Code Jam 2010 Round 1B Problem B. Picking Up Chicks

    https://code.google.com/codejam/contest/635101/dashboard#s=p1   Problem A flock of chickens are runn ...

  4. Google Code Jam 2010 Round 1B Problem A. File Fix-it

    https://code.google.com/codejam/contest/635101/dashboard#s=p0   Problem On Unix computers, data is s ...

  5. dp - Google Code jam Qualification Round 2015 --- Problem B. Infinite House of Pancakes

    Problem B. Infinite House of Pancakes Problem's Link:   https://code.google.com/codejam/contest/6224 ...

  6. Google Code jam Qualification Round 2015 --- Problem A. Standing Ovation

    Problem A. Standing Ovation Problem's Link:   https://code.google.com/codejam/contest/6224486/dashbo ...

  7. Google Code Jam 2016 Round 1B Problem C. Technobabble

    题目链接:https://code.google.com/codejam/contest/11254486/dashboard#s=p2 大意是教授的学生每个人在纸条上写一个自己的topic,每个to ...

  8. Google Code Jam 2008 Round 1A C Numbers(矩阵快速幂+化简方程,好题)

    Problem C. Numbers This contest is open for practice. You can try every problem as many times as you ...

  9. Google Code Jam 2014 Round 1B Problem B

    二进制数位DP,涉及到数字的按位与操作. 查看官方解题报告 #include <cstdio> #include <cstdlib> #include <cstring& ...

随机推荐

  1. php如何将数组保存为文件的方法? 三个方法让你快速把数组保存成为文件存储

    php 缓存数组形式的变量,实际上就是将 php 将数组写入到一个文本文件或者后缀名为 .php 存储起来,使用的时候直接调用这个文件.那么如何使用 php 将数组保存为文本格式的文件呢?下面分享三种 ...

  2. 【OpenStack】OpenStack系列5之Cinder详解

    源码下载安装 git clone -b stable/icehouse https://github.com/openstack/cinder.git pip install -r requireme ...

  3. web开发中目录路径问题的解决

    web开发当中,目录路径的书写是再常用不过了,一般情况下不会出什么问题,但是有些时候出现了问题却一直感到奇怪,所以这里记录一下,彻底解决web开发中路径的问题,开发分为前端和服务端,那么就从这两个方面 ...

  4. 【数据结构】Huffman树

    参照书上写的Huffman树的代码 结构用的是线性存储的结构 不是二叉链表 里面要用到查找最小和第二小 理论上锦标赛法比较好 但是实现好麻烦啊 考虑到数据量不是很大 就直接用比较笨的先找最小 去掉最小 ...

  5. AJAX,JSON搜索智能提示

    效果 开发结构参考AJAX,JSON用户校验 主要有两个核心文件 1,处理输入字符,进行后台搜索的servlet Suggest.java package org.guangsoft.servlet; ...

  6. AU版有锁机的福利,704越狱彻底解决+86问题,完美IM/FT,重启不掉APN设置

    http://bbs.25pp.com/thread-172881-1-1.html 串号99的是au版 串号013的是sb版 44050   AU为 找到咱们SB版的文件,为44020 http:/ ...

  7. 7.适配器模式(Adapter Pattern)

    using System; namespace Test { /// <summary> /// 适配器模式主要解决的问题就是我们要调用的接口类型,无法满足我们新系统的使用需求, /// ...

  8. ASP.NET服务器端执行耗时操作的工作记录

    公司之前有这样一个业务需求: 一名同事做出文件a0和b0,然后将a0加密为a1.b0加密为b1:再将文件a0.a1.b0和b1上传至服务器M:同时要将服务器N上的数据表添加一条记录,该记录的ID就是前 ...

  9. Java Hour 31 Weather ( 4 )

    有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. Hour 31 Maven 的项目结构   Struts 2 Tags < ...

  10. jquery php ajax 表单验证

    本实例用到 JQuery 类库本身的函数和功能,所有表单信息利用 PHPMailer 类库邮件的形式发送.   .创建一个表单 html 页面   表单部分 html 代码   以下为引用内容: &l ...