Good Bye 2016D. New Year and Fireworks(dfs)

D. New Year and Fireworks

time limit per test:2.5 seconds

memory limit per test:256 megabytes

input:standard input

output:standard output

One tradition of welcoming the New Year is launching fireworks into the sky. Usually a launched firework flies vertically upward for some period of time, then explodes, splitting into several parts flying in different directions. Sometimes those parts also explode after some period of time, splitting into even more parts, and so on.

Limak, who lives in an infinite grid, has a single firework. The behaviour of the firework is described with a recursion depth n and a duration for each level of recursion t1, t2, …, tn. Once Limak launches the firework in some cell, the firework starts moving upward. After covering t1 cells (including the starting cell), it explodes and splits into two parts, each moving in the direction changed by 45 degrees (see the pictures below for clarification). So, one part moves in the top-left direction, while the other one moves in the top-right direction. Each part explodes again after covering t2 cells, splitting into two parts moving in directions again changed by 45 degrees. The process continues till the n-th level of recursion, when all 2n - 1 existing parts explode and disappear without creating new parts. After a few levels of recursion, it’s possible that some parts will be at the same place and at the same time — it is allowed and such parts do not crash.

Before launching the firework, Limak must make sure that nobody stands in cells which will be visited at least once by the firework. Can you count the number of those cells?

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 30) — the total depth of the recursion.

The second line contains n integers t1, t2, …, tn (1 ≤ ti ≤ 5). On the i-th level each of 2i - 1 parts will cover ti cells before exploding.

Output

Print one integer, denoting the number of cells which will be visited at least once by any part of the firework.

Examples

Input
4
4 2 2 3

Output
39

Input
6
1 1 1 1 1 3

Output
85

Input
1
3

Output
3

Note

For the first sample, the drawings below show the situation after each level of recursion. Limak launched the firework from the bottom-most red cell. It covered t1 = 4 cells (marked red), exploded and divided into two parts (their further movement is marked green). All explosions are marked with an ‘X’ character. On the last drawing, there are 4 red, 4 green, 8 orange and 23 pink cells. So, the total number of visited cells is 4 + 4 + 8 + 23 = 39.
题意:烟花在某点开始发射,共进行n个阶段,每个阶段结束后,烟花会一分为二,与原方向成45°,135°。问最后烟花经过的坐标有多少个。
题解:因为范围比较小最大才300,所以无脑暴力dfs,注意一下一分为二的的状态转化就好,详解看代码。
代码:

#include <bits/stdc++.h>
#define ll long long
#define bababaa printf("!!!!!!!\n")
using namespace std;
bool vis[310][310][8][6][32],mp[310][310];
int f[8][2] = {{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}},a[32],n;
void dfs(int x,int y,int fa,int cur,int nl)//横,纵,方向,此阶段所剩长度,阶段数
{
    if(vis[x][y][fa][cur][nl]||nl>n) return ;//如果这个之前就到过这个点并且状态相同,就可以剪枝。
    vis[x][y][fa][cur][nl]=1;
    mp[x][y]=1;
    if(cur)
        dfs(x+f[fa][0],y+f[fa][1],fa,cur-1,nl);
    else
    {
        dfs(x,y,(fa+1)%8,a[nl+1],nl+1);
        dfs(x,y,(fa+8-1)%8,a[nl+1],nl+1);
    }
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    int ans=0;
    dfs(150,150,0,a[1]-1,1);
    for(int i=0;i<=300;i++)
    {
        for(int j=0;j<=300;j++)
        {
            if(mp[i][j]) ans++;
        }
    }
    cout<<ans<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值