#include using namespace std; vector> twoDimensionVec; int n; //出发点 int row = 0, col = 0; //初始化方向 int dir = 0; //变更方向 //0:右 //1:左下 //2:下 //3:右上 void changeDir() { switch (dir) { //如果原来是向右 case 0: //检查左下是不是可以走 if (row + 1 < n && col - 1 >= 0) { dir = 1; } else { //左下走不了,走右上 dir = 3; } break; //如果原来是1号,即左下 case 1: //优先向下 if (row + 1 < n) dir = 2; //次选横向 else dir = 0; break; //如果原来是2号,优先右上 //否则 左下 case 2: if (col + 1 < n && row - 1 >= 0) dir = 3; else dir = 1; break; //右上 case 3: //优先向右,次选向下 if (col + 1 < n) dir = 0; else dir = 2; break; } } int main() { cin >> n; for (int i = 0; i < n; i++) { vector oneDimensionVec; for (int j = 0; j < 4; j++) { int c; cin >> c; oneDimensionVec.push_back(c); } twoDimensionVec.push_back(oneDimensionVec); } //第一个位置 cout << twoDimensionVec[row][col] << " "; int count = 0; //如果没有到达出口位置 while (!(row == n - 1 && col == n - 1)) { count++; if (count == 8) { int a = 1; } switch (dir) { case 0: //向右横向移动一格 if (col < n - 1) { col++; cout << twoDimensionVec[row][col] << " "; } break; case 1: //向左下角走一个斜格子 while (col > 0 && row < n - 1) { row++; col--; cout << twoDimensionVec[row][col] << " "; } break; case 2: if (row < n - 1) { row++; cout << twoDimensionVec[row][col] << " "; } break; case 3: while (col < n - 1 && row > 0) { row--; col++; cout << twoDimensionVec[row][col] << " "; } break; } //变更方向 changeDir(); } return 0; }