Sicily 1843. Pyramid

来源:互联网 发布:python 有序字典 编辑:程序博客网 时间:2024/06/14 18:55

1843. Pyramid

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB , Special Judge

Description

    In a classical computer game, a creature jumps around on the cells in a pyramid much like the one the picture below. The creature can jump to the cells directly above him or below him. Jumping outside the pyramid is of course not allowed.
    When the creature lands on a cell, it changes color: from red to green, green to blue or blue to red. Given how the cells are initially colored, determine a jumping sequence containing no more than 5000 jumps that turns all the cells into blue. You are allowed to start from any cell in the pyramid you want(this guarantees that there will always be a solution). The first cell to change color is the one reached after jumping from the start cell.

Input

    The input consists of several test cases. The first line is a positive number, N, the number of test cases. Each test case begins with a line containing a single integer n(2<=n<=40), the height of the pyramid. The next n lines describe the start configuration of the pyramid using the upper case letters ‘R’,’G’ and ‘B’.
    The pyramid is described in row major order, each row from left to right; see the sample input for the exact format(the first sample input corresponds to the pyramid in the picture). At least one cell in the pyramid is not blue in the start configuration.

Output

     For each test case, output two lines. The first line should contain two integers, the creatures start position in the pyramid. The first integer is the row(1 being the top row) and the second integer is the cell in that row(1 being the leftmost cell). The next line should contain a string describing the jumps. The length of this string should be at most 5000 characters. Each character should either be a ‘7’(jumping up-left),a ‘9’(up-right), a ‘1’(down-left) or a ‘3’(down-right).
    Any solution that has no more than 5000 jumps and which turns all cells into blue will be accepted.

Sample Input

24BRGBGRGBRB2RGB

Sample Output

3 11939191939193737377171919919193737372 1

919

// Problem#: 1843// Submission#: 3590299// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <stdio.h>int cs, n, l;char ans[5001];int a[50][50];int main() {    char s[50];    int i, j, x, y;    scanf("%d", &cs);    while (cs--) {        scanf("%d", &n);        for (i = 1; i <= n; i++) {            scanf("%s", &s);            for (j = 1; j <= i; j++)                switch(s[j - 1]) {                case 'R': a[i][j] = 0; break;                case 'G': a[i][j] = 1; break;                case 'B': a[i][j] = 2;            }        }        x = y = 1;        l = 0;        for (i = 1; i < n; i++) {            while (x < n) {                x++;                a[x][y] = (a[x][y] + 1) % 3;                ans[l++] = '1';            }            while (x > i) {                while (a[x][y] != 2) {                    a[x - 1][y] = (a[x - 1][y] + 1) % 3;                    ans[l++] = '9';                    a[x][y] = (a[x][y] + 1) % 3;                    ans[l++] = '1';                }                x--;                a[x][y] = (a[x][y] + 1) % 3;                ans[l++] = '9';            }            x++;            y++;            a[x][y] = (a[x][y] + 1) % 3;            ans[l++] = '3';        }        while (x != 1 || y != 1) {            while (a[x][y] != 2) {                a[x - 1][y - 1] = (a[x - 1][y - 1] + 1) % 3;                ans[l++] = '7';                a[x][y] = (a[x][y] + 1) % 3;                ans[l++] = '3';            }            x--;            y--;            a[x][y] = (a[x][y] + 1) % 3;            ans[l++] = '7';        }        switch(a[x][y]) {        case 0:            ans[l - 1] = 0;            printf("1 1\n");            printf("%s\n", ans);            break;        case 1:            ans[l] = 0;            printf("2 1\n");            printf("9%s\n", ans);            break;        case 2:            ans[l] = 0;            printf("1 1\n");            printf("%s\n", ans);        }    }    return 0;}                                 


0 0
原创粉丝点击