当前位置:网站首页>【练习-7】Crossword Answers
【练习-7】Crossword Answers
2022-07-06 09:26:00 【火焰车】
Description
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 (1≤r≤10 and 1≤c≤10), 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.
题目大意:
就是按照顺序输出字符串,这段字符串结束条件是遇到了 * 或是到了该行(列)的结尾。并且输出该字符串之前要输出第一个字母位置上的数,排数的规则是第一行和第一列只要不是 * 就要有数,其他的如果在该位置左侧或上侧有 * 也要有数。
AC CODE:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int r,c;
int main()
{
int time=1;
while(cin>>r && r)
{
char a[15][15]={
0};
int sign[15][15]={
0},ed[15][15]={
0},cnt=0,ord=0;
cin>>c;
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++)
{
cin>>a[i][j];
if(a[i][j]!='*')
if(i==1||j==1||a[i][j-1]=='*'||a[i-1][j]=='*')
sign[i][j] = ++cnt;
}
cout<<"puzzle #"<<time<<":"<<endl;
cout<<"Across"<<endl;
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++)
{
while(a[i][j]=='*') j++;
if(j>c) continue;
if(sign[i][j]<10) cout<<" "<<sign[i][j]<<".";
else cout<<" "<<sign[i][j]<<".";
while(j<=c && a[i][j]!='*')
{
cout<<a[i][j];
j++;
}
cout<<endl;
}
cout<<"Down"<<endl;
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++)
{
while(sign[i][j]==0||ed[i][j]) j++;
if(j>c) continue;
if(sign[i][j]<10) cout<<" "<<sign[i][j]<<".";
else cout<<" "<<sign[i][j]<<".";
int ii=i;
while(ii<=r && a[ii][j]!='*')
{
ed[ii][j]=1;
cout<<a[ii][j];
ii++;
}
cout<<endl;
}
cout<<endl;
time++;
}
return 0;
}
分解着讲一下吧:
第一部分:
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++)
{
cin>>a[i][j];
if(a[i][j]!='*')
if(i==1||j==1||a[i][j-1]=='*'||a[i-1][j]=='*')
sign[i][j] = ++cnt;
}
这就是按照规定排数字,输入的同时就可以排完。挺简单的,应该能看明白。
第二部分:
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++)
{
while(a[i][j]=='*') j++;
if(j>c) continue;
if(sign[i][j]<10) cout<<" "<<sign[i][j]<<".";
else cout<<" "<<sign[i][j]<<".";
while(j<=c && a[i][j]!='*')
{
cout<<a[i][j];
j++;
}
cout<<endl;
}
遍历输出啊。。。感觉这一步还是比较容易理解的可以。
第三部分:
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++)
{
while(sign[i][j]==0||ed[i][j]) j++;
if(j>c) continue;
if(sign[i][j]<10) cout<<" "<<sign[i][j]<<".";
else cout<<" "<<sign[i][j]<<".";
int ii=i;
while(ii<=r && a[ii][j]!='*')
{
ed[ii][j]=1;
cout<<a[ii][j];
ii++;
}
cout<<endl;
}
可能比较难的就是这一部分了,实际上还是两层for循环的输出,只不过如果找到了字符串的起始位置,要往下继续找,并做好标记,之后再扫到这个字符的时候不会重复输出。
细节:
①记得标号puzzle的改变
②最后多输出一个回车
③很奇怪的是在我用的OJ上标号要保持三个位置,如:()()1.AB ()10.WUD
边栏推荐
- Cost accounting [24]
- Opencv learning log 32 edge extraction
- JS调用摄像头
- Research Report on market supply and demand and strategy of Chinese hospital cleaning chemicals industry
- Medical colposcope Industry Research Report - market status analysis and development prospect forecast
- Learning record: Tim - capacitive key detection
- STM32 learning record: LED light flashes (register version)
- Opencv learning log 19 skin grinding
- Accounting regulations and professional ethics [1]
- Flink 使用之 CEP
猜你喜欢
随机推荐
C语言是低级和高级的分水岭
Path problem before dynamic planning
Research Report on medical toilet industry - market status analysis and development prospect forecast
JS --- detailed explanation of JS DOM (IV)
Market trend report, technical innovation and market forecast of geosynthetic clay liner in China
TCP的三次握手与四次挥手
Research Report on market supply and demand and strategy of China's Medical Automation Industry
Opencv learning log 15 count the number of solder joints and output
E. Breaking the Wall
0-1 knapsack problem (I)
毕业才知道IT专业大学生毕业前必做的1010件事
STM32如何使用STLINK下载程序:点亮LED跑马灯(库版本)
FSM and I2C experiment report
Accounting regulations and professional ethics [4]
csapp shell lab
Borg Maze (BFS+最小生成树)(解题报告)
Cost accounting [15]
学习记录:使用STM32F1看门狗
Hospital privacy screen Industry Research Report - market status analysis and development prospect forecast
C语言数组的概念