题目描述
blablabla
样例
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
int arr[][]=new int[n][m];
int index=1;
int dr=n-1;
int dc=m-1;
int tr=0,tc=0;
while(tr<=dr&&tc<=dc){
index=rota(arr,tr++,tc++,dr--,dc--,index);
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
public static int rota(int arr[][],int tr,int tc,int dr,int dc,int index){
if(tr==dr){
/*while(tc!=dc){
arr[dr][tc++]=index++;
}*/
for(int i=tc;i<=dc;i++){
arr[tr][i]=index++;
}
}else if(tc==dc){
for(int i=tr;i<=dr;i++){
arr[i][tc]=index++;
}
}else {
int curC = tc;
int curR = tr;
while (curC != dc) {
arr[tr][curC] = index++;
curC++;
}
while (curR != dr) {
arr[curR][dc] = index++;
curR++;
}
while (curC != tc) {
arr[dr][curC] = index++;
curC--;
}
while (curR != tr) {
arr[curR][tc] = index++;
curR--;
}
}
return index;
}
}
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度分析:blablabla
C++ 代码
blablabla
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度分析:blablabla
C++ 代码
blablabla