当前位置:网站首页>[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
边栏推荐
- 动态规划前路径问题
- Learning record: how to perform PWM output
- Research Report on medical toilet industry - market status analysis and development prospect forecast
- X-forwarded-for details, how to get the client IP
- Shell脚本编程
- cs零基础入门学习记录
- Learning record: use stm32f1 watchdog
- Matlab comprehensive exercise: application in signal and system
- STM32 how to use stlink download program: light LED running light (Library version)
- Medical colposcope Industry Research Report - market status analysis and development prospect forecast
猜你喜欢
MySQL import database error [err] 1273 - unknown collation: 'utf8mb4_ 0900_ ai_ ci’
【练习-5】(Uva 839)Not so Mobile(天平)
渗透测试 ( 1 ) --- 必备 工具、导航
信息安全-史诗级漏洞Log4j的漏洞机理和防范措施
Information security - Epic vulnerability log4j vulnerability mechanism and preventive measures
Learning records: serial communication and solutions to errors encountered
【高老师UML软件建模基础】20级云班课习题答案合集
X-Forwarded-For详解、如何获取到客户端IP
Ball Dropping
Nodejs+vue online fresh flower shop sales information system express+mysql
随机推荐
入门C语言基础问答
C语言数组的概念
C 基本语法
Report on the market trend, technological innovation and market forecast of printing and decorative paper in China
Research Report on medical toilet industry - market status analysis and development prospect forecast
Research Report on shell heater industry - market status analysis and development prospect forecast
Record of force deduction and question brushing
Cost accounting [13]
初入Redis
【练习-7】(Uva 10976)Fractions Again?!(分数拆分)
Research Report on market supply and demand and strategy of geosynthetics industry in China
Hospital privacy screen Industry Research Report - market status analysis and development prospect forecast
Information security - Epic vulnerability log4j vulnerability mechanism and preventive measures
Cost accounting [17]
Opencv learning log 14 - count the number of coins in the picture (regardless of overlap)
Market trend report, technical innovation and market forecast of Chinese hospital respiratory humidification equipment
Truck History
信息安全-安全编排自动化与响应 (SOAR) 技术解析
CS zero foundation introductory learning record
信息安全-威胁检测引擎-常见规则引擎底座性能比较