HDU 1986 - Encoding

28 篇文章 0 订阅
Problem Description
Chip and Dale have devised an encryption method to hide their (written) text messages. They first agree secretly on two numbers that will be used as the number of rows (R) and columns (C) in a matrix. The sender encodes an intermediate format using the following rules:
1. The text is formed with uppercase letters [A-Z] and <space>.
2. Each text character will be represented by decimal values as follows:
<space> = 0, A = 1, B = 2, C = 3, ..., Y = 25, Z = 26
The sender enters the 5 digit binary representation of the characters’ values in a spiral pattern along the matrix as shown below. The matrix is padded out with zeroes (0) to fill the matrix completely. For example, if the text to encode is: "ACM" and R=4 and C=4, the matrix would be filled in as follows:



The bits in the matrix are then concatenated together in row major order and sent to the receiver. The example above would be encoded as: 0000110100101100

Input
The first line of input contains a single integer N, (1 ≤ N ≤ 1000) which is the number of datasets that follow.
Each dataset consists of a single line of input containing R (1<=R<=20), a space, C (1<=C<=20), a space, and a text string consisting of uppercase letters [A-Z] and <space>. The length of the text string is guaranteed to be <= (R*C)/5.

Output
For each dataset, you should generate one line of output with the following values: The dataset number as a decimal integer (start counting at one), a space, and a string of binary digits (R*C) long describing the encoded text. The binary string represents the values used to fill in the matrix in rowmajor
order. You may have to fill out the matrix with zeroes (0) to complete the matrix.

Sample Input
  
  
4 4 4 ACM 5 2 HI 2 6 HI 5 5 HI HO
 
Sample Output
  
  
1 0000110100101100 2 0110000010 3 010000001001 4 0100001000011010110000010
 
题意:用二进制表示出所有的字母,然后给出一串字母和 n*n 的大小,用蛇形填数来将这些字母的二进制填充这个地图,没有填到的地方用0来填,然后将地图输出。
第一行 T 数据组数,然后给出地图的长和宽,字母。
#include <cstdio>
#include <cstring>

const char alphe[27][6]={{"00000"},{"00001"},{"00010"},{"00011"},{"00100"},{"00101"},{"00110"},{"00111"},{"01000"},{"01001"},{"01010"},{"01011"},{"01100"},{"01101"},{"01110"},{"01111"},{"10000"},{"10001"},{"10010"},{"10011"},{"10100"},{"10101"},{"10110"},{"10111"},{"11000"},{"11001"},{"11010"}};
int maps[30][30];
int num[1000];
char str[100];

int main()
{
    int n;
    scanf("%d", &n);
    for (int icase = 1; icase <= n; ++icase)
    {
        memset(num, 0, sizeof(num));
        for (int i = 0; i < 30; ++i)
            for (int j = 0; j < 30; ++j)
                maps[i][j] = -1;
        int row, col;
        scanf("%d%d", &row, &col);
        getchar();

        gets(str);
        int cur = 0;
        for (int i = 0; str[i] != '\0'; ++i)
        {
            int sym = 0;
            if (str[i] <= 'Z' && str[i] >= 'A')
                sym = str[i] - 'A' + 1;
            for (int j = 0; j < 5; ++j)
                num[++cur] = alphe[sym][j] - '0';
        }
        int tot = 0;
        int x = 0;
        int y = 0;
        maps[0][0] = num[++tot];
        while (tot < cur)
        {
            while (y+1 < col && maps[x][y+1] == -1)
                maps[x][++y] = num[++tot];

            while (x+1 < row && maps[x+1][y] == -1)
                maps[++x][y] = num[++tot];

            while (y-1 >= 0 && maps[x][y-1] == -1)
                maps[x][--y] = num[++tot];

            while (x-1 >= 0 && maps[x-1][y] == -1)
                maps[--x][y] = num[++tot];
        }
        printf("%d ", icase);
        for (int i = 0; i < row; ++i)
        {
            for (int j = 0; j < col; ++j)
            {
                if (maps[i][j] == -1)
                    printf("0");
                else
                    printf("%d", maps[i][j]);
            }
        }
        printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值