题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=168

(该题目同POJ 1888)

13832879 232 Crossword Answers Accepted C++ 0.279 2014-07-04 13:47:00
13832817 232 Crossword Answers Runtime error C++11 0.000 2014-07-04 13:30:54
13832786 232 Crossword Answers Runtime error C++ 0.000 2014-07-04 13:24:03
13832776 232 Crossword Answers Runtime error C++ 0.000 2014-07-04 13:21:02

 Crossword Answers 

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

解题思路:纯粹字符串乱搞题。开始由于输出时候遍历越界使劲的RE,在不严谨的POJ上居然没有RE,直接AC了,让我好无语。看来是g++和C++的编译环境差异,以后有机会去了解一下两者的区别。越说越想看CSAPP呃。

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <string>
#include <algorithm>
#include <numeric>
using namespace std; bool cmp(pair<int, string> a, pair<int, string> b) {
return a.first < b.first;
} int main() {
char Map[][];
int Mapf[][];
int x, y, cnt = ; while(cin >> x) {
if(x) cin >> y;
else break;
for(int i = ; i < ; i++) {
for(int j = ; j < ; j++) {
Map[i][j] = '*'; }
}
memset(Mapf, , sizeof(Mapf));
pair<int, string> p[];
int cur = ; for(int i = ; i <= x; i++) {
for(int j = ; j <= y; j++) {
cin >> Map[i][j];
if(Map[i][j] == '*') {
Mapf[i][j] = -;
} else {
if(i - < || j - < || Map[i - ][j] == '*' || Map[i][j - ] == '*') Mapf[i][j] = ++cur;
else Mapf[i][j] = -;
}
}
}
/*for(int i = 1; i <= x; i++) {
for(int j = 1; j <= y; j++) {
cout << Mapf[i][j] << " ";
}
cout << endl;
}*/
if(cnt) cout << endl;
int last = ;
printf("puzzle #%d:\n", ++cnt);
cout << "Across" << endl;
for(int i = ; i < ; i++) {
string str = "";
for(int j = ; j < ; j++) {
if(Map[i][j] != '*') {
if(str == "") last = Mapf[i][j];
str += Map[i][j];
} else {
if(str != "") {//cout << " " << last << "." << str << endl;
printf("%3d.", last);
cout << str << endl;
}
str = "";
} }
} cout << "Down" << endl;
int pcu = ;
for(int j = ; j < ; j++) {
string str = "";
for(int i = ; i < ; i++) {
if(Map[i][j] != '*') {
if(str == "") last = Mapf[i][j];
str += Map[i][j];
} else {
if(str != "") {
p[pcu].first = last;
p[pcu++].second = str;
//cout << " " << last << "." << str << endl;
str = "";
}
} }
}
sort(p, p + pcu, cmp);
for(int i = ; i < pcu; i++) {
printf("%3d.", p[i].first);
cout << p[i].second<<endl;
}
} return ;
}


UVa232.Crossword Answers的更多相关文章

  1. poj 1888 Crossword Answers 模拟题

    Crossword Answers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 869   Accepted: 405 D ...

  2. UVa 232 Crossword Answers

     Crossword Answers  A crossword puzzle consists of a rectangular grid of black and white squares and ...

  3. 紫书第三章训练1 D - Crossword Answers

    A crossword puzzle consists of a rectangular grid of black and white squares and two lists of defini ...

  4. Crossword Answers -------行与列按序输出

    题目链接:https://vjudge.net/problem/UVA-232#author=0 题意:关键句:The de nitions correspond to the rectangular ...

  5. Crossword Answers UVA - 232

    题目大意 感觉挺水的一道题.找出左面右面不存在或者是黑色的格子的白各,然后编号输出一横向单词和竖向单词(具体看原题) 解析 ①找出各个格子的编号 ②对每个节点搜索一下 ③输出的时候注意最后一个数据后面 ...

  6. 【习题 3-6 UVA - 232】Crossword Answers

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟题.注意场宽为3 [代码] #include <bits/stdc++.h> using namespace std ...

  7. [刷题]算法竞赛入门经典 3-4/UVa455 3-5/UVa227 3-6/UVa232

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa455:Periodic Strings 代码: //UVa455 #inclu ...

  8. Uva 232 一个换行WA 了四次

    由于UVA OJ上没有Wrong anwser,搞的多花了好长时间去测试程序,之前一直以为改OJ有WA,后来网上一搜才知道没有WA,哎哎浪费了好长时间.此博客用来记录自己的粗心大意. 链接地址:htt ...

  9. HDU3038 How Many Answers Are Wrong[带权并查集]

    How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

随机推荐

  1. Android中各种onTouch事件

    Android里有两个类 android.view.GestureDetector android.view.GestureDetector.SimpleOnGestureListener 1) 新建 ...

  2. c语言二维数组变色龙之死字的打印

    1 #include <stdio.h> #include <stdlib.h> void main() { ][]= { {'#','#','#',' ','#','#',' ...

  3. linux shell在while中用read从键盘输入

    系统是ubuntu 14.04 64bit,之前曾想安装Stream来玩dota2,但最终没成功.由于Stream只有32bit,安装Stream时也安装了大量32bit的库.删除Stream后,这些 ...

  4. 如何将XML转换成XSD(XML Schema)文件

    将xml装换为xsd,先决条件是已经安装了Visual Stutio 1) 输入cmd在运行窗口 2) 将xsd的路径加入到path变量 set path=%path%;C:\Program File ...

  5. Ubuntu 10.04下安装Qt

    sudo apt-get install qt4-dev-tools qt4-doc qt4-qtconfig qt4-demos qt4-designer qt-creator 其中: qt4-de ...

  6. Gson源码分析之Json结构抽象和注解使用

    github上的博客地址: http://chuyun923.github.io/blog/2015/01/06/gsonyuan-ma-fen-xi/ XML和Json作为最常用的两种网络传输格式而 ...

  7. JQuery.ajax一解

    关于JQuery.ajax方法,好处也不用多说了,主要是想记下ajax中的一些参数: url:请求的目标地址,为一个字符串,格式为:http://localhost:端口号/User/方法名.eg:现 ...

  8. PowerDesigner使用常见问题

    1.在数据库生成表的时候,要求PowerDesigner中设计的表的Name的值要放到数据库中表的描述中,而不是PowerDesigner 中字段的Comment: 具体方法如下:首先将PowerDe ...

  9. VC连接数据库方式

    转自:http://www.cnblogs.com/renyuan/archive/2012/07/27/2612412.html 目前Windows系统上常见的数据库接口包括: ODBC(开放数据库 ...

  10. SQL 标准中的四种隔离级别

    READ UNCOMMITED(未提交读) 在RERAD UNCOMMITED级别,事务中的修改,即使没有提交,对其他事务也都是可见的.事务可以读取未提交的数据,这也成为脏读(Dirty Read). ...