题目: 给你一个二维的字符表,里面有一些字母和,和边界都当做是分隔符, 按照图示方式编号连续位置(从上到下,从左到右)生成单词,输出所有横向和竖向单词。
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<iomanip>
#define ll long long
using namespace std;
int m,n,a[20][20];
string s[20];
typedef struct _node
{
int id;
char str[12];
}node;
node data[101];
int cmp(node a, node b)
{
return a.id < b.id;
}
void init()
{
int c = 0;
memset(a,0,sizeof(a));
for(int i = 0; i < n; i++)
if(s[0][i]!='*') a[0][i] = (++c);
for(int i = 1; i < m; i++)
for(int j = 0; j < n; j++) {
if(s[i][j]!='*') {
if(j == 0) a[i][j] = (++c);
else if(s[i-1][j] == '*'||s[i][j-1] == '*') a[i][j] = (++c);
}
}
}
void solve() {
printf("Across\n");
for (int i = 0; i < m; i++) {
int c = 0;
while (c < n) {
if (a[i][c]) {
printf("%3d.",a[i][c]);
int save = 0;
while (c < n && s[i][c] != '*')
printf("%c",s[i][c ++]);
printf("\n");
}else c++;
}
}
printf("Down\n");
int cut = 0;
for (int i = 0; i < n; ++ i) {
int c = 0;
while (c < m) {
if (a[c][i]) {
data[cut].id = a[c][i];
int save = 0;
while (c < m && s[c][i] != '*')
data[cut].str[save ++] = s[c ++][i];
data[cut ++].str[save ++] = 0;
}else c ++;
}
}
sort(data, data+cut, cmp);
for (int i = 0; i < cut; i++)
printf("%3d.%s\n",data[i].id, data[i].str);
}
int main()
{
int caset = 0;
while(1){
scanf("%d",&m); if(m == 0) break;
if(caset != 0) cout << endl;
scanf("%d",&n);
for(int i = 0; i < m; i++) cin >> s[i];
init();
printf("puzzle #%d:\n",++caset);
solve();
for(int i = 0; i < m; i++)
s[i].erase();
}
return 0;
}