当前位置:网站首页>[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
边栏推荐
- MySQL import database error [err] 1273 - unknown collation: 'utf8mb4_ 0900_ ai_ ci’
- Cost accounting [21]
- Cost accounting [24]
- C 基本语法
- Information security - Analysis of security orchestration automation and response (soar) technology
- Matlab comprehensive exercise: application in signal and system
- 信息安全-安全专业名称|CVE|RCE|POC|VUL|0DAY
- 毕业才知道IT专业大学生毕业前必做的1010件事
- 【练习-9】Zombie’s Treasure Chest
- Opencv learning log 32 edge extraction
猜你喜欢

Optimization method of path problem before dynamic planning

Borg Maze (BFS+最小生成树)(解题报告)

STM32 learning record: play with keys to control buzzer and led

7-1 懂的都懂 (20 分)

Learning record: how to perform PWM output

Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs

STM32 learning record: LED light flashes (register version)

Nodejs+vue online fresh flower shop sales information system express+mysql

信息安全-安全编排自动化与响应 (SOAR) 技术解析

C语言学习笔记
随机推荐
【练习-8】(Uva 246)10-20-30==模拟
【练习-2】(Uva 712) S-Trees (S树)
基于web的照片数码冲印网站
STM32 learning record: play with keys to control buzzer and led
【练习-11】4 Values whose Sum is 0(和为0的4个值)
Information security - Epic vulnerability log4j vulnerability mechanism and preventive measures
Learning record: use stm32f1 watchdog
Research Report on pharmaceutical R & D outsourcing service industry - market status analysis and development prospect forecast
差分(一维,二维,三维) 蓝桥杯三体攻击
China potato slicer market trend report, technical dynamic innovation and market forecast
Learning record: STM32F103 clock system overview working principle
Gartner:关于零信任网络访问最佳实践的五个建议
cs零基础入门学习记录
Accounting regulations and professional ethics [5]
最全编程语言在线 API 文档
数据在内存中的存储&载入内存,让程序运行起来
Cost accounting [17]
Cost accounting [13]
Research Report on market supply and demand and strategy of Chinese hospital cleaning chemicals industry
想应聘程序员,您的简历就该这样写【精华总结】