1-4-之字形打印矩阵

1 /*
2     题目描述:
3         对于一个矩阵,请设计一个算法,将元素按“之”字形打印。具体见样例。
4         给定一个整数矩阵mat,以及他的维数nxm,请返回一个数组,其中元素依次为打印的数字。
5         测试样例:
6             [[1,2,3],[4,5,6],[7,8,9],[10,11,12]],4,3
7             返回:[1,2,3,6,5,4,7,8,9,12,11,10]
8 */
 1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4 /*
 5     实现斜之字打印矩阵:
 6     eg:矩阵A为:
 7         1  2  3
 8         4  5  6
 9         7  8  9
10         10 11 12
11     斜之字打印的结果为:
12         [1,2,4,7,5,3,6,8,10,11,9,12]
13 */
14 vector<int> printLine(vector<vector<int> > m, int t1, int t2, int d1, int d2, bool flag){
15     vector<int> line;
16     if (!flag){    // 从右上到左下打印
17         while (t1 <= d1 && t2 >= d2){
18             line.push_back(m[t1++][t2--]);
19         }
20     }
21     else{    // 从左下到右上打印
22         while (t1 <= d1 && t2 >= d2){
23             line.push_back(m[d1--][d2++]);
24         }
25     }
26     return line;
27 }
28 
29 vector<int> printMatrix(vector<vector<int> > mat, int n, int m) {
30     int t1 = 0;
31     int t2 = 0;
32     int d1 = 0;
33     int d2 = 0;
34     bool flag = true;
35     vector<int> rt;
36     while (t1 < n && t2 < m && d1 < n && d2 < m){
37         vector<int> line = printLine(mat, t1, t2, d1, d2, flag);
38         for (int i = 0; i < line.size(); i++)
39             rt.push_back(line[i]);
40         if (t2 == m-1)
41             t1++;
42         else
43             t2++;
44         if (d1 == n-1)
45             d2++;
46         else
47             d1++;
48         flag = !flag;
49     }
50     return rt;
51 }
 1 /*
 2     NowCoder上面的题目较简单。
 3     矩阵A的打印结果为:
 4         [1,2,3,6,5,4,7,8,9,12,11,10]
 5 */
 6 vector<int> printMatrix(vector<vector<int> > mat, int n, int m) {
 7     vector<int> rt;
 8     bool flag = true;
 9     for (int i = 0; i < n; i++){ // 逐行打印
10         if (flag){    // 从左到右打印
11             for (int j = 0; j < m; j++)
12                 rt.push_back(mat[i][j]);
13         }
14         else{    // 从右向左打印
15             for (int j = m-1; j>= 0; j--)
16                 rt.push_back(mat[i][j]);
17         }
18         flag = !flag;
19     }
20     return rt;
21 }
 1 test:
 2 int main(){
 3     vector<vector<int> > matrix;
 4     vector<int> a;
 5     a.push_back(1);
 6     a.push_back(2);
 7     a.push_back(3);/*
 8     a.push_back(4);*/
 9     matrix.push_back(a);
10 
11     vector<int> b;
12     b.push_back(4);
13     b.push_back(5);
14     b.push_back(6);/*
15     b.push_back(8);*/
16     matrix.push_back(b);
17 
18     vector<int> c;
19     c.push_back(7);
20     c.push_back(8);
21     c.push_back(9);/*
22     c.push_back(12);*/
23     matrix.push_back(c);
24 
25     vector<int> d;
26     d.push_back(10);
27     d.push_back(11);
28     d.push_back(12);/*
29     c.push_back(12);*/
30     matrix.push_back(d);
31 
32     vector<int> rt;
33     rt = printMatrix(matrix,4,3);
34     for (int i = 0; i < rt.size(); i++)
35             cout << rt[i] << ",";
36     return 0;
37 }

 

posted @ 2015-10-16 11:12  KindaCrazy  阅读(294)  评论(0编辑  收藏  举报