螺旋填数问题,重点复习!题目有坑,空格是00000--B - Encoding

https://vjudge.net/problem/OpenJ_Bailian-3421

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

AC代码:

#include<cstdio>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<cmath>
#pragma warning(disable:4996)
using namespace std;
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
const int maxn = 200 + 10;
const int INF = 0x7fffffff;
string tranfer(char c) {//将一个字母转化成5位的二进制字符串
	if (c == ' ')
		return "00000";
	string res = "";
	stack<int> st;
	int a = c - 'A' + 1;
	while (a != 0) {
		st.push(a % 2);
		a /= 2;
	}
	while (!st.empty()) {
		if (st.top() == 1)
			res += '1';
		else
			res += '0';
		st.pop();
	}
	while (res.size() < 5)
		res = "0" + res;
	return res;
}
void draw(int r, int c, string s) {
	int total = r * c;
	char ans[maxn][maxn];
	int circle = r / 2 + r % 2, index = 0;
	for (int i = 0; i < circle; i++) {//共转circle圈,每圈四个方向
		for (int j = i; index < s.size() && j < c - i; j++)//从左到右
			ans[i][j] = s[index++];
		for (int j = i + 1; index < s.size() && j < r - i - 1; j++)//从上到下
			ans[j][c - i - 1] = s[index++];
		for (int j = c - i - 1; index < s.size() && j >= i; j--)//从右到左
			ans[r - i - 1][j] = s[index++];
		for (int j = r - i - 2; index<s.size() && j > i; j--)//从下到上
			ans[j][i] = s[index++];
	}
	for (int i = 0; i < r; i++)
		for (int j = 0; j < c; j++)
			printf("%c", ans[i][j]);
	printf("\n");
}
int main() {
	int r, c;
	while (scanf("%d %d ", &r, &c) != EOF) {
		string s, tmp = "";
		int total = r * c;
		getline(cin, s);
		for (int i = 0; i < s.size(); i++) {
			tmp += tranfer(s[i]);
		}
		while (tmp.size() < total) {//如果当前二进制串不够行*列,则补0
			tmp += "0";
		}
		draw(r, c, tmp);
		//cout << tmp << endl;
	}
	return 0;
}
/*
*/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值