P5731 【深基5.习6】蛇形方阵 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
//为什么用动态二维数组 //动态二维数组怎么用(申明、删除) #include<iostream> #include<cstring> #include<iomanip> using namespace std; int n,x,y,value;//y横向,x纵向 int main() { cin>>n; int **a=new int*[n]; for (size_t i = 0; i < n; i++) { a[i]=new int[n]; memset(a[i],0,n*sizeof(int)); } x=y=0; value=a[0][0]=1; while (value<n*n) { while (y+1<n&&!a[x][y+1])//向右 { //y+1<n是为了不超过边界,a[x][y+1]==0是控制及时拐弯 a[x][++y]=++value; } while (x+1<n&&!a[x+1][y])//向下 { a[++x][y]=++value; } while (y-1>=0&&!a[x][y-1])//向左 { a[x][--y]=++value; } while (x-1>=0&&!a[x-1][y])//向上 { a[--x][y]=++value; } } for (size_t i = 0; i < n; i++) { for (size_t j = 0; j < n; j++) { cout<<setw(3)<<a[i][j]; } cout<<endl; } for (size_t i = 0; i < n; i++) { delete[] a[i]; } delete[] a; return 0; }
标签:P5731,int,value,++,蛇形,include,方阵,size From: https://www.cnblogs.com/shinnyblue/p/17132389.html