7 条题解

  • 3
    @ 2024-9-7 12:53:07

    python递归有上限,so

    import sys

    sys.setrecursionlimit(3000)

    import sys
    sys.setrecursionlimit(3000)
    n=int(input())
    f=[[0 for i in range(100)]for j in range(100)]
    def out(x,y,cnt):
        cnt+=1
        if x>n or y>n or x<1 or y<1 or cnt>n*n:
            return
        f[x][y]=cnt
        if x==1 and y!=n:
            out(n,y+1,cnt)
        elif y==n and x!=1:
            out(x-1,1,cnt)
        elif x==1 and y==n:
            out(x+1,y,cnt)
        elif x!=1 and y!=n:
            if f[x-1][y+1]==0:
                out(x-1,y+1,cnt)
            else:   
                out(x+1,y,cnt)
    f[1][n//2+1]=1
    out(1,(n+1)//2,0)
    for i in range(1,n+1):
        for j in range(1,n+1):
            print(f[i][j],end=' ')
        print('')
    
    • 3
      @ 2024-9-6 10:57:10
      n=int(input())
      a=[[0 for i in range(n+10)] for j in range(n+10)]
      #开二维数组
      posx=[0]*40*40
      posy=[0]*40*40
      #posx,posy记录第i个数的x,y坐标
      a[1][n//2+1]=1
      posx[1]=1
      posy[1]=n//2+1
      for i in range (2,n*n+1):
          if posx[i-1]==1 and posy[i-1]!=n:
              a[n][posy[i-1]+1]=i
              posx[i]=n
              posy[i]=posy[i-1]+1
          elif posx[i-1]!=1 and posy[i-1]==n:
              a[posx[i-1]-1][1]=i
              posx[i]=posx[i-1]-1
              posy[i]=1
          elif posx[i-1]==1 and posy[i-1]==n:
              a[posx[i-1]+1][posy[i-1]]=i
              posx[i]=posx[i-1]+1
              posy[i]=posy[i-1]
          elif posx[i-1]!=1 and posy[i-1]!=n:
              if a[posx[i-1]-1][posy[i-1]+1]==0:
                  a[posx[i-1]-1][posy[i-1]+1]=i
                  posx[i]=posx[i-1]-1
                  posy[i]=posy[i-1]+1
              else:
                  a[posx[i-1]+1][posy[i-1]]=i
                  posx[i]=posx[i-1]+1
                  posy[i]=posy[i-1]
      #简单易懂更符合新高二宝宝体质的for判断
      #(虽然很丑
      for i in range(1,n+1):
          for j in range(1,n+1):
              print(a[i][j],end=' ')
          print()
      
      • 2
        @ 2023-4-6 20:39:57
        #include<bits/stdc++.h>
        using namespace std;
        const int N = 50;
        int a[N][N],n,now;
        void dfs(int x,int y){
            if(now==n*n+1)return;
            a[x][y]=++now;
            if(x==1 && y!=n) dfs(n,y+1);
            if(y==n && x!=1) dfs(x-1,1);
            if(x==1 && y==n) dfs(2,y);
            if(x!=1 && y!=n){
                if(a[x-1][y+1])dfs(x+1,y);
                else dfs(x-1,y+1);
            }
        }
        int main(){
            cin>>n;
            dfs(1,(n+1)/2);
            for(int i=1;i<=n;i++){
                for(int j=1;j<=n;j++)cout<<a[i][j]<<' ';
                puts("");
            }
            return 0;
        }
        
        • 1
          @ 2023-7-17 9:34:33
          #include <bits/stdc++.h>
          using namespace std;
          
          const int N = 40;
          
          int m[N][N],n,t;
          
          void dfs(int x,int y)
          {
          	if(t == n*n+1) return ;
          	t++;
          	m[x][y] = t;
          	if(x == 1 && y!=n) dfs(n,y+1);
          	else if(x!=1 && y == n) dfs(x-1,1);
          	else if(x == 1 && y == n) dfs(x+1,y);
          	else if(x!=1 && y!=n)
          	{
          		if(m[x-1][y+1]) dfs(x+1,y);
          		else dfs(x-1,y+1);
          	}
          }
          
          int main()
          {
          	cin >> n;
          	dfs(1,(n+1)/2);
          	for(int i=1;i<=n;i++)
          	{
          		for(int j=1;j<=n;j++)
          			cout << m[i][j] << " ";
          		cout << endl;
          	}
          	return 0;
          }
          
          • 0
            @ 2024-9-20 16:27:08

            根据题意模拟即可,while判断

            n=int(input())
            a=[[0 for i in range(100)]for j in range(100)]
            prei=1
            prej=n//2+1
            posj=0
            posi=0
            a[1][n//2+1]=1
            k=2
            while(k<=n*n):
               if(prei==1 and prej!=n):
                   posi=n
                   posj=prej+1
               elif(prei!=1 and prej==n):
                   posi=prei-1
                   posj=1
               elif(prei==1 and prej==n):
                   posi=prei+1
                   posj=prej
               elif(prei!=1 and prej!=n):
                   if(a[prei-1][prej+1]==0):
                       posi=prei-1
                       posj=prej+1
                   else:
                       posj=prej
                       posi=prei+1
               prei=posi
               prej=posj
               a[posi][posj]=k
               k+=1
            for i in range(1,n+1):
                for j in range(1,n+1):
                    print(a[i][j],end=" ")
                print()
            
            • -1
              @ 2024-9-4 23:45:57

              n=int(input())

              p=[[0 for i1 in range(n)] for i2 in range(n)]

              p[0][(n-1)//2]=1

              def t(x):

              for z in range(n):

              for c in range(n):

              if p[z][c]==x:

              return [z,c]

              #t(x)=[x所在行,x所在列]

              for k in range(2,n**2+1,1):

              j=k-1

              if t(j)[0]==0 and t(j)[1]!=n-1:

              p[n-1][t(j)[1]+1]=k

              elif t(j)[1]==n-1 and t(j)[0]!=0:

              p[t(j)[0]-1][0]=k

              elif t(j)[0]==0 and t(j)[1]==n-1:

              p[1][n-1]=k

              else:#if t(j)[0]!=0 and t(j)[1]!=n-1:

              if p[t(j)[0]-1][t(j)[1]+1]==0:

              p[t(j)[0]-1][t(j)[1]+1]=k

              else:

              p[t(j)[0]+1][t(j)[1]]=k

              for i in p:

              op=''

              for d in i:

              op+=str(d)+' '

              print(op)

              • -4
                @ 2024-9-7 12:50:37

                python版本的深搜!

                sys.setrecursionlimit(3000)
                def dfs(x,y,q):
                    if q==n*n+1:
                        return 0
                    a[x][y]=q
                    q=q+1
                    if x==1 and y!=n:
                        dfs(n,y+1,q)
                    if x!=1 and y==n:
                        dfs(x-1,1,q)
                    if x==1 and y==n:
                        dfs(x+1,y,q)
                    if x!=1 and y!=n:
                        if a[x-1][y+1]==0:
                            dfs(x-1,y+1,q)
                        else:
                            dfs(x+1,y,q)
                    return 0
                
                n=int(input())
                a=[[0 for i in range(n+2)] for j in range(n+2)]
                q=1
                p=(n+1)//2
                dfs(1,p,q)
                for i in range(1,n+1):
                    for j in range(1,n+1):
                        print(a[i][j],end=' ')
                    print()
                
              • 1

              信息

              ID
              156
              时间
              1000ms
              内存
              128MiB
              难度
              4
              标签
              递交数
              138
              已通过
              68
              上传者