{
if (rb == re)
{
if (cb < ce)
{
auto t = M[rb][ce];
for(size_t i = ce; i >= cb+1; --i)
{
M[rb][i] = M[rb][i-1];
}
M[rb][cb] = t;
}
return;
}
if (cb == ce)
{
if (rb < re)
{
auto t = M[re][cb];
for(size_t i = re; i >= rb+1; --i)
{
M[cb][i] = M[cb][i-1];
}
M[rb][cb] = t;
}
return;
}
auto t = M[rb+1][cb];
for(size_t i = rb+1; i <= re - 1; ++i) M[i][cb] = M[i+1][cb];
for(size_t i = cb+1; i <= ce; ++i) M[re][i-1] = M[re][i];
for(size_t i = re-1; i >= rb; --i) M[i+1][ce] = M[i][ce];
for(size_t i = ce-1; i >= cb; --i) M[rb][i+1] = M[rb][i];
M[rb][cb] = t;
++rb; --re; ++cb; --ce;
if (rb <= re && cb <= ce) Rotate(M,rb,re,cb,ce);
}
int main(int argc, const char * argv[])
{
int A[3][4] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
Rotate(A,0,2,0,3);
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 4; ++j)
{
cout << setw(5) << A[i][j];
}
cout << endl;
}
}