您的位置:首页 > 编程语言 > C语言/C++

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;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACMICPC uva c语言