纵横字谜的答案 (UVa232)

本文介绍了一种纵横字谜解析算法的实现细节。该算法能够处理由字母和标记组成的网格,通过判断起始格并进行编号,最终输出横向和纵向的所有有效单词。适用于解决特定格式的纵横字谜游戏。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

纵横字谜的答案

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Submit Status

Description

 

 Crossword Answers 

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

 

 

题意:输入一个r行c列的网格,黑格用*号表示,每个白格都填有一个字母。如果一个白格的左边相邻位置或者上边相邻位置没有白格(可能是黑格,也可能出了网格边界),则称这个白格是一个起始格。首先把所有起始格从左到右,从上到下顺序编号1,2,3,。。。。要求找出所有横向单词。这些单词必须从一个起始格开始,向右延伸到一个黑格的左边或者整个网格的最右边。最后找出所有的竖向单词。

 

实现代码

#include <stdio.h>
#include <ctype.h>
#include <memory.h>

#define MAX 7 
char map[MAX][MAX];
int Across_begin[MAX][MAX];
int Down_begin[MAX][MAX];

int main()
{
	int r,c, count = 0;
	scanf("%d%d", &r, &c);
	getchar();
	
	memset(map, 0, sizeof(map));
	memset(Across_begin, 0, sizeof(Across_begin));
	memset(Down_begin, 0, sizeof(Down_begin));

	for(int i = 0; i < r; ++i)
	{
		for(int j = 0; j < c; ++j)
		{
			int ch;
			while(!isalpha(ch = getchar()) &&  ch != '*');
		
			map[i][j] = ch;
			if(ch != '*')
			{
				if(j - 1 < 0 || map[i][j-1] == '*')
				{
					Across_begin[i][j] = ++count;

				}
				else if(i-1 < 0 || map[i-1][j] == '*')
				{
					Down_begin[i][j] = ++count;
				}
			}	
		}
	}

	printf("\npuzzle #1:\nArcross\n");
	// Across
	for(int i = 0; i < r; ++i)
	{
		for(int j = 0; j < c; ++j)
		{
			if(Across_begin[i][j] != 0)
			{
				printf("%d.", Across_begin[i][j]);
				int i_temp = i, j_temp = j;

				while(j_temp < c && map[i_temp][j_temp] != '*')
				{
					printf("%c", map[i_temp][j_temp++]);
				}
				printf("\n");
			}
		}
	}

	printf("puzzle #2:\nDown\n");
	// Down
	for(int i = 0; i < r; ++i)
	{
		for(int j = 0; j < c; ++j)
		{
			if(Down_begin[i][j] != 0)
			{
				printf("%d.", Down_begin[i][j]);
				int i_temp = i, j_temp = j;

				while(i_temp < r && map[i_temp][j_temp] != '*')
				{
					printf("%c", map[i_temp++][j_temp]);
				}
				printf("\n");
			}
		}
	}

}

 

### C语言纵横字谜解决方案 #### 示例代码实现 为了创建一个简单的纵横字谜游戏,在C语言中可以定义如下结构体来表示网格: ```c #include <stdio.h> #include <string.h> #define GRID_SIZE 5 void initialize_grid(char grid[GRID_SIZE][GRID_SIZE]) { for (int i = 0; i < GRID_SIZE; ++i) { for (int j = 0; j < GRID_SIZE; ++j) { grid[i][j] = '_'; } } } void display_grid(const char grid[GRID_SIZE][GRID_SIZE]) { printf("Grid:\n"); for (int i = 0; i < GRID_SIZE; ++i) { for (int j = 0; j < GRID_SIZE; ++j) { printf("%c ", grid[i][j]); } printf("\n"); } } ``` 这段代码初始化了一个`5x5`大小的字符数组作为纵横字谜的游戏板,并提供了一种显示当前状态的方法。 #### 设计测试用例 对于上述功能,以下是几个可能的设计思路用于构建有效的测试案例[^1]: - **测试用例编号:** TC_Initialize_Grid_001 - **所属模块:** 初始化模块 - **用例类型:** 功能性测试 - **测试用例标题:** 验证网格是否被正确初始化为空白格子('_') - **关键词:** 初始值设置、空白填充 - **优先级:** P1 - **前置条件:** 程序已编译成功并运行正常 - **步骤:** 调用 `initialize_grid()` 函数后立即调用 `display_grid()`. - **预期结果:** 所有位置都应显示为下划线(_) 另一个例子可能是验证打印方法的行为: - **测试用例编号:** TC_Display_Grid_002 - **所属模块:** 显示模块 - **用例类型:** 输出验证 - **测试用例标题:** 检查输出格式是否符合预期 - **关键词:** 屏幕输出、格式化字符串 - **优先级:** P2 - **前置条件:** 已经有一个完全由'_'组成的网格实例存在 - **步骤:** 使用预设数据集调用 `display_grid()` 方法. - **预期结果:** 控制台应该显示出整齐排列成五行五列的'_',每行之间有一条新行分隔. 这些只是基础级别的单元测试;更复杂的场景会涉及到实际填入单词后的表现以及边界情况处理等更多方面[^2].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值