int mat[n][n];
int x,y;

x=y=0;
while(y<n){
 swap(mat[y][x],mat[x][y]);

 y+=(x!=n-1)?0:1;
 x=(x!=n-1)?x+1:0;
}