UVa 232 - Crossword Answers

Contents

  1. 1. Problem
  2. 2. Solution
  3. 3. Code

Problem

中文網址

Solution

直接去判斷每一格的上面和左邊,是否有東西。記得自己那格不得為 ‘*’ 。

Code

UVa 232
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<cstdio>

int main()
{
char puzzle[12][12] = {};
int r, c, Case = 0, id[12][12];
while (scanf("%d", &r) && r)
{
scanf("%d", &c);

getchar();
int i, j, count = 1;

//border
for (i = 0; i <= c + 1; i++)
puzzle[r + 1][i] = NULL;
for (i = 0; i <= r + 1; i++)
puzzle[i][c + 1] = NULL;

for (i = 1; i <= r; i++)
{
for (j = 1; j <= c; j++)
{
puzzle[i][j] = getchar();
if (puzzle[i][j] != '*' && (!puzzle[i - 1][j] || puzzle[i - 1][j] == '*' || !puzzle[i][j - 1] || puzzle[i][j - 1] == '*'))
id[i][j] = count++;
}
getchar();
}

if (Case)
putchar('\n');

printf("puzzle #%d:\n", ++Case);

//Across
puts("Across");
for (i = 1; i <= r; i++)
{
for (j = 1; j <= c; j++)
{
if (puzzle[i][j] != '*' && (puzzle[i][j - 1] == '*' || !puzzle[i][j - 1]))
{
printf("%3d.", id[i][j]);
for (int k = j; puzzle[i][k] != '*'&&puzzle[i][k]; k++)
putchar(puzzle[i][k]);
putchar('\n');
}
}
}

//Down
puts("Down");
for (i = 1; i <= r; i++)
{
for (j = 1; j <= c; j++)
{
if (puzzle[i][j] != '*' && (puzzle[i - 1][j] == '*' || !puzzle[i - 1][j]))
{
printf("%3d.", id[i][j]);
for (int k = i; puzzle[k][j] != '*'&&puzzle[k][j]; k++)
putchar(puzzle[k][j]);
putchar('\n');
}
}
}
}

return 0;
}