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& ...
随机推荐
- How to Configure Nginx for Optimized Performance
Features Pricing Add-ons Resources | Log in Sign up Guides & Tutorials Web Server Guides Nginx ...
- Java for LeetCode 061 Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...
- hdu 1233 还是畅通工程 解题报告
题目链接:http://code.hdu.edu.cn/showproblem.php?pid=1233 并查集的运用, 实质就是求最小生成树.先对所有的村庄距离从小到大排序,然后判断村庄之间是否属于 ...
- String解析
常量池(Constant Pool):指的是在编译期被确定,并被保存在已编译的.class文件中的一些数据.JVM虚拟机为每个被装载的类型维护一个常量池.常量池就是该类型所用到常量的一个有序集和,包括 ...
- Balance(poj 1837)
题意:一个天平上有C个挂钩,第i个挂钩的位置为C[i],C[i] < 0表示该挂钩在原点的左边,C[i] > 0表示该挂钩在原点的右边:然后给出G个钩码的重量,问有多少种挂法使得天平保持平 ...
- C语言,输入一个正整数,按由大到小的顺序输出它的所有质数的因子(如180=5*3*3*2*2)
#include <iostream> using namespace std; int main() { long num; while(cin >> num){ ){ co ...
- Primace 5.0软件与KEIL单片机软件联合在线仿真步骤
Primace 软件是CME(京微雅格)公司的FPGA芯片开发专用软件.因为CME的FPGA,如M5.M7等内嵌有8051核,所以可以和MCU联合在线仿真,虽然FPGA内的程序不可控,不能一步一步的仿 ...
- 配置redis外网可访问
redis采用的安全策略,默认会只准许本地访问 通过简单配置,完成允许外网访问 [root@cache01 conf]# egrep "(^bind|#bind|# bind)" ...
- CentOS 6.5 下安装 Kibana5
1. 导入Elastic PGP Key 执行命令 rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch 2. 安装Kiban ...
- .net学习之多线程、线程死锁、线程通信 生产者消费者模式、委托的简单使用、GDI(图形设计接口)常用的方法
1.多线程简单使用(1)进程是不执行代码的,执行代码的是线程,一个进程默认有一个线程(2)线程默认情况下都是前台线程,要所有的前台线程退出以后程序才会退出,进程里默认的线程我们叫做主线程或者叫做UI线 ...