# 雁过无痕

 C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::

1 按顺时针方向构建一个m * n的螺旋矩阵（或按顺时针方向螺旋访问一个m * n的矩阵）：

2 在不构造螺旋矩阵的情况下，给定坐标ij值求其对应的值f(i, j)

(i, i) ----------------------------------------- (i, n–1-i)

|                                                    |

|                                                    |

|                                                    |

(m-1-i, i) ----------------------------------------- (m-1-i, n-1-i)

inline void act(int t) { printf("%3d ", t); }

const int small = col < row ? col : row;

const int count = small / 2;

for (int i = 0; i < count; ++i) {

const int C = col - 1 - i;

const int R = row - 1 - i;

for (int j = i; j < C; ++j) act(arr[i][j]);

for (int j = i; j < R; ++j) act(arr[j][C]);

for (int j = C; j > i; --j) act(arr[R][j]);

for (int j = R; j > i; --j) act(arr[j][i]);

}

if (small & 1) {

const int i = count;

if (row <= col) for (int j = i; j < col - i; ++j) act(arr[i][j]);

else for (int j = i; j < row - i; ++j) act(arr[j][i]);

}

const int small = col < row ? col : row;

const int count = small / 2;

for (int i = 0; i < count; ++i) {

const int C = col - 1 - i;

const int R = row - 1 - i;

const int cc = C - i;

const int rr = R - i;

const int s = 2 * i * (row + col - 2 * i) + 1;

for (int j = i, k = s; j < C; ++j) arr[i][j] = k++;

for (int j = i, k = s + cc; j < R; ++j) arr[j][C] = k++;

for (int j = C, k = s + cc + rr; j > i; --j) arr[R][j] = k++;

for (int j = R, k = s + cc * 2 + rr; j > i; --j) arr[j][i] = k++;

}

if (small & 1) {

const int i = count;

int k = 2 * i * (row + col - 2 * i) + 1;

if (row <= col) for (int j = i; j < col - i; ++j) arr[i][j] = k++;

else for (int j = i; j < row - i; ++j) arr[j][i] = k++;

}

(i, i) ----------------------------------------- (i, n–1-i)

|                                                    |

|                                                    |

|                                                    |

(m-1-i, i) ----------------------------------------- (m-1-i, n-1-i)

k = min{x, y, m-1-x, n-1-y}

m*n矩阵删除访问第k个矩形前所访问的所有元素后，可得到(m-2*k)*(n-2*k)矩阵，因此已访问的元素个数为：m*n-(m-2*k)*(n-2*k)=2*k*(m+n-2*k)，因而 (k,k)对应的值为：

T(k) = 2*k*(m+n-2*k)+ 1

① 向右和向下都只是横坐标或纵坐标增加1，这两条边上的点满足f(x, y) = T(k) + d

② 向左和向下都只是横坐标或纵坐标减少1，这两条边上的点满足f(x, y) = T(k+1) - d

int getv(int row, int col, int max_row, int max_col) // row < max_row, col < max_col

{

int level = min(min(row, max_row - 1 - row), min(col, max_col - 1 - col));

int distance = row + col - level * 2;

int start_value = 2 * level * (max_row + max_col - 2 * level) + 1;

if (row == level || col == max_col - 1 - level ||

(max_col < max_row && level * 2 + 1 == max_col))

return start_value + distance;

int next_value = start_value + (max_row + max_col - 4 * level - 2) * 2;

return next_value - distance;

}

if (small & 1) act[count][count];

//螺旋矩阵，给定坐标直接求值 by flyinghearts

//www.cnblogs.com/flyinghearts

#include<iostream>

#include<algorithm>

using std::min;

using std::cout;

/*

int getv2(int row, int col, int max_row, int max_col) // row < max_row, col < max_col

{

int level = min(min(row, max_row - 1 - row), min(col, max_col - 1 - col));

int distance = row + col - level * 2;

int start_value = 2 * level * (max_row + max_col - 2 * level) + 1;

if (row == level || col == max_col - 1 - level) return start_value + distance;

//++level; int next_value = 2 * level * (max_row + max_col - 2 * level) + 1;

int next_value = start_value + (max_row + max_col - 4 * level - 2) * 2;

if (next_value > max_col * max_row) return start_value + distance;

return next_value - distance;

}

*/

int getv(int row, int col, int max_row, int max_col) // row < max_row, col < max_col

{

int level = min(min(row, max_row - 1 - row), min(col, max_col - 1 - col));

int distance = row + col - level * 2;

int start_value = 2 * level * (max_row + max_col - 2 * level) + 1;

if (row == level || col == max_col - 1 - level || (max_col < max_row && level * 2 + 1 == max_col))

return start_value + distance;

//++level; int next_value = 2 * level * (max_row + max_col - 2 * level) + 1;

int next_value = start_value + (max_row + max_col - 4 * level - 2) * 2;

return next_value - distance;

}

int main()

{

int test[][2] = {{5, 5}, {5, 7}, {7, 5}, {4, 4}, {4, 6}, {6, 4}};

const int sz = sizeof(test) / sizeof(test[0]);

for (int k = 0; k < sz; ++k) {

int M = test[k][0];

int N = test[k][1];

for (int i = 0; i < M; ++i) {

for (int j = 0; j < N; ++j)

cout.width(4), cout << getv(i, j, M, N) << " ";

cout << "\n";

}

cout << "\n";

}

}

//螺旋矩阵 by flyinghearts#qq.com

//www.cnblogs.com/flyinghearts

#include<iostream>

int counter = 0;

inline void act(int& t)

{

//std::cout.width(3), std::cout << t;

t = ++::counter;

}

void act_arr(int *arr, int row, int col, int max_col) //col < max_col

{

const int small = col < row ? col : row;

const int count = small / 2;

int *p = arr;

for (int i = 0; i < count; ++i) {

const int C = col - 1 - 2 * i;

const int R = row - 1 - 2 * i;

for (int j = 0; j < C; ++j) act(*p++);

for (int j = 0; j < R; ++j) act(*p), p += max_col;

for (int j = 0; j < C; ++j) act(*p--);

for (int j = 0; j < R; ++j) act(*p), p -= max_col;

p += max_col + 1;

}

if (small & 1) {

const int i = count;

if (row <= col) for (int j = 0; j < col - 2 * i; ++j) act(*p++);

else for (int j = 0; j < row - 2 * i; ++j) act(*p), p += max_col;

}

}

void act_arr(int* arr[], int row, int col)

{

const int small = col < row ? col : row;

const int count = small / 2;

for (int i = 0; i < count; ++i) {

const int C = col - 1 - i;

const int R = row - 1 - i;

for (int j = i; j < C; ++j) act(arr[i][j]);

for (int j = i; j < R; ++j) act(arr[j][C]);

for (int j = C; j > i; --j) act(arr[R][j]);

for (int j = R; j > i; --j) act(arr[j][i]);

}

if (small & 1) {

const int i = count;

if (row <= col) for (int j = i; j < col - i; ++j) act(arr[i][j]);

else for (int j = i; j < row - i; ++j) act(arr[j][i]);

}

}

void act_arr_2(int* arr[], int row, int col)

{

const int small = col < row ? col : row;

const int count = small / 2;

for (int i = 0; i < count; ++i) {

const int C = col - 1 - i;

const int R = row - 1 - i;

const int cc = C - i;

const int rr = R - i;

const int s = 2 * i * (row + col - 2 * i) + 1;

for (int j = i, k = s; j < C; ++j) arr[i][j] = k++;

for (int j = i, k = s + cc; j < R; ++j) arr[j][C] = k++;

for (int j = C, k = s + cc + rr; j > i; --j) arr[R][j] = k++;

for (int j = R, k = s + cc * 2 + rr; j > i; --j) arr[j][i] = k++;

}

if (small & 1) {

const int i = count;

int k = 2 * i * (row + col - 2 * i) + 1;

if (row <= col) for (int j = i; j < col - i; ++j) arr[i][j] = k++;

else for (int j = i; j < row - i; ++j) arr[j][i] = k++;

}

}

void print_arr(int *arr, int row, int col, int max_col) //col < max_col

{

for (int i = 0, *q = arr; i < row; ++i, q += max_col) {

for (int *p = q; p < q + col; ++p)

std::cout.width(4), std::cout << *p;

std::cout << "\n";

}

std::cout << "\n";

}

void print_arr(int* a[], int row, int col) //col < max_col

{

for (int i = 0; i < row; ++i) {

for (int j = 0; j < col; ++j)

std::cout.width(4), std::cout << a[i][j];

std::cout << "\n";

}

std::cout << "\n";

}

void test_1()

{

const int M = 25;

const int N = 25;

int a[M][N];

int test[][2] = {{5, 5}, {5, 7}, {7, 5}, {4, 4}, {4, 6}, {6, 4}};

const int sz = sizeof(test) / sizeof(test[0]);

std::cout << "Test 1:\n";

for (int i = 0; i < sz; ++i) {

int row = test[i][0];

int col = test[i][1];

if (row < 0 || row > M) row = 3;

if (col < 0 || col > N) col = 3;

::counter = 0;

act_arr(&a[0][0], row, col, N);

print_arr(&a[0][0], row, col, N);

}

}

void test_2()

{

int test[][2] = {{5, 5}, {5, 7}, {7, 5}, {4, 4}, {4, 6}, {6, 4}};

const int sz = sizeof(test) / sizeof(test[0]);

std::cout << "Test 2:\n";

for (int i = 0; i < sz; ++i) {

int row = test[i][0];

int col = test[i][1];

int **arr = new int*[row];

for (int i = 0; i < row; ++i) arr[i] = new int[col];

::counter = 0;

act_arr(arr, row, col);

print_arr(arr, row, col);

for (int i = 0; i < row; ++i) delete[] arr[i];

delete[] arr;

}

}

int main()

{

test_1();

test_2();

}

posted on 2010-12-23 23:09 flyinghearts 阅读(4377) 评论(3)  编辑 收藏 引用 所属分类: 算法

### 评论

# re: 螺旋矩阵 2010-12-24 10:10 _飞寒

# re: 螺旋矩阵 2010-12-25 11:20 flyinghearts
@_飞寒

# re: 螺旋矩阵 2014-08-12 13:57 121e1212
int test[][2] = {{5, 5}, {5, 7}, {7, 5}, {4, 4}, {4, 6}, {6, 4}};

const int sz = sizeof(test) / sizeof(test[0]);

std::cout << "Test 2:\n";

for (int i = 0; i < sz; ++i) {

int row = test[i][0];

int col = test[i][1];

int **arr = new int*[row];

for (int i = 0; i < row; ++i) arr[i] = new int[col];
回复  更多评论