public class Solution {
//下x++ 左y-- 上x-- 右y++
public void prints(int n) {
int[][] mp = new int[n][n];
for(int i=0; i
for(int j=0; j
mp[i][j] = 0;
}
}
int x , y, tot;
tot = mp[x=0][y=n-1] = 1;
while(tot < n*n) {
while(x+1
while(y-1>=0 && mp[x][y-1]==0) mp[x][--y] = ++tot;
while(x-1>=0 && mp[x-1][y]==0) mp[--x][y] = ++tot;
while(y+1
}
for(int i=0; i
for(int j=0; j
System.out.print(mp[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
Solution solution = new Solution();
solution.prints(8);
}
}
/*
22 23 24 25 26 27 28 1
21 44 45 46 47 48 29 2
20 43 58 59 60 49 30 3
19 42 57 64 61 50 31 4
18 41 56 63 62 51 32 5
17 40 55 54 53 52 33 6
16 39 38 37 36 35 34 7
15 14 13 12 11 10 9 8
*/
标签:Java,int,矩阵,tot,++,蛇形,&&,public,mp
来源: https://www.cnblogs.com/Roni-i/p/10434418.html