[英]How to read a char matrix anticlockwise in C++?
So my input should be a nxn (n<=20) matrix of words which do not exceed 9 characters each. 所以我的输入应该是一个nxn(n <= 20)字符矩阵,每个字符不超过9个字符。 The output should be a string sentence, formed by reading the matrix in an clockwise spiral way starting from the top left corner.
输出应该是一个字符串句子,通过从左上角开始以顺时针螺旋方式读取矩阵而形成。 With some help, I managed to get it to work.. though I still have some trouble fully comprehending what's exactly going on.
在一些帮助下,我设法让它发挥作用..虽然我仍然有一些麻烦完全理解究竟发生了什么。 I have absolutely no idea how to do it the other way around though - anticlockwise spiral starting from the bottom right corner.
我完全不知道如何以相反的方式做到这一点 - 逆时针螺旋从右下角开始。 Here's my code so far:
到目前为止,这是我的代码:
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;
Any ideas? 有任何想法吗? And is there not some easier way to do this?
有没有更简单的方法来做到这一点?
The code flows directly from that algorithm. 代码直接从该算法流出。 (Note, I haven't compiled this, so you'll have to check for errors yourself).
(注意,我没有编译这个,所以你必须自己检查错误)。
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.
}
}
Associate every cell (x,y) with a distance and an angle: 将每个单元格(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;
Then sort your data first based on distance, next on angle -- the order is well defined. 然后首先根据距离对数据进行排序,然后根据角度对数据进行排序 - 顺序定义明确。 A combined metric would use eg
d(x,y) * 1000 + (int)a(x,y);
组合度量将使用例如
d(x,y) * 1000 + (int)a(x,y);
to perform the sort in a single pass. 在一次通过中执行排序。
(Disclaimer -- this is more or less language agnostic) (免责声明 - 这或多或少与语言无关)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.