7 solutions

  • 2
    @ 2024-4-8 16:42:44

    从1位置起始,到头就改变方向,直至数字达到n*m停止

    #include <bits/stdc++.h>
    using namespace std;
    
    const int N = 110;
    int mp[N][N];
    
    int main() {
        int n, m;
        scanf("%d %d", &n, &m);
    
        int posx = 1, posy = m;
        int num = 1;
        int direction = 1;
    
        while (num <= n * m) {
            mp[posx][posy] = num++;
            if (direction == 1) {
                if (posy < m && !mp[posx][posy + 1]) posy++;
                else {
                    direction = 2;
                    posx++;
                }
            } else if (direction == 2) {
                if (posx < n && !mp[posx + 1][posy]) posx++;
                else {
                    direction = 3;
                    posy--;
                }
            } else if (direction == 3) {
                if (posy > 1 && !mp[posx][posy - 1]) posy--;
                else {
                    direction = 4;
                    posx--;
                }
            } else {
                if (posx > 1 && !mp[posx - 1][posy]) posx--;
                else {
                    direction = 1;
                    posy++;
                }
            }
        }
    
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                printf("%5d", mp[i][j]);
            }
            printf("\n");
        }
    
        return 0;
    }
    
    
    • 0
      @ 2024-4-18 20:09:49
      #include <iostream>
      #include <cstring>
      #include <algorithm>
      using namespace std;
      
      const int N = 110;
      int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
      int g[N][N];
      int n, m;
      
      int main()
      {
          cin >> n >> m;
          int d = 2, x = 0, y = m - 1;
          for (int i = 1; i <= n * m; i ++ )
          {
              g[x][y] = i;
              int a = x + dx[d], b = y + dy[d];
              if(a < 0 || a >= n || b < 0 || b >= m || g[a][b])
              {
                  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 ++ )
                  printf("%5d", g[i][j]);
              cout << endl;
          }
              
          return 0;
      }
      
      • 0
        @ 2024-4-14 21:33:25
        #include <cstdio>
        #include <cmath>
        #include<iostream>
        using namespace std;
        int a[200][200];
        int flag[200][200];
        int main(){
        	int m,n;
        	cin>>m>>n;
        	int x=n,y=1;
        	int count=1;
        	
        	int num=1;
        	while(num<=n*m){
        		while(y<=m&&flag[x][y]!=1&&num<=n*m){
        			flag[x][y]=1;
        			a[x][y]=num;
        			num++;
        			y++;
        		}
        		x--;
        		y--;
        		while(x>=1&&flag[x][y]!=1&&num<=n*m){
        			flag[x][y]=1;
        			a[x][y]=num;
        			num++;
        			x--;
        		}
        		x++;
        		y--;
        		while(y>=1&&flag[x][y]!=1&&num<=n*m){
        			flag[x][y]=1;
        			a[x][y]=num;
        			num++;
        			y--;
        		}
        		y++;
        		x++;
        		while(x<=n&&flag[x][y]!=1&&num<=n*m){
        			flag[x][y]=1;
        			a[x][y]=num;
        			num++;
        			x++;
        		}
        		x--;
        		y++;
        		
        	}
        	for(int i=1;i<=m;++i){
        		for(int j=1;j<=n;++j){
        			printf("%5d",a[j][i]);
        		}
        		cout<<endl;
        	}
        	return 0;
        }
        
        
        • 0
          @ 2024-4-12 17:07:28
          //
          #include <iostream>
          #include <cstdio>
          
          using namespace std;
          int res[100][100];
          
          int main()
          {
          	int m,n;
          	cin>>m>>n;
          	int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
          	//偏移量
          	for(int x=0,y=n-1,d=0,k=1; x<m,y<n,k<=m*n ;k++)
          	{
          		res[x][y]=k;
          		int a=x+dx[d],b=y+dy[d];
          		if(a<0||a>=m||b>=n||b<0||res[a][b])
          //res[a][b]是重复格子的情况,初始化数组为零,如果走过一遍格子会有数字,需要转弯
          		{
          			d=(d+1)%4;
          		a=x+dx[d],b=y+dy[d];
          	}//看是否撞墙或者走到重复的格子
          		x=a,y=b;//没有的话就更新位置
          	}
          	
          	for(int i=0;i<m;i++)
          	{
          		for(int j=0;j<n;j++)
          		{
          			printf("%5d",res[i][j]);
          		}
          		cout<<endl;
          	}
          	return 0;
          }
          
          • 0
            @ 2024-4-11 20:39:23

            重点理解while循环的内容,理解蛇形填数的排布

            #include<bits/stdc++.h>
            
            using namespace std;
            
            const int N=110;
            
            int a[N][N];
            int m,n;   //矩阵行、列
            int x,y;  //数字 1 的坐标
            int number; // 记录数字的个数 
            
            int main(){
            	cin>>m>>n; 
            	
            	memset(a,0,sizeof(a));
            	
            	//初始化 1 的坐标  (0,n-1)
            	x=0;  y=n-1; 
            	a[x][y]=1;
            	
            	number=1;
            	
            	while(number<m*n){  //顺时针填写数字 
            		while(x+1 < m && !a[x+1][y]) a[++x][y] = ++number;  //从上到下:x轴++,y轴不变 :x++,number++,a[x][y]=number;
            		while(y-1 >= 0 && !a[x][y-1]) a[x][--y] = ++number; //从右到左:x轴不变,y轴--
            		while(x-1>=0 && !a[x-1][y]) a[--x][y] = ++number; //从下到上 :x轴--,y轴不变  
            		while(y+1<n && !a[x][y+1]) a[x][++y] = ++number; //从左到右 :x轴不变,y轴++
            	}
            	
            	//打印出来 
            	for(int i=0;i<m;i++){
            		for(int j=0;j<n;j++)
            			printf("%5d",a[i][j]);
            		cout<<endl;
            	}
            	cout<<endl;
            	return 0;
            }
            
            • 0
              @ 2024-4-11 14:22:17

              #include<bits/stdc++.h> using namespace std; typedef long long ll; #define endl '\n'

              int main() { int m,n; scanf("%d %d",&m,&n); int dir=1; vector<vector<int>> arr(m,vector<int>(n)); // int arr[100][100]; int x,y,bound_x,bound_y; bound_x=m; bound_y=n; x = 0; y = n-1; int cir = 0; for(int cnt=1;cnt<=m*n;cnt++) {

              if(dir==1)
              	{
              		arr[x][y] = cnt;
              		x++;
              		if(x == (bound_x-cir))
              		{
              			dir = 2;
              			x--;
              			y--;
              		}
              	}
              	else if(dir==2)
              	{
              		arr[x][y] = cnt;
              		y--;
              		if(y == (cir-1))
              		{
              			dir = 3;
              			y++;
              			x--;
              		}
              	}
              	else if(dir==3)
              	{
              		arr[x][y] = cnt;
              		x--;
              		if(x == (cir -1))
              		{
              			dir =4;
              			x++;
              			y++;
              		}
              	}
              	else if(dir ==4 )
              	{
              		arr[x][y] = cnt;
              		y++;
              		if(y == (n-cir-1)) 
              		{
              			dir = 1;
              			y--;x++;
              			cir++;
              		}
              	}
              }
              for(int i = 0; i < m;i++)
              {
              	for(int j = 0;j < n;j++)
              	{
              		printf("%5d",arr[i][j]);
              	}
              	cout<<endl;
              }
              
              return 0;
              

              }

              • 0
                @ 2024-4-10 11:52:14
                #include <bits/stdc++.h>
                
                using namespace std;
                
                
                
                const int N = 1010;
                
                int a[N][N];
                
                
                
                int main() {
                
                    int m, n;
                
                    cin >> m >> n;
                
                	//初始位置为右上角
                
                    int y = n;
                
                    int x = 1;
                
                
                    int cnt = 1;
                
                
                
                    while (cnt <= m \* n) {
                
                
                        // 下
                
                        while (x <= m && a[x][y] == 0) {
                
                            a[x][y] = cnt++;
                
                            x++;
                
                        }
                
                        x--;//这里x--是因为最后下走的时候x多加了一次 下面同理
                
                        y--;
                
                
                
                        // 左
                
                        while (y >= 1 && a[x][y] == 0) {
                
                            a[x][y] = cnt++;
                
                            y--;
                
                        }
                
                        y++;
                
                        x--;
                
                
                
                        // 上
                
                        while (x >= 1 && a[x][y] == 0) {
                
                            a[x][y] = cnt++;
                
                            x--;
                
                        }
                
                        x++;
                
                        y++;
                
                
                
                        // 右
                
                        while (y <= n && a[x][y] == 0) {
                
                            a[x][y] = cnt++;
                
                            y++;
                
                        }
                
                        y--;
                
                        x++;
                
                    }
                
                
                
                    // 输出
                
                    for(int i = 1;i <= m;i++)
                
                    {	
                
                    	for(int j = 1;j <= n;j++)
                
                    	{
                
                    		cout << setw(5) << a[i][j];	
                
                    	}
                
                    	cout << '\\n';
                
                    }
                
                    return 0;
                
                }
                
                • 1

                Information

                ID
                78
                Time
                1000ms
                Memory
                256MiB
                Difficulty
                7
                Tags
                # Submissions
                667
                Accepted
                171
                Uploaded By