春季赛day15 Snailography
题目描述
Snails have many enemies including snakes, turtles, and birds. So, snails need to communicate their travel paths using cryptography to avoid their routes being detected.
The encryption technique: The message will contain only letters. Let’s assume the message length is L. We use the smallest odd integer N such that N×N ≥ L. Then, an N×N table is used to encrypt the message as follows:
Put the first letter of the message in the cell at the center of the table and then put the remaining letters in the table by moving in a circular way (snail like) around the center cell.
For example, the table to the right shows the order for placing the letters from the message in a 7×7 table. So, from the center cell, we move up, then move right, then move down, then move left, then move up, etc.
Below are some sample encryptions. To help with the illustrations, when encrypting the message, if there are more cells in the table than there are letters in the message, we put the character ‘#’ in the extra cells.
Message: ABCDEFGH
Encryption:
#BC
HAD
GFE
Message: ABCDEFGHIJKLMNOPQRSTUVW
Encryption:
#JKLM
#IBCN
WHADO
VGFEP
UTSRQ
Message: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv
Encryption:
#Zabcde
vYJKLMf
uXIBCNg
tWHADOh
sVGFEPi
rUTSRQj
qponmlk
Given the size of the two-dimensional table to use and the original message, you are to encrypt the message (to help snails live longer lives).输入
The first input line contains an odd integer, n (1 ≤ n ≤ 19), indicating the table size to use. The second input line will provide the message to encrypt, a string of 1-361 (19×19) letters (lowercase and uppercase). Assume that the message will fit in the table.
输出
Print the encrypted message on one output line using the row-major order, i.e., print Row 1 followed by Row 2, followed by Row 3, etc.
Remember to print a newline character after printing the last row.
The output should not include ‘#’ characters.样例输入 Copy
【样例1】 3 ABCDEFGH 【样例2】 5 ABCDEFGHIJKLMNOPQRSTUVW样例输出 Copy
【样例1】 BCHADGFE 【样例2】 JKLMIBCNWHADOVGFEPUTSRQ
填表的时候注意每一次操作填满的话下一次操作的开头不能直接从尾巴开始(不然会重复填一个格子)
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
char tab[21][21];
string s;
void fill(int x,int y,int start,int len){
if(start>=s.length())return;
int p=start;
for(int i=y;i<y+len-2;++i){tab[x][i]=s[p++];if(p==s.length())return;}
for(int i=x;i<x+len-1;++i){tab[i][y+len-2]=s[p++];if(p==s.length())return;}
for(int i=y+len-2;i>y-1;--i){tab[x+len-1][i]=s[p++];if(p==s.length())return;}
for(int i=x+len-1;i>=x;--i){tab[i][y-1]=s[p++];if(p==s.length())return;}
fill(x-1,y-1,p,len+2);
}
int main(){
cin>>n>>s;
for(int i=0;i<n;++i){
for(int j=0;j<n;++j)tab[i][j]='#';
}
tab[n/2][n/2]=s[0];
fill(n/2-1,n/2,1,3);
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
if(tab[i][j]!='#')cout<<tab[i][j];
}
}
return 0;
}