#include<iostream>
#define R 4
#define C 4
using namespace std;
int main()
{
int mat[][C]={
{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16}
};
int top=0;
int down=R-1;
int left=0;
int right=C-1;
while(true)
{
//print first row
for(int i=left;i<=right;++i) cout<<mat[top][i]<<" ";
top++;
if(top>down || left>right) break;
//print last column
for(int i=top;i<=down;++i) cout<<mat[i][right]<<" ";
right--;
if(top>down || left>right) break;
//print last row
for(int i=right;i>=left;--i) cout<<mat[down][i]<<" ";
down--;
if(top>down || left>right) break;
//print first column
for(int i=down;i>=top;--i) cout<<mat[i][left]<<" ";
left++;
if(top>down || left>right) break;
}
return 0;
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNkZWZpbmUgUiA0CiNkZWZpbmUgQyA0CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmludCBtYWluKCkKewoKICAgIGludCBtYXRbXVtDXT17CiAgICAgICAgICAgICAgICAgIHsxLDIsMyw0fSwKICAgICAgICAgICAgICAgICAgezUsNiw3LDh9LAogICAgICAgICAgICAgICAgICB7OSwxMCwxMSwxMn0sCiAgICAgICAgICAgICAgICAgIHsxMywxNCwxNSwxNn0KICAgICAgICAgICAgICAgICB9OwoKICAgIGludCB0b3A9MDsKICAgIGludCBkb3duPVItMTsKICAgIGludCBsZWZ0PTA7CiAgICBpbnQgcmlnaHQ9Qy0xOwogICAgCiAgICB3aGlsZSh0cnVlKQogICAgewogICAgICAgIAogICAgICAgIC8vcHJpbnQgZmlyc3Qgcm93CiAgICAgICAgZm9yKGludCBpPWxlZnQ7aTw9cmlnaHQ7KytpKSBjb3V0PDxtYXRbdG9wXVtpXTw8IiAiOwogICAgICAgIHRvcCsrOwogICAgICAgIAogICAgICAgIGlmKHRvcD5kb3duIHx8IGxlZnQ+cmlnaHQpIGJyZWFrOwogICAgICAgIC8vcHJpbnQgbGFzdCBjb2x1bW4KICAgICAgICBmb3IoaW50IGk9dG9wO2k8PWRvd247KytpKSBjb3V0PDxtYXRbaV1bcmlnaHRdPDwiICI7CiAgICAgICAgcmlnaHQtLTsKICAgICAgICAKICAgICAgICBpZih0b3A+ZG93biB8fCBsZWZ0PnJpZ2h0KSBicmVhazsKICAgICAgICAvL3ByaW50IGxhc3Qgcm93CiAgICAgICAgZm9yKGludCBpPXJpZ2h0O2k+PWxlZnQ7LS1pKSBjb3V0PDxtYXRbZG93bl1baV08PCIgIjsKICAgICAgICBkb3duLS07CiAgICAgICAgCiAgICAgICAgaWYodG9wPmRvd24gfHwgbGVmdD5yaWdodCkgYnJlYWs7CiAgICAgICAgLy9wcmludCBmaXJzdCBjb2x1bW4KICAgICAgICBmb3IoaW50IGk9ZG93bjtpPj10b3A7LS1pKSBjb3V0PDxtYXRbaV1bbGVmdF08PCIgIjsKICAgICAgICBsZWZ0Kys7CiAgICAgICAgaWYodG9wPmRvd24gfHwgbGVmdD5yaWdodCkgYnJlYWs7CiAgICB9CiAgICByZXR1cm4gMDsKfQ==