TOJ 3007. Decoding

终于AC了!!!!!!!!!!!!一直是Presentation Error,知道是末尾不能有空格的原因,但是就是改不好,而且不能用while循环来判断,因为会超时,我的方法是这样的:

用一个变量fla代表出现的空格数,初始化为0;只要遇到是空格的情况先不要急着输出,而是累加fla,在下一个非空格的字符输出前输出,而且fla还累计空格个数。

我能够说我提交了20多遍都是错误的吗?

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 <space>.
2. 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 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

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

For each dataset, you should generate one line of output with the following values: The dataset number as a decimal integer (start counting at one), a space, and the decoded text message. You should throw away any trailing spaces and/or partial characters found while decoding.

Sample Input

4
4 4 0000110100101100
5 2 0110000010
2 6 010000001001
5 5 0100001000011010110000010

Sample Output

1 ACM
2 HI
3 HI
4 HI HO
AC代码:

<pre name="code" class="cpp">
<span style="font-size:10px;"></span><pre name="code" class="cpp">#include<iostream>
#include<string>
using namespace std;
int main(){
	int N,r,c,l,i,j,k,a[21][21],b[410],flag[21][21],m;
	char t;
	cin>>N;
	for(l=1;l<=N;l++){
		cin>>r>>c;
		for(i=0;i<r;i++)
			for(j=0;j<c;j++){
				cin>>t;
				a[i][j]=t-'0';
				flag[i][j]=0;
			}
		flag[0][0]=1;
		m=r*c;
		k=0;
		i=0;
		j=0;
		int f=1;//1,2,3,4分别代表向右,下,左,上
		while(true){
			b[k]=a[i][j];
			k++;
			if(k==m)
				break;
			if(f==1){
				if(j==c-1||flag[i][j+1]==1){
					f=2;
					i++;
				}
				else j++;
			}
			else if(f==2){
				if(i==r-1||flag[i+1][j]==1){
					f=3;
					j--;
				}
				else i++;
			}
			else if(f==3){
				if(j==0||flag[i][j-1]==1){
					f=4;
					i--;
				}
				else j--;
			}
			else if(f==4){
				if(i==0||flag[i-1][j]==1){
					f=1;
					j++;
				}
				else i--;
			}
			flag[i][j]=1;
		}
		cout<<l<<" ";
		int fla=0;
		for(i=0;i<=m-5;i=i+5){
			
			k=b[i];
			for(j=i+1;j<i+5;j++)
			{
				k=k*2;
				k=k+b[j];
			}
			if(k==0){
				fla++;
			}
			else{
				if(fla!=0){
					for(int kk=0;kk<fla;kk++)
					cout<<" ";
				fla=0;
				}
				char s=k+'A'-1;
				cout<<s;			
			}	
			
		}	
		cout<<endl;
	}
	return 0;
}


 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值