//Stone 2016/3/21:38:32#include <iostream>#include<vector>using namespacestd;classSolution { Public: voidPrintmatrix (vector<vector<int> > &matrix) { intCount =0; if(Matrix.empty ())return; intUpedge =0; intLeftedge =0; intRightedge = matrix[0].size (); intDownedge =matrix.size (); BOOLEnd =false; CharDirection ='R'; inti =0; intj =0; while(!end) { Switch(direction) { Case 'R': if(J <Rightedge) { while(J <Rightedge) {Matrix[i][j]= count++; J++; } Upedge+=1; Direction='D'; I++; J--; } ElseEnd=true; Break; Case 'D': if(I <Downedge) { while(I <Downedge) {Matrix[i][j]= count++; I++; } Direction='L'; J--; I--; Rightedge-=1; } ElseEnd=true; Break; Case 'L': if(J >=Leftedge) { while(J >=Leftedge) {Matrix[i][j]= count++; J--; } Direction='u'; I--; J++; Downedge-=1; } ElseEnd=true; Break; Case 'u': if(I >=Upedge) { while(I >=Upedge) {Matrix[i][j]= count++; I--; } Leftedge+=1; Direction='R'; J++; I++; } ElseEnd=true; Break; default: Break; } } }};intMain () {intm =0; intn =0; while(Cin >> M >>N) {if(M! =0&& n! =0) {vector<vector<int> > Maxtrix (M, vector<int> (n,0)); classsolution test; Vector<int>result; Test.printmatrix (Maxtrix); for(inti =0; I < m; i++) { for(intj =0; J < N; J + +) {cout<<Maxtrix[i][j]; if(J! = N-1) cout<<" "; } if(I! = M-1) cout<<Endl; } } }}
Prints the matrix clockwise, given the number of matrix rows and matrix columns (starting at 0)