公司面试题目 打印矩阵,先往右打印,到头然后向左下打印,到头然后向左打印,最后向上打印

来源:互联网 发布:留学中介机构排名知乎 编辑:程序博客网 时间:2024/05/17 01:09

2014 amazon 公司面试题目 打印矩阵,

1)从该矩阵的最左上点往右水平打印,到最右列后然后向左下(45°夹角)打印,到最下的一列后,再水平向左打印,到达最左一列后,最后垂直向上打印。

2)再重复以上step1 动作,直到打印完所有满条件的节点。


./amazon_online_matric

//input

5 3

1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

 

//output

1 2 3 4 5 9 13 12 11 6 7 8

 

另外一个测试用例

//input

3 5

1 2 3

4 5 6

7 8 9

10 11 12

13 14 15

//output

1 2 3 5 7 4

 

Third test case

//input

4 4

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

C++代码如下:

#include < map >#include < set >#include < list >#include < cmath >#include < ctime >#include < deque >#include < queue >#include < stack >#include < bitset >#include < cstdio >#include < limits >#include < vector >#include < cstdlib >#include < numeric >#include < sstream >#include < iostream >#include < algorithm >using namespace std;map printednodeMap; map::iterator it; vector outdata; int **data=NULL; int Matrixwidth,Matrixheight;void firstprint(int x1,int y1,int x2, int y2){       if(x1 >= Matrixwidth || y1 >= Matrixheight)        return;    int row,column;   //to right   for(column=x1;column<=x2;column++){    if(printednodeMap[y1*Matrixwidth+column]==true) {       //printed        break;    }           outdata.push_back(data[y1][column]);     printednodeMap[y1*Matrixwidth+column]=true;  }  //to leftdown  column-=2;  row=y1+1;  while((column>=x1)&&(row<=y2)) {    if(printednodeMap[row*Matrixwidth+column]==true){        //printed       //return;        break;      }        outdata.push_back(data[row][column]);     printednodeMap[row*Matrixwidth+column]=true;    column--;    row++;  }      row--;     column++;    //to left  while((column-1) >= x1){      column--;      if(printednodeMap[row*Matrixwidth+column]==true){      //printed         return;      }     outdata.push_back(data[row][column]);     printednodeMap[row*Matrixwidth+column]=true;      }  //  to up   row--;    while(row>=y1){      if(printednodeMap[row*Matrixwidth+column]==true){        //printed       break;             }           outdata.push_back(data[row][column]);      printednodeMap[row*Matrixwidth+column]=true;      row--;     }    firstprint(x1+1,y1+1,x2-1,y2-1);}vector convertMatrix(int width, int height, vectormatrix) {   Matrixwidth=width;  Matrixheight=height;    //make **data  data=new int *[height];  //make a trial to generate ** of string[][]  //BTW define int data[height][width]; is also OK in C++  string **dtm=NULL;  dtm=new string *[height];   for(int index=0;index    data[index]=new int[width];    dtm[index]=new string[width];  } // OK to define here dynamically // int data[height][width];  int i,j; for(i=0;i    for(j=0;j        data[i][j]=matrix[ i* width + j];    } } for(i=0;i  printednodeMap[i]=false;   } //first print  firstprint(0,0,width-1,height-1);   return outdata;   }int main() {    vector < int > res;    int _width;    cin >> _width;       int _height;    cin >> _height;       vector _matrix;    int _matrix_item;    for(int _matrix_i=0; _matrix_i<_width * _height; _matrix_i++) {        cin >> _matrix_item;        _matrix.push_back(_matrix_item);    }       res = convertMatrix(_width, _height, _matrix);    for(int res_i=0; res_i < res.size(); res_i++) {     cout << res[res_i] << " ";    }       return 0;}


0 0
原创粉丝点击