UVa 232 Crossword Answer(纵横字谜的答案)

Description

Download as PDF

A crossword puzzle consists of a rectangular grid of black and white squares and two lists of definitions (or descriptions).

One list of definitions is for ``words" to be written left to right across white squares in the rows and the other list is for words to be written down white squares in the columns. (A word is a sequence of alphabetic characters.)

To solve a crossword puzzle, one writes the words corresponding to the definitions on the white squares of the grid.

The definitions correspond to the rectangular grid by means of sequential integers on ``eligible" white squares. White squares with black squares immediately to the left or above them are ``eligible." White squares with no squares either immediately to the left or above are also ``eligible." No other squares are numbered. All of the squares on the first row are numbered.

The numbering starts with 1 and continues consecutively across white squares of the first row, then across the eligible white squares of the second row, then across the eligible white squares of the third row and so on across all of the rest of the rows of the puzzle. The picture below illustrates a rectangular crossword puzzle grid with appropriate numbering.

An ``across" word for a definition is written on a sequence of white squares in a row starting on a numbered square that does not follow another white square in the same row.

The sequence of white squares for that word goes across the row of the numbered square, ending immediately before the next black square in the row or in the rightmost square of the row.

A ``down" word for a definition is written on a sequence of white squares in a column starting on a numbered square that does not follow another white square in the same column.

The sequence of white squares for that word goes down the column of the numbered square, ending immediately before the next black square in the column or in the bottom square of the column.

Every white square in a correctly solved puzzle contains a letter.

You must write a program that takes several solved crossword puzzles as input and outputs the lists of across and down words which constitute the solutions.

Input

Each puzzle solution in the input starts with a line containing two integers r and c ( tex2html_wrap_inline39 and tex2html_wrap_inline41 ), where r (the first number) is the number of rows in the puzzle and c (the second number) is the number of columns.

The r rows of input which follow each contain c characters (excluding the end-of-line) which describe the solution. Each of those ccharacters is an alphabetic character which is part of a word or the character ``*", which indicates a black square.

The end of input is indicated by a line consisting of the single number 0.

Output

Output for each puzzle consists of an identifier for the puzzle (puzzle #1:puzzle #2:, etc.) and the list of across words followed by the list of down words. Words in each list must be output one-per-line in increasing order of the number of their corresponding definitions.

The heading for the list of across words is ``Across". The heading for the list of down words is ``Down".

In the case where the lists are empty (all squares in the grid are black), the Across and Down headings should still appear.

Separate output for successive input puzzles by a blank line.

Sample Input

2 2
AT
*O
6 7
AIM*DEN
*ME*ONE
UPON*TO
SO*ERIN
*SA*OR*
IES*DEA
0

Sample Output

puzzle #1:
Across
  1.AT
  3.O
Down
  1.A
  2.TO

puzzle #2:
Across
  1.AIM
  4.DEN
  7.ME
  8.ONE
  9.UPON
 11.TO
 12.SO
 13.ERIN
 15.SA
 17.OR
 18.IES
 19.DEA
Down
  1.A
  2.IMPOSE
  3.MEO
  4.DO
  5.ENTIRE
  6.NEON
  9.US
 10.NE
 14.ROD
 16.AS
 18.I
 20.A
	
	

这题刚看到的时候觉得so easy,可是看似简单一开始竟然没有看出序号的规律,后来仔细研究了一下,发现带序号的方格有共同的特点就是要么左边是星号要么上边是星号要么在第0行要么在第0列,满足任意一种或几种情况就可以给这个方格分配一个序号了,从0行0列顺次编号即可。然后通过对序号的检索和对边界的判断来决定该序号对应的词是横向输出还是纵向输出,细节稍加注意即可。

代码实现上,开了两个等大的全局二维数组(开成全局方便函数对其直接操作),一个用来保存原始数据,一个用来保存给它们分配的编号,多组输入每次输入以后对两数组分别进行初始化为'*'和0,方便遍历数组时对条件的判断。然后编写一个函数,用来控制输出,因为横向和纵向的输出相似,所以用一个函数设定两个模式即可,节省代码空间,另外对条件的控制一定要仔细,两个数组的作用要把握好,稍微粗心就会导致整个程序没得看。。= =,输出格式依然是此题的坑点,同样是两组结果之间加一个空行,稍不留神就会WA,还有序号的输出是宽度为3的整数。注意到这些,AC就很简单了,另外提一下,这个代码交了两次竟然都是CE,我也是醉了。。。后来看编译器反馈才知道一开始将存储序号的数组命名成index[][],而cstring头文件里有index这么一个库函数,所以导致冲突报错(但是在别的oj上还有本机的编译器都没事,真心不知道UVa的规则啊。。T T),所以以后再也不敢用max啥的当变量名了。。。上代码:

#include <iostream>
#include <cstring>
#include <iomanip>

using namespace std;

char str[11][11];		//存储原始数据
int indexs[11][11];		//存储对应序号
void PrintPuzzlesWords(int x,int y,int mode);

int main()
{
	int r,c,i,j,k,mark=1;
	while(cin>>r,r)
	{
		cin>>c;
		memset(indexs,0,sizeof(indexs));	//处理成*和0为了方便下面对边界条件等的处理和判断
		memset(str,'*',sizeof(str));
		for(i=0,k=1;i<r;i++)
			for(j=0;j<c;j++)
			{
				cin>>str[i][j];		//因为输入是自上而下自左而右,所以完全可以边输入边判断,提高效率
				if(str[i][j]!='*'&&(i==0||j==0||str[i-1][j]=='*'||str[i][j-1]=='*'))
					indexs[i][j]=k++;
			}
		if(mark!=1)		//两组之间输出一个空行
			cout<<endl;
		cout<<"puzzle #"<<mark<<":"<<endl;
		cout<<"Across"<<endl;
		for(i=0;i<r;i++)
			for(j=0;j<c;j++)
				if(indexs[i][j]!=0&&(j==0||str[i][j-1]=='*'))	//第0列或左边有星号说明应该横向输出
					{
						cout<<setw(3)<<indexs[i][j]<<".";
						PrintPuzzlesWords(i,j,1);		//以给定坐标为起点横向输出
					}
		cout<<"Down"<<endl;
		for(i=0;i<r;i++)
			for(j=0;j<c;j++)
				if(indexs[i][j]!=0&&(i==0||str[i-1][j]=='*'))	//第0行或上面有星号说明应该纵向输出
				{
					cout<<setw(3)<<indexs[i][j]<<".";
					PrintPuzzlesWords(i,j,2);		//以给定坐标为起点纵向输出
				}
		mark++;
	}

	return 0;
}

void PrintPuzzlesWords(int x,int y,int mode)
{
	while(str[x][y]!='*')	//遇到*则输入结束
	{
		if(mode==1)		//模式1横向输出
			cout<<str[x][y++];
		else if(mode==2)	//模式2纵向输出
			cout<<str[x++][y];
	}
	cout<<endl;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值