当前位置:网站首页>[exercise-7] crossover answers
[exercise-7] crossover answers
2022-07-06 15:56:00 【Flame car】
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.
The main idea of the topic :
Is to output strings in order , The end condition of this string is that * Or to the line ( Column ) Ending . And the number at the first letter position should be output before outputting the string , The rule of row number is the first row and the first column as long as it is not * Just count , Others, if there is * We should also count .
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;
}
Break it down :
The first part :
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;
}
This is to arrange the numbers according to the regulations , Input at the same time you can finish . Pretty simple , Should be able to see .
The second part :
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;
}
Traverse the output ... I feel this step is relatively easy to understand .
The third part :
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;
}
This part may be more difficult , In fact, it's still two floors for The output of the loop , But if you find the starting position of the string , Keep looking down , And mark , Then when you scan this character again, it will not be output repeatedly .
details :
① Remember the label puzzle Changes
② Finally, output one more carriage return
③ It's strange that I use OJ The superscript should keep three positions , Such as :()()1.AB ()10.WUD
边栏推荐
- Opencv learning log 33 Gaussian mean filtering
- Alice and Bob (2021牛客暑期多校训练营1)
- F - Birthday Cake(山东省赛)
- 渗透测试 ( 3 ) --- Metasploit Framework ( MSF )
- HDU - 6024 Building Shops(女生赛)
- 信息安全-威胁检测引擎-常见规则引擎底座性能比较
- Research Report on medical toilet industry - market status analysis and development prospect forecast
- 洛谷P1102 A-B数对(二分,map,双指针)
- Truck History
- Research Report on shell heater industry - market status analysis and development prospect forecast
猜你喜欢
渗透测试 ( 5 ) --- 扫描之王 nmap、渗透测试工具实战技巧合集
【练习-4】(Uva 11988)Broken Keyboard(破损的键盘) ==(链表)
洛谷P1102 A-B数对(二分,map,双指针)
C语言数组的概念
MATLAB综合练习:信号与系统中的应用
Information security - threat detection engine - common rule engine base performance comparison
Optimization method of path problem before dynamic planning
STM32 learning record: play with keys to control buzzer and led
渗透测试 ( 1 ) --- 必备 工具、导航
信息安全-史诗级漏洞Log4j的漏洞机理和防范措施
随机推荐
STM32 learning record: play with keys to control buzzer and led
Research Report of exterior wall insulation system (ewis) industry - market status analysis and development prospect prediction
Ball Dropping
Cost accounting [23]
Determine the Photo Position
渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具
Alice and Bob (2021牛客暑期多校训练营1)
D - Function(HDU - 6546)女生赛
【练习-8】(Uva 246)10-20-30==模拟
动态规划前路径问题优化方式
HDU-6025-Coprime Sequence(女生赛)
【练习-1】(Uva 673) Parentheses Balance/平衡的括号 (栈stack)
STM32如何使用STLINK下载程序:点亮LED跑马灯(库版本)
Learning record: use STM32 external input interrupt
China's PCB connector market trend report, technological innovation and market forecast
0-1背包问题(一)
MySQL import database error [err] 1273 - unknown collation: 'utf8mb4_ 0900_ ai_ ci’
Path problem before dynamic planning
Borg Maze (BFS+最小生成树)(解题报告)
0 - 1 problème de sac à dos (1)