# Decoding

### 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: ## 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 string of binary digits that represents the contents of the matrix (R * C binary digits).
The binary digits are in row major order.

## Output

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

## Sample Input

``````4
4 4 0000110100101100
5 2 0110000010
2 6 010000001001
5 5 0100001000011010110000010
``````

## Sample Output

``````1 ACM
2 HI
3 HI
4 HI HO
``````

### 示例

``````#include<iostream>
using namespace std;
int dir={{0,1},{1,0},{0,-1},{-1,0}};
int n,m,num,l;
char map,str,str1;
void dfs()
{
int i=0,j=-1,num1=0,k=0,flag=0,w=0;
l=0;
while(num1<n*m)
{
i+=dir[k];
j+=dir[k];
if(map[i][j]=='#'||i<0||j<0||i>=n||j>=m)
{i-=dir[k];j-=dir[k];k++;k%=4;continue;}
num1++;
str1[w++]=map[i][j];
map[i][j]='#';
}
str1[w]='\0';flag=0;
for(i=0;str[i];i++)
if(i%5==0){num[++l]=str1[i]-'0';}
else num[l]=num[l]*2+str1[i]-'0';
}
int main()
{
int cas,q,i,j,k,bo;
scanf("%d",&cas);
for(q=1;q<=cas;q++)
{
k=l=0;memset(num,0,sizeof(num));
scanf("%d%d%s",&n,&m,str);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
map[i][j]=str[k++];
dfs();
printf("%d ",q);for(i=l;i>0;i--)
if(num[i]==0)l--;
else break;
for(i=1;i<=l;i++)
if(num[i]!=0)printf("%c",num[i]+'A'-1);
else printf(" ");
printf("\n");
}
return 0;
}
``````
# acm