正解:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
char s[11][11];
int r, c, i, j, k, a[11][11], d[11][11], first = 1, count = 0;//a[11][11]的作用是标记百格,若是白格则标位1//d[11][11]的作用是标记起始格,从1开始增加
while (~scanf("%d", &r) && r)
{
scanf("%d", &c);
for (i = 1; i <= r; i++)
scanf("%s", s[i] + 1);//···①//一行一行的输出
memset(a, 0, sizeof(a));
memset(d, 0, sizeof(d));
k = 0;
for (i = 1; i <= r; i++)
for (j = 1; j <= c; j++)
{
if (s[i][j] != '*') a[i][j] = 1;//a[i][j]=1表示白格
if (a[i][j] && (a[i][j - 1] == 0 || a[i - 1][j] == 0)) d[i][j] = ++k;//d[i][j]=k表示该起始格的编号是k//判断完白格之后再判断是否为起始格
}
/* ···②
for (i = 1;i <= r;i++)
puts(s[i]+1);
for (i = 1;i <= r;i++) {
for (j = 1;j <= c;j++)
printf("%d", a[i][j]);
printf("\n");
}
for (i = 1;i <= r;i++) {
for (j = 1;j <= c;j++)
printf("%d", d[i][j]);
printf("\n");
}
*/
if (first) first = 0;
else putchar('\n');//输入与输出之间分行
printf("puzzle #%d:\nAcross\n", ++count);
for (i = 1; i <= r; i++)//判断横向单词
for (j = 1; j <= c; j++)
{
if (d[i][j] && a[i][j - 1] == 0)//···③//找横向单词的首个,即该格是起始格,上一格是黑格
{
printf("%3d.", d[i][j]);//···④//输出起始格序号
for (k = j; a[i][k] == 1 && k <= c; k++)//a[[i][k]即判断该格是否为白格,而避免考虑下一个是否为黑格
putchar(s[i][k]);
putchar('\n');
}
}
printf("Down\n");
for (i = 1; i <= r; i++)
for (j = 1; j <= c; j++)//判断纵向单词
{
if (d[i][j] && a[i - 1][j] == 0)
{
printf("%3d.", d[i][j]);
for (k = i; a[k][j] == 1 && k <= r; k++)
putchar(s[k][j]);
putchar('\n');
}
}
}
return 0;
}
//所有遍历都可以整个数组遍历,在通过if语句查找是否满足要求,因此可以定义多组数据来保存符合要求的,避免了讨论遍历的内容
//将所有功能(输入查找等)都放在同一组循环完成(同一组数据中形成
我的设想:
#include<stdio.h>
#include<string.h>
struct card {
int a;
int b;
}CARD[25];
int main()
{
char s[5][5];
char num[25];
char c = ' ';
int i, j;
int count = 0;
int t = 0;
int p, q;
int temp = 1;
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
c = getchar();
s[i][j] = c;
}
getchar();
}
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
if (s[i - 1][j] == '*' || s[i][j - 1] == '*' || i == 0 || j == 0) {
CARD[count].a = i;
CARD[count].b = j;
count++;
}
}
}
for (t = 0; t <= count; t++) {
p = CARD[t].a;
q = CARD[t].b;
printf("%d", t);
for (int ss = 0; ss < count; ss++) {
if (CARD[ss].a == p && CARD[ss].b == q) {
temp = 0;
printf("%c", s[p + ss][q + ss]);
break;
}
else break;
}
}
return 0;
}