zoukankan      html  css  js  c++  java
  • 23:二维数组回形遍历

    23:二维数组回形遍历

    总时间限制: 
    1000ms
     
    内存限制: 
    65536kB
    描述

    给定一个row行col列的整数数组array,要求从array[0][0]元素开始,按回形从外向内顺时针顺序遍历整个数组。如图所示:

    输入
    输入的第一行上有两个整数,依次为row和col。
    余下有row行,每行包含col个整数,构成一个二维整数数组。
    (注:输入的row和col保证0 < row < 100, 0 < col < 100)
    输出
    按遍历顺序输出每个整数。每个整数占一行。
    样例输入
    4 4
    1 2 3 4
    12 13 14 5
    11 16 15 6
    10 9 8 7
    样例输出
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    来源
    北京大学2009年医学部练习题
    这道题的关键,在于
    1.如何找到四个方向
    2.四个方向分别应该怎么走
    3.方向转换的判断条件
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 using namespace std;
     6 int a[10001][10001];
     7 int b[10001][10001];
     8 int now=4;// 1上 2下 3左 4右 
     9 int tot=1;
    10 int main()
    11 {
    12     int n,m;
    13     cin>>n>>m;
    14     int i=1,j=1;
    15     for(int i=1;i<=n;i++)
    16     {
    17         for(int j=1;j<=m;j++)
    18         {
    19             cin>>a[i][j];
    20         }
    21     }
    22     cout<<a[1][1]<<endl;
    23     b[1][1]=1;
    24     while(tot!=n*m)
    25     {
    26         if((j==m&&now==4)||(b[i][j+1]==1&&now==4))
    27             now=2;//
    28             if((i==n&&now==2)||(b[i+1][j]==1&&now==2))
    29             now=3;//
    30             if((j==1&&now==3)||(b[i][j-1]==1&&now==3))
    31             now=1;//
    32             if(b[i-1][j]==1&&now==1)
    33             now=4;//
    34             if(now==1)
    35             {
    36                 cout<<a[i-1][j]<<endl;
    37                 b[i-1][j]=1;
    38                 i--;
    39                 tot++;
    40             }//
    41             if(now==2)
    42             {
    43                 cout<<a[i+1][j]<<endl;
    44                 b[i+1][j]=1;
    45                 tot++;
    46                 i++;
    47             }//
    48             if(now==3)
    49             {
    50                 cout<<a[i][j-1]<<endl;
    51                 b[i][j-1]=1;
    52                 tot++;
    53                 j--;
    54             }//
    55             if(now==4)
    56             {
    57                 cout<<a[i][j+1]<<endl;
    58                 b[i][j+1]=1;
    59                 tot++;
    60                 j++;
    61             }//
    62     }
    63         
    64     return 0;
    65 }
  • 相关阅读:
    数据库
    java语法
    《Lucene实战(第2版)》 配书代码在IDEA下的编译方法
    lucene学习
    社交关系调研(费)
    微博开发平台java SDK demo学习之examples(demo)
    微博开发平台java SDK demo学习之friendships
    F. Classical? (数论 + 思维 + 推理 + 容斥)
    石子合并问题,经典区间DP
    luoguP2048 [NOI2010]超级钢琴
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/6511946.html
Copyright © 2011-2022 走看看