Time Limit: 1000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
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
= 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
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 . The length of the text string is guaranteed to be ≤ (R*C)/5.
Output
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 row major order. You may have to fill out the matrix with zeroes (0) to complete the matrix.
Sample Input
4 4 ACM
Sample Output
0000110100101100
Source
Greater NY 2007
输入的地方看仔细了 有空格!空格处理成5个0
其他的地方就是4个循环,每个方向走一走就好。
#include<cstdio>
#include<cstring>
using namespace std;
int r,c;
int a[30][30];
char s[800];
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
bool vis[30][30];
int an[10000];
int anlen;
void dfs(int x,int y,int dir)
{
int xx,yy;
xx=x+dx[dir];
yy=y+dy[dir];
// if(xx<0||xx>=r||y<||y>=c)
// {
//
//
// }
}
int dij[30][30];
int main()
{
for(int i=1;i<=26;i++)
{
int tmp[10];
int len=0;
int x=i;
while(x)
{
tmp[len++]=x%2;
// printf("%d",tmp[len+1]);
x/=2;
}
while(len<5)
tmp[len++]=0;
for(int j=0;j<5;j++)
{
dij[i][j]=tmp[--len];
}
// printf("i:%d\n",i);
// for(int j=0;j<5;j++)
// {
// printf("%d",dij[i][j]);
// }
// for(;len>=0;len--)
// tmp[len]=0;
// printf("i:%d ",i);
//for(int j=0;i<5;j++)
// printf("%d",tmp[j]);
// puts("");
}
while(scanf("%d%d",&r,&c)!=EOF)
{
getchar();
memset(vis,0,sizeof vis);
gets(s);
//scanf("%s",s);
int len=0;
len=strlen(s);
anlen=0;
for(int i=0;i<len;i++)
{
for(int j=0;j<5;j++)
{if(s[i]<='Z'&&s[i]>='A')
an[anlen++]=dij[s[i]-'A'+1][j];
else
an[anlen++]=0;
// printf("%d",an[anlen-1]);
}
}
// puts("");
// for(int i=0;i<anlen;i++)
// {
//
// printf("%d",an[i]);
// }
memset(a,0,sizeof a);
int num=len*5;
int x,y;
x=0;
y=0;
int now=1;
vis[0][0]=1;
a[0][0]=an[0];
while(now<anlen)
{
while(y<c-1&&!vis[x][y+1])
{ y++;
vis[x][y]=1;
a[x][y]=an[now];
now++; if(now>=anlen)
break;
}
while(x<r-1&&!vis[x+1][y])
{ x++;
vis[x][y]=1;
a[x][y]=an[now];
now++;if(now>=anlen)
break;
}
while(y>0&&!vis[x][y-1])
{ y--;
vis[x][y]=1;
a[x][y]=an[now];
now++;
if(now==anlen)
break;
}
while(x>0&&!vis[x-1][y])
{
x--;
vis[x][y]=1;
a[x][y]=an[now];
now++;
if(now==anlen)
break;
}
}
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
printf("%d",a[i][j]);
}
puts("");
}
return 0;
}
#include<cstring>
using namespace std;
int r,c;
int a[30][30];
char s[800];
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
bool vis[30][30];
int an[10000];
int anlen;
void dfs(int x,int y,int dir)
{
int xx,yy;
xx=x+dx[dir];
yy=y+dy[dir];
// if(xx<0||xx>=r||y<||y>=c)
// {
//
//
// }
}
int dij[30][30];
int main()
{
for(int i=1;i<=26;i++)
{
int tmp[10];
int len=0;
int x=i;
while(x)
{
tmp[len++]=x%2;
// printf("%d",tmp[len+1]);
x/=2;
}
while(len<5)
tmp[len++]=0;
for(int j=0;j<5;j++)
{
dij[i][j]=tmp[--len];
}
// printf("i:%d\n",i);
// for(int j=0;j<5;j++)
// {
// printf("%d",dij[i][j]);
// }
// for(;len>=0;len--)
// tmp[len]=0;
// printf("i:%d ",i);
//for(int j=0;i<5;j++)
// printf("%d",tmp[j]);
// puts("");
}
while(scanf("%d%d",&r,&c)!=EOF)
{
getchar();
memset(vis,0,sizeof vis);
gets(s);
//scanf("%s",s);
int len=0;
len=strlen(s);
anlen=0;
for(int i=0;i<len;i++)
{
for(int j=0;j<5;j++)
{if(s[i]<='Z'&&s[i]>='A')
an[anlen++]=dij[s[i]-'A'+1][j];
else
an[anlen++]=0;
// printf("%d",an[anlen-1]);
}
}
// puts("");
// for(int i=0;i<anlen;i++)
// {
//
// printf("%d",an[i]);
// }
memset(a,0,sizeof a);
int num=len*5;
int x,y;
x=0;
y=0;
int now=1;
vis[0][0]=1;
a[0][0]=an[0];
while(now<anlen)
{
while(y<c-1&&!vis[x][y+1])
{ y++;
vis[x][y]=1;
a[x][y]=an[now];
now++; if(now>=anlen)
break;
}
while(x<r-1&&!vis[x+1][y])
{ x++;
vis[x][y]=1;
a[x][y]=an[now];
now++;if(now>=anlen)
break;
}
while(y>0&&!vis[x][y-1])
{ y--;
vis[x][y]=1;
a[x][y]=an[now];
now++;
if(now==anlen)
break;
}
while(x>0&&!vis[x-1][y])
{
x--;
vis[x][y]=1;
a[x][y]=an[now];
now++;
if(now==anlen)
break;
}
}
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
printf("%d",a[i][j]);
}
puts("");
}
return 0;
}