Blue Bridge cup-shaped back access simulation

  Basic exercises meandering access  
time limit: 1.0s memory limit: 512.0MB
   
problems described
  meandering fetch access is along side of the matrix, if the current direction has numerous desirable or take off, then 90 degrees left. A start left corner of the matrix, the downward direction.
Input format
  input of the first row is a positive integer of not more than two of m 200, n, denotes the matrix of rows and columns. Next, each row of n row m is an integer, denotes the matrix.
Output format
  output a single line, a total of mn number, taken as an input matrix meandering number of results obtained. Between the number separated by a space, end of the line do not have extra spaces.
Sample input
. 3. 3
. 1 2. 3
. 4. 5. 6
. 7. 8. 9
Sample Output
147,896,325
sample input
. 3 2
. 1 2
. 3. 4
. 5. 6
Sample Output
135642

Ideas: the use of cycle can, where you can take advantage of an array of bool inner control it go, because through the outer ring are marked up

#include<iostream>
using namespace std;
const int N = 210;
int g[N][N];
bool flag[N][N];
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;++i){
        for(int j=1;j<=m;++j){
            cin>>g[i][j];
        }
    }
    cout<<g[1][1];//结尾不能有多余空格,这里对第一个数做特殊处理
    int x=1,y=1;
    flag[x][y]=true;
    int num=1;
    while(num<n*m){
        while(x+1<=n&&!flag[x+1][y])cout<<" "<<g[++x][y],flag[x][y]=true,num++;
        while(y+1<=m&&!flag[x][y+1])cout<<" "<<g[x][++y],flag[x][y]=true,num++;
        while(x-1>=1&&!flag[x-1][y])cout<<" "<<g[--x][y],flag[x][y]=true,num++;
        while(y-1>=1&&!flag[x][y-1])cout<<" "<<g[x][--y],flag[x][y]=true,num++;
    } 
    return 0;
}

Guess you like

Origin www.cnblogs.com/clear-love/p/11295017.html