小紫书 习题 3-6(UVA 232) 纵横字谜的答案(Crossword Answers)

写得很矬,写across一种写法,写down一种写法,down那个是后来改的,没注意到是按数字顺序排的,所以感觉down的那种方法适用性更广。

最后UVA居然会报PE,傻逼地没有看到每个字母的数字编号是怎么样的,以为是在前面加两个空格,然而只是一个%3d就ok的事情。


很矬的AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int main()
{
	
	int cse = 1;
	while (1)
	{
		int r = -1, c = -1, cnt = 1;
		char grid[10][11];
		int cntpos[10][11];
		memset(cntpos, 0, sizeof(cntpos));
		scanf("%d", &r);
		if (r == 0)
			break;
		scanf("%d", &c);
		for (int i = 0; i < r; i++)
			scanf("%s", &grid[i]);
		for (int j = 0; j < c; j++)
		{
			if (cntpos[0][j] == 0 && grid[0][j] != '*')
				cntpos[0][j] = cnt++;
		}
		for (int i = 1; i < r; i++)
		{
			for (int j = 0; j < c; j++)
			{
				if (j == 0 && grid[i][j] != '*')
					cntpos[i][j] = cnt++;
				else if (grid[i - 1][j] == '*'&&grid[i][j] != '*')
					cntpos[i][j] = cnt++;
				else if (grid[i][j - 1] == '*'&&grid[i][j] != '*')
					cntpos[i][j] = cnt++;
			}
		}
		if (cse != 1)
			printf("\n");
		printf("puzzle #%d:\n", cse++);
		printf("Across\n");
		for (int i = 0; i < r; i++)
		{
			for (int j = 0; j < c; j++)
			{
				if (grid[i][j] != '*')
				{
					printf("%3d.", cntpos[i][j]);
					for (; grid[i][j] != '*'&&j < c; j++)
					{
						printf("%c", grid[i][j]);
					}
					printf("\n");
					/*if (j < c&&grid[i][j] == '*')
						j++;*/
				}
			}
		}
		printf("Down\n");
		for (int i = 0; i < r; i++)
		{
			for (int j = 0; j < c; j++)
			{
				if ((i == 0 || grid[i - 1][j] == '*') && grid[i][j] != '*')
				{
					printf("%3d.", cntpos[i][j]);
					for (int k = i; k < r; k++)
					{
						if (grid[k][j] == '*')
							break;
						printf("%c", grid[k][j]);
					}
					printf("\n");
				}
			}
		}
	}

//	system("pause");
	return 0;
}


你可能感兴趣的:(小紫书 习题 3-6(UVA 232) 纵横字谜的答案(Crossword Answers))