Google Code Jam 2010 Round 1A Problem A. Rotate
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 - ....... |
- Illegal Position - ....... |
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 |
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 - ....... |
- Rotate - ....... |
- Gravity - ....... |
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, T. T 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 |
Case #1: Neither |
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Google Code Jam 2016 Round 1B Problem C. Technobabble
题目链接:https://code.google.com/codejam/contest/11254486/dashboard#s=p2 大意是教授的学生每个人在纸条上写一个自己的topic,每个to ...
- 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 ...
- Google Code Jam 2014 Round 1B Problem B
二进制数位DP,涉及到数字的按位与操作. 查看官方解题报告 #include <cstdio> #include <cstdlib> #include <cstring& ...
随机推荐
- SHSEE 备战最后十(四)天日记
努力. Day -1 看书.睡觉. Day 0 上午考试.语文纯RP题跪.理总不错. 下午上课,各种神. Day 1 上午下午讲课...Day 0成绩出来才#17.... Day 2 考试..这次题目 ...
- 利用zabbix监控某个目录大小
近期,因为JMS的消息堆积导致ApacheMQ频率故障(消息没有被消费掉,导致其数据库达到1.2G,JMS此时直接挂掉),很是郁闷!刚好自 己在研究zabbix.既然zabbix如此强大,那么它可以监 ...
- Linux awk命令详解??????????(研究)
http://blog.chinaunix.net/uid-25120309-id-3801250.html 一. AWK 说明 awk是一种编程语言,用于在linux/unix下对文本和数据进行 ...
- Backpack | & ||
Backpack | Given n items with size Ai, an integer m denotes the size of a backpack. How full you can ...
- CodeForces - 426B(对称图形)
Sereja and Mirroring Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64 ...
- c# 正则表达式 匹配回车
1 "." 匹配除 "\n" 之外的任何单个字符,一般用".*?"匹配不包括回车的任意字符. 2 我们在用正则表达式分析html或者是xml ...
- Linux命令自己总结
对于每一个Linux学习者来说,了解Linux文件系统的目录结构,是学好Linux的至关重要的一步.,深入了解linux文件目录结构的标准和每个目录的详细功能,对于我们用好linux系统只管重要,下面 ...
- 二叉树学习笔记之二叉查找树(BSTree)
二叉查找树即搜索二叉树,或者二叉排序树(BSTree),学习回顾一下有关的知识. >>关于二叉查找树 二叉查找树(Binary Search Tree)是指一棵空树或者具有下列性质的二叉树 ...
- Mac下搭建go语言开发环境
一.下载安装go 到墙内下载go的安装包: http://www.golangtc.com/download 点击安装包然后进行安装 二.配置 1.查看环境 go version 2.安装完sdk之后 ...
- AppInventor学习笔记(二)——Hello Purr
一.设计组件 1.定义: 就是WPF中的控件一个意思,直接调用不需要知道怎么写的,只需要写响应函数即可. 2.Designer: (1)界面如下: (2)分区: ①预览窗口: 用于放置应用中所需的组件 ...