首页 > 其他分享 >ACW756回形矩阵

ACW756回形矩阵

时间:2023-11-26 22:00:10浏览次数:40  
标签:Scanner int res 矩阵 System dx dy ACW756 回形

点击查看代码

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 res[][]=new int[n][m];
//         回形矩阵
   int dx[] = {-1, 0, 1, 0};int dy[] = {0,1,0,-1};
         int x = 0, y = 0, d = 1;
         for (int i = 1; i <= n*m; i++) {
             res[x][y]=i;
             int a=x+dx[d];int b=y+dy[d];
             if(a<0||a>=n||b<0||b>=m||res[a][b]!=0){
                 d=(d+1)%4;
                 a=x+dx[d];
                 b=y+dy[d];
             }
             x=a;
             y=b;
         }
         for (int i = 0; i < n; i++) {
             for (int j = 0; j < m; j++) {
                 System.out.print(res[i][j]+" ");
             }
             System.out.println();
         }
    }
}

dx,dy方向图,d数组索引
image
伪代码
image
计算
image

标签:Scanner,int,res,矩阵,System,dx,dy,ACW756,回形
From: https://www.cnblogs.com/fhwudi/p/17858048.html

相关文章