【问题标题】:How to print a Pentomino rectangle using c++如何使用 C++ 打印 Pentomino 矩形
【发布时间】:2018-12-28 07:54:20
【问题描述】:

我有一个 6x10 数组,它定义了我应该打印的形状。例如

{'a','b','b','b','c','c','c','d','e','e'},
{'a','b','f','f','f','c','d','d','d','e'},
{'a','b','f','f','g','c','h','d','e','e'},
{'a','i','i','g','g','h','h','h','j','j'},
{'a','i','g','g','k','h','j','j','j','l'}, 
{'i','i','k','k','k','k','l','l','l','l'}

和这张图一样:

我应该使用这些字符:

我的代码是这样的:

void PrintSolution()
{
    for (i=0; i<6 ; i++)
    {
        for (j=0; j<10 ; j++)
        {
            char a=board[i][j], b=board[i][j+1], c=board[i+1][j+1], d=board[i+1][j];

            if (a==b==c==d)
                cout<<(char)32;

            if (a==d && b==c && a!=c)
                cout<<(char)179;


            if (b==c && a!=b && a!=d && c!=d)
                cout<<(char)180;

            if (a==b==c && d!=a)
                cout<<(char)191;

            if (a==c==d && b!=a)
                cout<<(char)192;

            if (c==d && a!=b && a!=d && b!=c)
                cout<<(char)193;

            if (a==b && a!=c && a!=d && d!=c)
                cout<<(char)194;

            if (a==d && a!=c && a!=b && b!=c)
                cout<<(char)195;

            if (a==b && c==d && a!=c)
                cout<<(char)196;

            if (a!=b && b!=c && a!=d && d!=c)
                cout<<(char)197;

            if (b==c==d && a!=b)
                cout<<(char)217;

            if (a==b==d && a!=c)
                cout<<(char)218;
        }
        cout<<endl;
    }
    cout<<endl;
}

它查找矩阵中的每四个元素,并相应地打印正确的字符,但它打印出一团糟!我不知道是否应该更改算法或在代码中添加一些细节。

提前致谢。

【问题讨论】:

  • 请使用tour 并阅读How to Ask。此外,像您这样的问题必须带有minimal reproducible example,即您应该首先将您的问题归结为核心。否则,您的问题将被视为题外话。
  • 请注意参考图片中使用了多少个字符(不应该是图像,而是复制粘贴的文本)来绘制五联骨牌。您还应该在访问越界数组之前检查索引。
  • if (a==b==c==d) 嗯.. 你收到警告了吗?
  • @Bob__ 我不知道如何找到字符数。这只是一张没有更多细节的图片。
  • @4386427 不,我没有收到相关消息。

标签: c++ algorithm terminal


【解决方案1】:

您需要添加特殊代码来处理边框。您当前的代码忽略了第一行的边框,更糟糕的是,当i 为 5 且j 为 9 时,您访问数组外部。例如这里:c=board[i+1][j+1]

此外,我建议您使用if-else 使代码更清晰,即很明显您在每个循环中只打印一个字符。然后,您可以稍后对其进行优化。

下面的代码不是一个完整的解决方案 - 它是一些可以让您更好地开始的代码。

void PrintSolution()
{
    for (int i=0; i<6 ; i++)
    {
        for (int j=0; j<10 ; j++)
        {
            if (i == 0)
            {
                if (j == 0)
                {
                    // Special case, i.e. first row, first column

                    // Here the output is: ┌
                }
                else if (j == 9)
                {
                    // Special case, i.e. first row, last column

                    // Here the output is one of: ┬ or ─
                    if (board[i][j-1] == board[i][j])
                    {
                        // Here the output is: ─
                    }
                    else
                    {
                        // Here the output is: ┬
                    }

                    // And an extra char for last column
                    // Here the output is: ┐
                }
                else
                {
                    // Special case, i.e. first row

                    // Here the output is one of: ┬ or ─
                    if (board[i][j-1] == board[i][j])
                    {
                        // Here the output is: ─
                    }
                    else
                    {
                        // Here the output is: ┬
                    }
                }
            }
            else if (i == 5)
            {
                if (j == 0)
                {
                    // Special case, i.e. last row, first column
                }
                else if (j == 9)
                {
                    // Special case, i.e. last row, last column
                }
                else
                {
                    // Special case, i.e. last row
                }
            }
            else
            {
                if (j == 0)
                {
                    // Special case, i.e. first column
                }
                else if (j == 9)
                {
                    // Special case, i.e. last column
                }
                else
                {
                    // Normal case
                }
            }
        }
        cout << endl;
    }
    cout << endl;
}

如您所见,最终需要考虑 9 种不同的情况。

【讨论】:

  • 谢谢,但我的主要问题是处理正常情况。我不知道我的算法是否有效。
  • @S.H.W 我已经为你完成了第一行。这应该会给你一个想法。
  • 这似乎是不必要的冗长。决定打印哪个字符所需的唯一信息是边框周围的 4 个字符。当然,外面有一个边框,但是可以很容易地处理边框,而无需通过为其分配一些不存在于边框内的常量来对其进行特殊处理。
  • @PeterT 正如我在代码之前写的那样,这是故意制作的,以便每个案例都由其自己的 if-else 块处理。并且代码可以在以后进行优化。这个想法是“保持每个案例简单”,以便 OP 可以找出解决方案。而且 - 是的 - 对于“中间”的所有字符,您只需要查看 4 个数组值。
  • @4386427 非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-28
  • 2017-09-26
相关资源
最近更新 更多