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:
- The text is formed with uppercase letters [A-Z] and <space>.
- 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 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
4
4 4 0000110100101100
5 2 0110000010
2 6 010000001001
5 5 0100001000011010110000010
Sample Output
1 ACM
2 HI
3 HI
4 HI HO
示例
对于这题,我觉得比Encoding简单。也就是直接用Encoding的向量搜索,再把五个二进制字符直接转化成了十进制数。这题要注意在输出时最后多余的空格在去掉,因此我用了一个数组保存了每个数,处理掉多余空格后再一起输出。
#include<iostream>
using namespace std;
int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int n,m,num[1003],l;
char map[21][21],str[1002],str1[1002];
void dfs()
{
int i=0,j=-1,num1=0,k=0,flag=0,w=0;
l=0;
while(num1<n*m)
{
i+=dir[k][0];
j+=dir[k][1];
if(map[i][j]=='#'||i<0||j<0||i>=n||j>=m)
{i-=dir[k][0];j-=dir[k][1];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;
}