【问题标题】:How to read a char matrix anticlockwise in C++?如何在 C++ 中逆时针读取 char 矩阵?
【发布时间】:2014-01-23 19:40:26
【问题描述】:

所以我的输入应该是一个 nxn (n

    string s;
    int hor = 0, vert = 0;
    while (hor < n / 2 && vert < n / 2)
    {
        for (i = vert; i < n - vert; i++)
            s = s + a[hor][i] + " ";
        for (i = hor + 1; i < n - hor; i++)
            s = s + a[i][n - vert - 1] + " ";
        for (i = n - vert - 2; i >= vert; i--)
            s = s + a[n - hor - 1][i] + " ";
        for (i = n - hor - 2; i > hor; i--)
            s = s + a[i][vert] + " ";
        hor++;
        vert++;
    }
    if (n % 2)
        for (i = vert; i < n - vert; i++)
            s = s + a[hor][i] + " ";
    else
        for (i = hor; i < n - hor; i++)
            s = s + a[i][vert] + " ";
    cout << s << endl;

有什么想法吗?难道没有更简单的方法吗?

【问题讨论】:

    标签: c++ string matrix char spiral


    【解决方案1】:
    1. 使用变量 x 和 y 作为您的位置,这样更容易跟踪。
    2. 使用状态变量来跟踪您的方向。逆时针是左、上、右、下。
    3. 从每个位置添加一个单词。
    4. 前进你的位置,直到你碰到边缘,然后改变方向。这样做四次(每个方向一次),然后增加一个边缘计数器,代表你的螺旋的宽度。
    5. 当您完成 400 次 (20*20) 后,您就完成了。

    代码直接来自该算法。 (注意,我没有编译这个,所以你必须自己检查错误)。

    int x = n - 1; // Start at right.
    int y = n - 1; // Start at bottom.
    int spiral = 0; // Track the edges already visited (spiral).
    int direction = 0; // 0=left, 1=up, 2=right, 3=down.  You could also use an enum.
    string s; // String builder.
    
    for (int count = 0; count < 400; count++) { // One word per cell, 400 cells.
      s = s + a[x][y] + " "; // Add the word.
      if (direction == 0) { // Left
        if (x > spiral) x--; // Check edge, advance if no edge.
        else { y--; direction = 1; } // Change direction to up if edge is hit.
      } else if (direction == 1) { // Up
        if (y > spiral) y--; // Check edge, advance if no edge.
        else { x++; direction = 2; } // Change direction to right if edge is hit.
      } else if (direction == 2) { // Right
        if (x < (n - 1) - spiral) x++; // Check edge, advance if no edge.
        else { y++; direction = 3; spiral++; } // Change edge and direction to down.      
      } else if (direction == 3) { // Down
        if (y < (n - 1) - spiral) y++; // Check edge, advance if no edge.
        else { x--; direction = 0; } // Change direction to left if hit.
      }
    }
    

    【讨论】:

    • 谢谢!这真的很有帮助,而且容易得多。我注意到了两个错误。我自己修的第一个 - 应该是 [y][x]。然而,第二个我做不到。螺旋整数有问题。不管我把它弄得多么混乱(将“螺旋++”放在不同的方向上)它都行不通。 3x3 在上下方向上与螺旋 ++ 一起使用,仅此而已。
    • 至于y/x位置,是根据你原来的代码,我看不到你的数据,所以很好。至于另一件事,你是对的,spiral++ 是在错误的地方。将其移至正确方向的 else 子句应该可以解决问题。我修改了我原来的帖子,所以你可以看到确切的位置。
    【解决方案2】:

    将每个单元格 (x,y) 与距离和角度相关联:

     d(x,y) = max(abs(x - N/2), abs(y - N/2))
     a(x,y) = atan2(y - N/2, x - N/2) + bias;
    

    然后首先根据距离对数据进行排序,然后根据角度对数据进行排序——顺序已明确定义。 组合指标将使用例如d(x,y) * 1000 + (int)a(x,y); 一次性执行排序。

    (免责声明——这或多或少与语言无关)

    【讨论】:

    • 这很有趣,但这不适用于方形阵列,因为到角的距离比到边缘的距离更远。
    • 这就是距离不是欧几里得的原因。
    • 确实很有趣——但是太高级了,而且对于 OP 来说可能有问题。但是 - 如果这是我的问题,我会很想以这种方式尝试只是为了好玩!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-07
    • 2019-11-05
    • 1970-01-01
    相关资源
    最近更新 更多