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. SHSEE 备战最后十(四)天日记

    努力. Day -1 看书.睡觉. Day 0 上午考试.语文纯RP题跪.理总不错. 下午上课,各种神. Day 1 上午下午讲课...Day 0成绩出来才#17.... Day 2 考试..这次题目 ...

  2. sharepoint更新多行文本webparth

    前台 <script> function Copy() { var value = document.getElementById("<%=BodyBox.ClientID ...

  3. 部署Office Web Apps Server并配置其与SharePoint 2013的集成

    部署Office Web Apps Server并配置其与SharePoint 2013的集成   Office Web Apps Server 是新的 Office 服务器产品,它提供 Word.P ...

  4. DP:Sumsets(POJ 2229)

     数的集合问题 题目大意:给定你一个整数m,你只能用2的k次幂来组合这个数,问你有多少种组合方式? 这一题一看,天啦太简单了,完全背包?是不是? 不过的确这一题可以用完全背包来想,但是交题绝对是TLE ...

  5. MFC 颜色选择对话框、颜色按钮

    COLORREF color=RGB(0,255,0); unsigned char r=GetRValue(color); unsigned char g=GetGValue(color); uns ...

  6. android 的四种枚举Context.MODE_PRIVATE

    标签: mode_private Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容,如果想把新写入的内容追加 ...

  7. tomcat The file is absent or does not have execute permission

    [root@centos02 bin]# ./startup.sh Cannot find ./catalina.sh The file is absent or does not have exec ...

  8. snmp v3

    http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=7654720&id=3355515 http://tydldd.ite ...

  9. wpa_supplicant.conf

    转自:http://w1.fi/gitweb/gitweb.cgi?p=hostap.git;a=blob_plain;f=wpa_supplicant/wpa_supplicant.conf ### ...

  10. Virtual Box创建共享目录

    1.先关闭ubuntu,在virtualbox“设置”中找到“共享文件夹”,点击进入,点击右边添加目录按钮,添加windows中要共享的目录,取一个名.比如我在D盘建一个名为share的文件夹,如下图 ...