比赛描述
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:
0→0→0→0
↓
1→1→0 1
↑ ↓ ↓
0 0←1 0
↑ ↓
1←1←0←0
A = 00001, C = 00011, M = 01101
(one extra 0)
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
输入
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 string of binary digits that represents the contents of the matrix (R * C binary digits).
The binary digits are in row major order.
输出
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 the decoded text message. You
should throw away any trailing spaces and/or partial characters found while decoding.
样例输入
4
4 4 0000110100101100
5 2 0110000010
2 6 010000001001
5 5 0100001000011010110000010
样例输出
1 ACM
2 HI
3 HI
4 HI HO
提示
undefined
题目来源
Greater New York Region 2007
#include<stdio.h> #define MAX_R 20 #define MAX_C 20 int main(){ int T,t,R,C,i,j,k,a,b,dirNo; char s[MAX_R][MAX_C]; char r[MAX_R*MAX_C+1]; char dir[4][2]={0,1,1,0,0,-1,-1,0}; scanf("%d",&T); for(t=1; t<=T; t++){ scanf("%d%d%s",&R,&C,r); k=0; for(i=0; i<R; i++){ for(j=0; j<C; j++){ s[i][j] = r[k++]-'0'; } } k=0; i=j=0; a=0; b=1; dirNo=0; while(k<R*C){ r[k++] = s[i][j]; s[i][j] = 2; if(i+a<0 || i+a==R || j+b<0 || j+b==C || s[i+a][j+b]==2){ dirNo = (dirNo+1)%4; a = dir[dirNo][0]; b = dir[dirNo][1]; } i += a; j += b; } printf("%d ",t); i=0; a = 0; while(i+5<=R*C){ k=0; for(j=0;j<5;j++){ k<<=1; k+=r[i+j]; } if(0==k){ r[a++] = ' '; } else{ r[a++] = k-1+'A'; } i+=5; } r[a]=0; while(' '==r[--a]){ //删除末尾的空格 r[a] = 0; } printf("%s\n",r); } }