怎么也过不了,不知道哪里出错了哎
查看原帖
怎么也过不了,不知道哪里出错了哎
658760
jlu_dwt楼主2022/1/8 17:24
//用string接收输入以'\n'为结尾
//根据输入的n和m来传入参数初始化char二维数组
//自定义数据结构用于存储数据,起始格、上、下没有白格的情况
#include <stdio.h>
#include <string>
#include <string.h>
#include <iostream>

using namespace std;

#define maxn 100
#define BLACK '*'
//起始格
struct cell_data
{
    int num;
    //起始格的坐标
    int p;
    int q;
    //是否左边无白格
    bool across;
    //是否上面无白格
    bool down;
};


int main()
{
    int n, m, kcase = 0;
    while (scanf("%d",&n) == 1 && n != 0)
    {
        scanf("%d",&m);
        char a[n][m];
        kcase++;
        //这里是输入的时候换行 气死我了!!!
        if (kcase > 1)
        {
            printf("\n");
        }
        for (int i = 0; i < n; i++)
        {
            char ch = getchar();
            string str = "", s = "";
            while (ch == '\n')
            {
                ch = getchar();
            }
            str = ch;
            getline(cin, s, '\n');
            str += s;
            for (int j = 0; j < m; j++)
            {
                a[i][j] = str[j];
            }
        }
        //初始化起始格数组
        cell_data cdata[maxn];
        for (int i = 0; i < maxn; i++)
        {
            cdata[i].num = -1;
        }
        //记录起始格编号
        int num = 0;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                //记录次节点是否是起始格
                bool flag = false;
                //解决上边无白格问题
                if (i == 0 && (a[i][j] != BLACK))
                {
                    cdata[num].down = true;
                    flag = true;
                }
                else if (a[i - 1][j] == BLACK && (a[i][j] != BLACK))
                {
                    cdata[num].down = true;
                    flag = true;
                }
                //解决左边无白格问题
                if (j == 0 && (a[i][j] != BLACK))
                {
                    cdata[num].across = true;
                    flag = true;
                }
                else if (a[i][j - 1] == BLACK && (a[i][j] != BLACK))
                {
                    cdata[num].across = true;
                    flag = true;
                }
                //如果是起始格 则记录相关信息
                if (flag)
                {
                    cdata[num].num = num + 1;
                    cdata[num].p = i;
                    cdata[num].q = j;
                    num++;
                }
            }
        }
        printf("puzzle #%d:\n", kcase);
        printf("Across\n");
        for (int i = 0; cdata[i].num != -1; i++)
        {
            if (cdata[i].across)
            {
                int p = cdata[i].p;
                int q = cdata[i].q;
                printf("%3d.", cdata[i].num);
                while (q < m && (a[p][q] != '*'))
                {
                    printf("%c", a[p][q]);
                    q++;
                }
                printf("\n");
            }
        }
        printf("Down\n");
        for (int i = 0; cdata[i].num != -1; i++)
        {
            if (cdata[i].down)
            {
                int p = cdata[i].p;
                int q = cdata[i].q;
                printf("%3d.", cdata[i].num);
                while (p < n && (a[p][q] != '*'))
                {
                    printf("%c", a[p][q]);
                    p++;
                }
                printf("\n");
            }
        }
    }
    return 0;
}

2022/1/8 17:24
加载中...