UVa232
2016-09-29 08:33
295 查看
在获取数据后,通过题意判断条件判断网格中的编号并用二维数组 xuhao[ ] 存储,在寻找横向和竖向的字符串的过程中,可以设置一个 move 变量,通过 move 的移动,来数组字符。在输出竖向的字符串的时候应该格外的注意,因为在竖向输出字符串的时候,编号的顺序有可能会混乱。自己尝试了很久,最后看了小白菜又菜写的代码才给我带来了解题的思路。即建立一个结构体数组data,里面的data.id用来计数单词数,data.str来存储字符串。最后用sort来对所有的data.str进行排序,然后按序输出。
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef struct node{
int id;
char str[12];
}node;
node data[100+5];
char maps[10][10];
int xuhao[10][10];
int row, column;
int kase = 0;
int cmp(node a, node b)
{
return a.id < b.id;
}
int main()
{
while(scanf("%d",&row))
{
if(!row) break;
scanf("%d",&column);
for(int i = 0; i < row; i++)
{
scanf("%s",maps[i]);
}
int id = 1;
for(int i = 0; i < row; i++)
{
for(int j = 0; j < column; j++)
{
if( maps[i][j] != '*' && ( (!i) || (!j) || maps[i-1][j] == '*' || maps[i][j-1] == '*') )
xuhao[i][j] = id++;
else
xuhao[i][j] = 0;
}
}
if(kase) printf("\n");
printf("puzzle #%d:\n", ++kase);
printf("Across\n");
for(int i = 0; i < row; i++)
{
int move = 0;
while(move < column)
{
if(xuhao[i][move])
{
printf("%3d.",xuhao[i][move]);
while(move < column && maps[i][move] != '*')
{
printf("%c",maps[i][move++]);
}
printf("\n");
}
else
move++;
}
}
printf("Down\n");
int count = 0;
for(int i = 0; i < column; i++)
{
int move = 0;
while(move < row)
{
if(xuhao[move][i])
{
data[count].id = xuhao[move][i];
int save = 0;
while((move < row) && (maps[move][i] != '*'))
{
data[count].str[save++] = maps[move++][i];
}
data[count++].str[save++] = 0;
}
else move++;
}
}
sort(data, data+count, cmp);
for(int i = 0; i < count; i++)
{
printf("%3d.%s\n",data[i].id, data[i].str);
}
}
return 0;
}
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef struct node{
int id;
char str[12];
}node;
node data[100+5];
char maps[10][10];
int xuhao[10][10];
int row, column;
int kase = 0;
int cmp(node a, node b)
{
return a.id < b.id;
}
int main()
{
while(scanf("%d",&row))
{
if(!row) break;
scanf("%d",&column);
for(int i = 0; i < row; i++)
{
scanf("%s",maps[i]);
}
int id = 1;
for(int i = 0; i < row; i++)
{
for(int j = 0; j < column; j++)
{
if( maps[i][j] != '*' && ( (!i) || (!j) || maps[i-1][j] == '*' || maps[i][j-1] == '*') )
xuhao[i][j] = id++;
else
xuhao[i][j] = 0;
}
}
if(kase) printf("\n");
printf("puzzle #%d:\n", ++kase);
printf("Across\n");
for(int i = 0; i < row; i++)
{
int move = 0;
while(move < column)
{
if(xuhao[i][move])
{
printf("%3d.",xuhao[i][move]);
while(move < column && maps[i][move] != '*')
{
printf("%c",maps[i][move++]);
}
printf("\n");
}
else
move++;
}
}
printf("Down\n");
int count = 0;
for(int i = 0; i < column; i++)
{
int move = 0;
while(move < row)
{
if(xuhao[move][i])
{
data[count].id = xuhao[move][i];
int save = 0;
while((move < row) && (maps[move][i] != '*'))
{
data[count].str[save++] = maps[move++][i];
}
data[count++].str[save++] = 0;
}
else move++;
}
}
sort(data, data+count, cmp);
for(int i = 0; i < count; i++)
{
printf("%3d.%s\n",data[i].id, data[i].str);
}
}
return 0;
}
相关文章推荐
- UVA 232 字符串处理
- UVa 232 - Crossword Answers
- UVA-3.6-纵横字谜-232
- uva 232 - Crossword Answers(纵 dbbb 横迷宫)
- UVa 232:Crossword Answers
- (UVA)232 - Crossword Answers
- UVa232 Crossword Answers——C++版本
- UVA - 232 Crossword Answers
- UVA - 232 Crossword Answers
- UVa 232 - Crossword Answersa
- 纵横字谜的答案(Crossword Answer,ACM/ICPC Word Finals 1994,UVa232)
- UVa 232, Crossword Answers
- UVa 232 Crossword Answers
- uva 232 Crossword Answers
- 小紫书 习题 3-6(UVA 232) 纵横字谜的答案(Crossword Answers)
- UVa 232 Crossword Answer(纵横字谜的答案)
- UVA - 232 Crossword Answers
- uva 232 - Crossword Answers
- 习题3-6 纵横字谜的答案(Crossword Answers, ACM/ICPC World Finals 1994, UVa232)
- 算法竞赛入门经典-习题3-6 纵横字谜的答案(Crossword Answers, ACM/ICPC World Finals 1994,UVa232)