Google Round B China New Grad Test 2014

来源:互联网 发布:知乎 智能晾衣架 编辑:程序博客网 时间:2024/05/01 08:15


比赛结果及solution可从该网址下载:https://code.google.com/codejam/contest/2929486/scoreboard

1.    Sudoku Checker

Sudoku is a popular single playergame. The objective is to fill a 9x9 matrix with digits so that each column,each row, and all 9 non-overlapping 3x3 sub-matrices contain all of the digitsfrom 1 through 9. Each 9x9 matrix is partially completed at the start of gameplay and typically has a unique solution.

Given a completed N2xN2 Sudokumatrix, your task is to determine whether it is a validsolution. A valid solutionmust satisfy the following criteria:

l  Each row contains each number from 1 to N2,once each.

l  Each column contains each number from 1 to N2,once each.

l  Divide the N2xN2 matrixinto N2 non-overlapping NxN sub-matrices.Each sub-matrix contains each number from 1 to N2,once each.

You don't need to worry about the uniqueness of the problem. Justcheck if the given matrix is a valid solution.

Input

The first line of the input gives the number of test cases, TT testcases follow. Each test case starts with an integer N. The next N2 linesdescribe a completed Sudoku solution, with each line contains exactly N2 integers.All input integers are positive and less than 1000.

Output

For each test case, output one line containing "Case #x:y", where x is the case number (starting from 1) and y is "Yes"(quotes for clarity only) if it is a valid solution, or "No" (quotesfor clarity only) if it is invalid. Note that the judge is case-sensitive, soanswers of "yes" and "no" will not be accepted.

Limits

1 ≤ T ≤ 100.

Small dataset

N = 3.

Large dataset

3 ≤ N ≤ 6.

Sample


Input 
 


Output 
 

3

3

5 3 4 6 7 8 9 1 2

6 7 2 1 9 5 3 4 8

1 9 8 3 4 2 5 6 7

8 5 9 7 6 1 4 2 3

4 2 6 8 5 3 7 9 1

7 1 3 9 2 4 8 5 6

9 6 1 5 3 7 2 8 4

2 8 7 4 1 9 6 3 5

3 4 5 2 8 6 1 7 9

3

1 2 3 4 5 6 7 8 9

1 2 3 4 5 6 7 8 9

1 2 3 4 5 6 7 8 9

1 2 3 4 5 6 7 8 9

1 2 3 4 5 6 7 8 9

1 2 3 4 5 6 7 8 9

1 2 3 4 5 6 7 8 9

1 2 3 4 5 6 7 8 9

1 2 3 4 5 6 7 8 9

3

5 3 4 6 7 8 9 1 2

6 7 2 1 9 5 3 4 8

1 9 8 3 4 2 5 6 7

8 5 9 7 6 1 4 2 3

4 2 6 8 999 3 7 9 1

7 1 3 9 2 4 8 5 6

9 6 1 5 3 7 2 8 4

2 8 7 4 1 9 6 3 5

3 4 5 2 8 6 1 7 9

Case #1: Yes

Case #2: No

Case #3: No


2.    Meet and party

Little Sin lives in a Manhattan-grid city, a 2D plane where peoplecan only go north, west, south or east along the grid. The distance from (x1,y1) to (x2, y2) is |x1 - x2| + |y1 - y2|.

Little Sin really likes to party and is hoping to host a house partyin Manhattan this Sunday. Little Sin has collected a list of people who willattend, and now needs to decide at whose home she will host the party.

Little Sin invited all of the people in several rectangular areas,and all of those people have said yes. A rectangular area is denoted as (x1,y1, x2, y2), where x1 ≤ x2, y1 ≤ y2. People who live in a rectangular area fill all integral points inside it. So there are atotal of (x2 - x1 + 1) * (y2 - y1 + 1) people in the rectangular area (x1, y1,x2, y2).

Little Sin knows the coordinates of those rectangular areas. Shewants the party to be hosted at the home of one of the people who is attending,but she also doesn't want everyone else to have to travel very far: she wantsto minimize the sum of all distances from allattendees' houses to the party. Can you help her?

Input

The first line of the input gives the number of test cases, TT testcases follow. Each test case starts with a line containing a single integer:the number of rectangular areas, BBlines follow. Eachline contains 4 integers: x1, y1, x2, y2, denoting the coordinates of arectangular area of people Little Sin has invited to her party.

Output

For each test case, output one line containing "Case #t: x yd", where t is the case number (starting from 1) and (x, y) is thecoordinates of the person whose home the party should be hosted. If there aremultiple positions with the same minimum total distance, choose the one withthe smallest x. If there are still multiple positions, choose the one with thesmallest y. The value d is the sum of the distances from all attendees' housesto the point (x, y).

Limits

1 ≤ T ≤ 10.

|x1|, |y1|, |x2|, |y2| ≤ 109.

x1 ≤ x2, y1 ≤ y2.

The rectangular areas within a test casedon't intersect.

Smalldataset

1 ≤ B ≤ 100.

1 ≤ Total number of people in eachtest case ≤ 1000.

Largedataset

1 ≤ B ≤ 1000.

1 ≤ Total number of people in eachtest case ≤ 1000000.

Sample


Input 
 


Output 
 

2

1

0 0 2 2

3

-1 2 -1 2

0 0 0 0

1 3 1 3

Case #1: 1 1 12

Case #2: -1 2 6


3.   Hex

This problem was inspired by a board gamecalled Hex, designed independently by Piet Hein and John Nash. It has a similaridea, but does not assume you have played Hex.

    This game is played on an NxN board, where each cell is a hexagon. There are two players: Red side(using red stones) and Blue side (using blue stones). The board starts empty,and the two players take turns placing a stone of their color on a single cellwithin the overall playing board. Each player can place their stone on any cellnot occupied by another stone of any color. There is no requirement that astone must be placed beside another stone of the same color. The player tostart first is determined randomly (with equal probability among the twoplayers).
    The upper side and lower sides of theboard are marked as red, and the other two sides are marked as blue. The goalof the game is to form a connected path of one player's stones connecting thetwo sides of the board that have that player's color. The first player toachieve this wins. Note that the four corners are considered connected to bothcolors.
    The game ends immediately when oneplayer wins.
    Given a game state, help someone newto the game determine the status of a game board. Say one of the following:

"Impossible": If it was impossible for two playersto follow the rules and to have arrived at that game state.

"Red wins": If the player playing the red stoneshas won.

"Blue wins": If the player playing the blue stoneshas won.

"Nobody wins": If nobody has yet won the game. Notethat a game of Hex can't end without a winner!

Note that in any impossible state, the onlycorrect answer is "Impossible", even if red or blue has formed aconnected path of stones linking the opposing sides of the board marked by hisor her colors. 
    Here's a an example game on a 6x6 gameboardwhere blue won. Blue was the first player to move, and placed a blue stone atcell marked as 1. Then Red placed at cell 2, then blue at cell 3, etc. Afterthe 11th stone is placed, blue wins.

Input

The first line of input gives the number oftest cases, TT test cases follow. Each testcase start with the size of the side of the board, N. This is followed by a board of N rows and N columns consisting of only'B', 'R' and '.' characters. 'B' indicates a cell occupied by blue stone, 'R'indicates a cell occupied by red stone, and '.' indicates an empty cell.

Output

For each test case, output one linecontaining "Case #x: y", where x is the case number (starting from 1)and y is the status of the game board. It can be "Impossible","Blue wins", "Red wins" or "Nobody wins"(excluding the quotes). Note that the judge is case-sensitive, so answers of"impossible", "blue wins", "red wins" and"nobody wins" will be judged incorrect.

Limits

1 ≤ T ≤ 100.

Smalldataset

1 ≤ N ≤ 10.

Largedataset

1 ≤ N ≤ 100.

Sample


Input 
 


Output 
 

7
1
.
1
B
1
R
2
BR
BB
4
BBBB
BBB.
RRR.
RRRR
4
BBBB
BBBB
RRR.
RRRR
6
......
..R...
BBBBBB
..R.R.
..RR..
......

Case #1: Nobody wins
Case #2: Blue wins
Case #3: Red wins
Case #4: Impossible
Case #5: Blue wins
Case #6: Impossible
Case #7: Blue wins


4.   Dragon Maze

You are the prince of Dragon Kingdom andyour kingdom is in danger of running out of power. You must find power to saveyour kingdom and its people. An old legend states that power comes from a placeknown as Dragon Maze. Dragon Maze appears randomly out of nowhere withoutnotice and suddenly disappears without warning. You know where Dragon Maze isnow, so it is important you retrieve some power before it disappears.

Dragon Maze is a rectangular maze, an N x M grid of cells. The top leftcorner cell of the maze is (0,0) and the bottom right corner is (N-1, M-1). Each cell making up the maze can be either a dangerous placewhich you never escape after entering, or a safe place that contains a certainamount of power. The power in a safe cell is automatically gathered once youenter that cell, and can only be gathered once. Starting from a cell, you canwalk up/down/left/right to adjacent cells with a single step.

Now you know where the entrance and exitcells are, that they are different, and that they are both safe cells. In orderto get out of Dragon Maze before it disappears, you must walk from the entrancecell to the exit cell taking as few steps as possible. If there are multiplechoices for the path you could take, you must choose the one on which you collectas much power as possible in order to save your kingdom.

Input

The first line of the input gives the numberof test cases, TT test cases follow.

Each test case starts with a line containingtwo integers N and M, which give the size of Dragon Maze asdescribed above. The second line of each test case contains four integers enxenyexxexy, describing the positionof entrance cell (enx, eny) and exit cell (exx, exy). Then N lines follow and each line has M numbers, separated by spaces,describing the N x M cells of Dragon Maze from top to bottom. Each number for a cell iseither -1, which indicates a cell is dangerous, or a positive integer, whichindicates a safe cell containing a certain amount of power.

Output

For each test case, output one linecontaining "Case #x: y", where x is the case number (starting from1). If it's possible for you to walk from the entrance to the exit, y should bethe maximum total amount of power you can collect by taking the fewest stepspossible. If you cannot walk from the entrance to the exit, y should be thestring "Mission Impossible." (quotes for clarity). Please note thatthe judge requires an exact match, so any other output like "missionimpossible." or "Mission Impossible" (which is missing thetrailing period) will be judged incorrect.

Limits

The amount of power contained in each cellwill not exceed 10,000.
1 ≤ T ≤ 30.
0 ≤ enxexx < N.
0 ≤ enyexy < M.

Smalldataset

1 ≤ NM ≤ 10.

Largedataset

1 ≤ NM ≤ 100.

Sample


Input 
 


Output 
 

2
2 3
0 2 1 0
2 -1 5
3 -1 6
4 4
0 2 3 2
-1 1 1 2
1 1 1 1
2 -1 -1 1
1 1 1 1

Case #1: Mission Impossible.
Case #2: 7
 


5.   Ignore all mycomments

Good programmers write fabulous comments.Igor is a programmer and he likes the old C-style comments in /* ... */ blocks. For him, it would beideal if he could use this style as a uniform comment format for allprogramming languages or even documents, for example Python, Haskell orHTML/XML documents.

Making this happen doesn't seem toodifficult to Igor. What he will need is a comment pre-processor that removesall the comment blocks in /*, followed by comment text, and by another */. Then the processed text can be handed over to thecompiler/document renderer to which it belongs—whatever it is.

Igor's pre-processor isn't quite thatsimple, though. Here are some cool things it does:

·  The comments the pre-processor reads can benested the same way brackets are nested in most programming languages. It'spossible to have comments inside comments. For example, the following codeblock has an outer level of comments that should be removed by the commentpre-processor. The block contains two inner comments.

printf("Hello /* a comment /* a comment inside comment */ 
        inside /* another comment inside comment */ 
        string */ world");

After the pre-process step, it becomes:

printf("Hello  world");

·  Igor recognizes comments can appear anywherein the text, including inside a string"/*...*/", a constant number 12/*...*/34 or even in a character escape \/*...*/n

Or more formally:

text:
  text-piece
  text-piece remaining-text
text-piece:
  char-sequence-without-/*
  empty-string
remaining-text:
  comment-block text
 
comment-block:
  /* comment-content */
comment-content:
  comment-piece
  comment-piece remaining-comment
comment-piece:
  char-sequence-without-/*-or-*/
  empty-string
remaining-comment:
  comment-block comment-content
 
char:
  letters
  digits
  punctuations
  whitespaces

Our pre-processor, given a text, removes all comment-block instances as specified.

Notes

·  Igor only needs to remove the comment in onepass. He doesn't remove additional comment blocks created as a result of theremoval of any comment block. For example:

//*no recursion*/* file header */

should generate:

/* file header */

·  The * character in any /* or /*cannotbe re-used in another /* or */. For example the following does NOT forma proper comment block

/*/

Input

A text document with comment blocks in /* and */. The input file is valid. It follows the specification of text in the problem statement. The input file always terminates with anewline symbol.

Output

We only have one test case for this problem. First we need to outputthe following line.

Case #1:

Then, print the document with all commentsremoved, in the way specified in the problem statements. Don't remove anyspaces or empty lines outside comments.

Limits

The input program contains only:

·  Letters: a\u2013z,A\u2013Z,

·  Digits: 0\u20139

·  Punctuation: ~ ! @ # % ^& * ( ) - + = : ; " ' < > , . ? | / \ { } [ ] _

·  Whitespace characters: space, newline

Small dataset

The small input contains a program of lessthan 2k bytes.

Large dataset

The large input contains a program of lessthan 100k bytes.

Sample


Input 
 

//*no recursion*/* file header
***********/************
* Sample input program *
**********/*************
*/
int spawn_workers(int worker_count) {
  /* The block below is supposed to spawn 100 workers.
     But it creates many more.
     Commented until I figure out why.
  for (int i = 0; i < worker_count; ++i) {
    if(!fork()) {
      /* This is the worker. Start working. */
      do_work();
    }
  }
  */
  return 0; /* successfully spawned 100 workers */
}
 
int main() {
  printf("Hello /*a comment inside string*/ world");
  int worker_count = 0/*octal number*/144;
  if (spawn_workers(worker_count) != 0) {
    exit(-1);
  }
  return 0;
}
 

 


Output 

 

Case #1:
/* file header
************************
*/
int spawn_workers(int worker_count) {
 
  return 0;
}
 
int main() {
  printf("Hello  world");
  int worker_count = 0144;
  if (spawn_workers(worker_count) != 0) {
    exit(-1);
  }
  return 0;
}