题目链接: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. javascript笔记4之运算符

    /* var box = 100; --box; //前置递增,box = box +1 alert(box);//99 var box = 100; var age = ++box; //先box累 ...

  2. Ubuntu/Linux 笔记应用 为知笔记(支持markdown)

    发现网易云笔记没有Linux,但是为知笔记有Linux版本,且支持markdown格式 sudo add-apt-repository ppa:wiznote-team sudo apt-get up ...

  3. poj 3273 Monthly Expense(二分搜索之最大化最小值)

    Description Farmer John ≤ moneyi ≤ ,) that he will need to spend each day over the next N ( ≤ N ≤ ,) ...

  4. 源码分析之spring-JdbcTemplate日志打印sql语句

    对于开源的项目来说的好处就是我们遇到什么问题可以通过看源码来解决. 比如近期有个同事问我说,为啥JdbcTemplate中只有在Error的时候才打印出sql语句呢.我一想,这和log的配置有关系吧. ...

  5. [Unity3D]蓝港面试题

    1. 请简述值类型与引用类型的差别 答: 差别:1.值类型存储在内存栈中,引用类型数据存储在内存堆中,而内存单元中存放的是堆中存放的地址.2.值类型存取快,引用类型存取慢.3.值类型表示实际数据,引用 ...

  6. android避免service被杀

    1.在service中重写下面的方法,这个方法有三个返回值, START_STICKY是service被kill掉后自动重写创建@Override    public int onStartComma ...

  7. Error when launching Quest Central for DB2: "QCC10000E - Unable to allocate environment handle fo

    标题 Error when launching Quest Central for DB2: "QCC10000E - Unable to allocate environment hand ...

  8. CodeSmith使用总结--调用自定义方法

    上一篇读取了一个表的内容,但是到了真正应用的时候还是不够用的,我们很容易可以对比出来,SQL里边的数据类型的定义和C#中有所不同,比如Saler--String,大写的String在C#中不是一个类型 ...

  9. WPF DataGrid 之数据绑定--实例2

    1.前台Grid定义 <!--数据绑定--> <DataGrid Grid.Row="1" Name="gridOne" Margin=&qu ...

  10. git命令简图

    基本概念 版本库(repository)=.git目录 工作区(working)=当前工作的目录 暂存区(stage)=临时缓存 图示 仓库操作 分支操作 工作区操作 参考链接(相当好) http:/ ...