CCF—Z字形扫描 20141202

来源:互联网 发布:linnx远程桌面端口开启 编辑:程序博客网 时间:2024/06/18 16:43
#include <iostream>
using namespace std;
//向右移动0  向右上移动1  向下移动2  向左下移动3
int main()
{
    int n,A[500][500];
    cin >> n;
    for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)cin >> A[i][j];
    int row = 0, col = 0;
    int temp=0;
    while (row != n - 1 || col != n - 1)
    {
        cout << A[row][col] << ' ';
        switch (temp)
        {
       case 0:
           col++;
           if (row == 0) temp = 3;
           else temp = 1;
           break;
       case 1:
           row--;
           col++;
           if (row == 0 && col != n - 1)temp = 0;
           else if (col == n - 1)temp = 2;
           else temp = 1;
           break;
       case 2:
           row++;
           if (col == 0)temp = 1;
           else temp = 3;
           break;
       case 3:
           row++;
           col--;
           if (col == 0 && row != n - 1) temp = 2;
           else if (row == n - 1) temp = 0;
           else temp = 3;
           break;
        }
    }
   cout << A[n - 1][n - 1];//保证最后一个元素输出来
    
}
原创粉丝点击