2017 Icpc BeijingF - Secret Poems

mac2022-06-30  35

题意:

给你一个矩阵,然后你要按照要求进行读取,以及输出 (折线读取,蛇形输出)

 

思路:

找到规律

#include <iostream> #include <cstdio> #include <cstring> #include <vector> using namespace std; typedef long long ll; const int maxn = 110; char s[maxn][maxn]; char p[maxn][maxn]; int vis[maxn][maxn]; char c[maxn*maxn]; int x,y,tot,n; int cnt; int main() { while(cin>>n) { memset(s,0,sizeof s); memset(p,0,sizeof p); memset(c,0,sizeof c); for(int i=0; i<n; i++) for(int j=0; j<n; j++) cin>>s[i][j]; p[0][0]=s[0][0]; x=0;y=0; cnt = 0; // 折线读取 for(int i=0; i<2*n; i++) { if(i%2) { for(int j=i; j>=0; j--) if(i-j<n&&j<n) c[cnt++]+=s[i-j][j]; } else { for(int j=0; j<=i; j++) if(i-j<n&&j<n) c[cnt++]+=s[i-j][j]; } } tot=0; while(tot<n*n-1)//紫书上的蛇形填数 { while(y+1<n&&!p[x][y+1])// p[x][++y]=c[++tot]; while(x+1<n&&!p[x+1][y])// p[++x][y]=c[++tot]; while(y-1>=0&&!p[x][y-1])// p[x][--y]=c[++tot]; while(x-1>=0&&!p[x-1][y])// p[--x][y]=c[++tot]; } for(int i=0; i<n; i++) { for(int j=0; j<n; j++) cout<<p[i][j]; cout<<endl; } } return 0; }

 

转载于:https://www.cnblogs.com/Tianwell/p/11552264.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)