HDU 1986 - Encoding

来源:互联网 发布:血族第四季 知乎 编辑:程序博客网 时间:2024/06/05 04:01
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
44 4 ACM5 2 HI2 6 HI5 5 HI HO
 
Sample Output
1 00001101001011002 01100000103 0100000010014 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;}


0 0
原创粉丝点击