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& ...
随机推荐
- SQL按照指定顺序对字段进行排序
SqlServer按照指定顺序对字段进行排序 问题如下,在SqlServer有一个这样的SQL查询 SELECT *FROM ProductWHERE ID IN ( 12490, 12494, 12 ...
- 3.子数组的最大和[MaximumContinuousSubArray]
[题目]: 输入一个整形数组,数组里有正数也有负数.数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和.求所有子数组的和的最大值,要求时间复杂度为O(n). 例如输入的数组为1, -2, ...
- spring无法扫描jar包的问题
在日常开发中往往会对公共的模块打包发布,然后调用公共包的内容.然而,最近对公司的公共模块进行整理发布后.spring却无法扫描到相应的bean.折腾了好久,最终发现是认识上的误区. 2015-11-1 ...
- Java for LeetCode 068 Text Justification
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- BaseColumns以及自定义Column
android provider 包下自带的BaseColumn /* * Copyright (C) 2006 The Android Open Source Project * * License ...
- 矩形覆盖(codevs 1101)
题目描述 Description 在平面上有 n 个点(n <= 50),每个点用一对整数坐标表示.例如:当 n=4 时,4个点的坐标分另为:p1(1,1),p2(2,2),p3(3,6),P4 ...
- ubuntu用户添加adduser, useradd
ubuntu和windows一样,可以任意创建或者删除新的用户,windows下比较简单,ubuntu下需要使用命令,不过操作起来不是很繁琐,所以我尽量写的详细一些. 如何创建ubuntu新用户? ...
- vsftp 一键安装包
http://pan.baidu.com/s/1mibAJC8
- Java创建Web项目
首先下载Tomcat服务,用来运行JAVA程序,跟windows中的IIS类似 下载地址:tomcat.apache.org ,最好下载ZIP压缩版的,解压后就可以直接用.如下图: 检查Tomcat是 ...
- Sonar+Hudson+Maven构建系列之二:迁移Sonar
摘要:由于昨天在一台机器上安装的东西太多了,导致Linux机器上非常卡,一台Linux负担了jira, fisheye, confluence, sonar, hudson, mysql 等等,本来已 ...