CUC-SUMMER-10-D

D - New Year and Fireworks
CodeForces - 750D

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 45degrees (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.

Example
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.


For the second sample, the drawings below show the situation after levels 4, 5 and 6. The middle drawing shows directions of all parts that will move in the next level.


题意:二叉树,经过n次爆炸后的面积

解法:dfs,要记录已经计算过的状态,不然会超时,用300x300的区域来表示坐标

代码:

#include<iostream>
using namespace std;
int n,t[35],cnt=0;
bool vis[310][310][8][5][31];
bool mark[310][310];
int dir[8][2]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};
void dfs(int x,int y,int d,int l,int c)
{
    if(vis[x][y][d][l][c]||c>n)
        return;
    vis[x][y][d][l][c]=1;
    for(int i=0;i<l;i++)
        if(!mark[x+i*dir[d][0]][y+i*dir[d][1]]){
            cnt++;
            mark[x+i*dir[d][0]][y+i*dir[d][1]]=1;
        }
    int d1,d2;
    d1=(d+8-1)%8;
    d2=(d+8+1)%8;
    dfs(x+(l-1)*dir[d][0]+dir[d1][0],y+(l-1)*dir[d][1]+dir[d1][1],d1,t[c+1],c+1);
    dfs(x+(l-1)*dir[d][0]+dir[d2][0],y+(l-1)*dir[d][1]+dir[d2][1],d2,t[c+1],c+1);
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>t[i];
    dfs(150,150,0,t[1],1);
    cout<<cnt<<endl;
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,155评论 0 23
  • 这是个特别的日子,我和老杨夫妻合法化六周年了……时间过得真快,回首这六年,娃生了两个,给自己建了栋房子,一家人买足...
    武娟7886阅读 179评论 0 0
  • 一觉醒来,想起昨晚梦见的好多人。 梦里,时间倒回至大学毕业搬走那一天。 虽然多了一些奇怪的人,但是你们都在, 舍友...
    Jiayi_miao阅读 121评论 0 0
  • 同步关键字用于在多个线程中需要对同一段数据进行访问时候,出现的不安全情况。因为多个线程执行同一段代码会造成数据不安...
    金馆长说阅读 6,076评论 0 1