OpenJ_Bailian - 3421

Time Limit: 1000MSMemory Limit: 65536KB64bit IO Format: %I64d & %I64u

Submit Status

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

2. Each text character will be represented by decimal values as follows: 
= 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值