EOJ 3536 蛇形矩阵

版权声明:Wubba lubba dub dub' https://blog.csdn.net/qq_41444888/article/details/79980084

题目链接

感觉就是一道很水的模拟,当时比赛的时候总是超时,事后听说是找规律?心态崩了心态崩了
题解链接: EOJ 3536
挂一下自己当时的模拟代码,思路还是不够灵活啊。。。!!
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
using namespace std;
long long a[200005];
//int b[200005];
int main()
{
    int n;
    while(scanf("%d",&n) != EOF)
    {
        memset(a, 0, sizeof(a));
        int cnt = 0;
        int now = n - 1;
        int nowhang = 1;
        int nowhang1 = n;
        if(n == 1)
        {
            printf("1\n");
            continue;
        }
        //int flag = 0;
        while(now > 0)
        {
            for(int i = 1; i <= now; i ++)
                a[nowhang] += (++ cnt);
            for(int i = 0; i < now; i ++)
                a[nowhang + i] += (++ cnt);
            for(int i = 1; i <= now; i ++)
                a[nowhang1] += (++ cnt);
            for(int i = 0; i < now; i ++)
                a[nowhang1 - i] += (++cnt);
            nowhang1 --;
            nowhang ++;
            now -= 2;
            if(nowhang1 == nowhang)
            {
                //flag = 1;
                a[nowhang1] += (++cnt);
                break;
            }
            if(nowhang1 < nowhang)
            {
                //flag = 1;
                break;
            }
        }
        for(int i = 1; i <= n; i ++)
            printf("%lld\n",a[i]);
        //cout<<flag;
    }
    return 0;
}

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页