Codeforces 750D 记忆化搜索

来源:互联网 发布:网狐三网通源码 编辑:程序博客网 时间:2024/04/28 20:59

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
44 2 2 3
output
39
input
61 1 1 1 1 3
output
85
input
13
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 45 and 6. The middle drawing shows directions of all parts that will move in the next level.


题意:放烟花  每个烟花在经过a[i]个格子后会爆炸  分成两个子烟花  方向分别顺时针和逆时针旋转45度  求经过的总格子数


题解:每个烟花最多延伸5个格子  那么30个烟花最多延伸150个格子  所以染色到的格子最大不超过400*400  而2^30约等于10^9  

10^9>>400*400  所以会有很多重复计算  这时候只要记忆化搜索就行了  


#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int a[405][405],b[32][400][400][8];int dx[8]={1,1,0,-1,-1,-1,0,1},dy[8]={0,1,1,1,0,-1,-1,-1};int main(){int n,i,t=0,d,x,y,z,dd,xx,yy;b[1][200][200][0]=1;scanf("%d",&n);for(i=1;i<=n;i++){scanf("%d",&d);for(x=0;x<400;x++){for(y=0;y<400;y++){for(z=0;z<8;z++){if(b[i][x][y][z]){for(dd=0,xx=x,yy=y;dd<d;dd++){xx+=dx[z],yy+=dy[z];if(!a[xx][yy])t++;5a[xx][yy]=1;}b[i+1][xx][yy][(z+1)&7]=b[i+1][xx][yy][(z+7)&7]=1;}}}}}printf("%d\n",t);return 0;}


0 0