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 ( and ), 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 c characters 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
题目类型: 简单模拟题
算法分析:对于二维数组从左至右,从上至下遍历每个空间并判断该位置是否是”起始格”并赋予相应的序号。然后先以行优先输出行标志为true的单词,然后是以行优先输出列标志为true的单词。注意输出的最后一个测试用例的后面没有多余的空行,否则在UVA上会WA,下面第一个代码是更好的实现
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
/************************************************** filename :l.cpp author :maksyuki created time :2017/12/2 11:45:58 last modified :2017/12/2 12:35:28 file location :C:\Users\abcd\Desktop\TheEternalPoet ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; int row, col; int v[66][66]; char g[66][66]; bool check(int x, int y) { if(x - 1 < 1 || y - 1 < 1) return true; if(g[x-1][y] == '*' || g[x][y-1] == '*') return true; return false; } int main() { #ifdef LOCAL CFF; //CFO; #endif int id = 1; while(scanf(" %d", &row) != EOF) { if(!row) break; scanf(" %d", &col); memset(v, 0, sizeof(v)); for(int i = 1; i <= row; i++) for(int j = 1; j <= col; j++) scanf(" %c", &g[i][j]); int cnt = 1; for(int i = 1; i <= row; i++) for(int j = 1; j <= col; j++) { if(g[i][j] != '*') if(check(i, j)) v[i][j] = cnt++; } if(id > 1) puts(""); printf("puzzle #%d:\n", id++); puts("Across"); for(int i = 1; i <= row; i++) { for(int j = 1; j <= col; j++) { if(v[i][j] && (j - 1 < 1 || g[i][j-1] == '*')) { printf("%3d.", v[i][j]); int tmpy = j; while(tmpy <= col && g[i][tmpy] != '*') { putchar(g[i][tmpy]); tmpy++; } puts(""); } } } puts("Down"); for(int i = 1; i <= row; i++) { for(int j = 1; j <= col; j++) { if(v[i][j] && (i - 1 < 1 || g[i-1][j] == '*')) { printf("%3d.", v[i][j]); int tmpx = i; while(tmpx <= row && g[tmpx][j] != '*') { putchar(g[tmpx][j]); tmpx++; } puts(""); } } } } return 0; } |
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
#include <iostream> #include <fstream> #include <cstring> #include <iomanip> using namespace std; const int maxn = 10 + 6; char ans[maxn][maxn]; struct point { int val; bool pos[2]; //pos = 0 means the across pos = 1 means the down; }; point num[maxn][maxn]; int row, col; bool OkMove (int x, int y) { if (x >= 0 && x <= row - 1 && y >= 0 && y <= col - 1 && ans[x][y] != '*') return true; return false; } void OutPut (int x, int y, int flag) { if (flag == 0) { cout << ans[x][y]; while (OkMove (x, y + 1)) { y += 1; cout << ans[x][y]; } } else { cout << ans[x][y]; while (OkMove (x + 1, y)) { x += 1; cout << ans[x][y]; } } cout << endl; } int main() { //ifstream cin ("aaa.txt"); int flag = 1; while (cin >> row) { if (row == 0) break; cin >> col; int i, j; for (i = 0; i < row; i++) for (j = 0; j < col; j++) { cin >> ans[i][j]; num[i][j].val = -1; num[i][j].pos[0] = false; num[i][j].pos[1] = false; } int cnt = 1; for (i = 0; i < row; i++) for (j = 0; j < col; j++) { if (ans[i][j] != '*' && !OkMove (i, j - 1)) { if (num[i][j].val == -1) { num[i][j].val = cnt++; num[i][j].pos[0] = true; } else num[i][j].pos[0] = true; } if (ans[i][j] != '*' && !OkMove (i - 1, j)) { if (num[i][j].val == -1) { num[i][j].val = cnt++; num[i][j].pos[1] = true; } else num[i][j].pos[1] = true; } } if (flag - 1) cout << endl; cout << "puzzle #" << flag++ << ":" << endl << "Across" << endl; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) if (num[i][j].val != -1 && num[i][j].pos[0]) { cout << setw (3) << num[i][j].val << "."; OutPut (i, j, 0); } } cout << "Down" << endl; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) if (num[i][j].val != -1 && num[i][j].pos[1]) { cout << setw (3) << num[i][j].val << "."; OutPut (i, j, 1); } } } return 0; } |
- « 上一篇:uva237
- uva272:下一篇 »