zoj3038

Triangle War II

Time Limit: 5 Seconds      Memory Limit: 32768 KB

Triangle War II is a new game developed by Cannon Studio.

Different from Triangle War I, Triangle War II is a single-player game. This game is based on a triangle of size N. Below is a triangle of size 4:

We call two dots adjacent if they are connected by a dashed line. As you can see, each dot has at least two adjacent dots (e.g. dot 1 has dot 2, 3 as its adjacent dots), and has at most six adjacent dots (e.g. dot 5 has dot 2, 3, 4, 6, 8, 9 as its adjacent dots). In this game, each dot has two states: pushed and unpushed. Each time, you can choose one of the unpushed dots, push it, change it to pushed state, and change all the state of its adjacent dots (change pushed dots to unpushed state and unpushed dots to pushed state).

Given the initial state of the triangle, you can push as many times as you wish, and your goal is to make the triangle containing as many unpushed dots as possible.

Input

There are multiple test cases. There are two parts for each case. The first part is one line with an integer N (1 <= N <= 6), the size of the triangle. The second part consists with N lines. Each line gives the states for one line of dots, in which '.' represents an unpushed dot and '*' represents a pushed dot. There're no more than 64 test cases in the input data.

Output

For each case, print a number in one line, the maximum number of unpushed dots you can get.

Sample Input

2
.
.*
3
*
*.
**.

Sample Output

2

5

 

DLX不会构造,写了个广搜

# include <math.h>
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <algorithm>
# include <iostream>
# include <string>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <vector>
# include <cstring>
# include <list>
# include <ctime>

# define For(i,a)   for((i)=0;i<(a);(i)++)
# define MAX(x,y)   ((x)>(y)? (x):(y))
# define MIN(x,y)   ((x)<(y)? (x):(y))
# define MEM(a)     (memset((a),0,sizeof(a)))
# define MEME(a)    (memset((a),-1,sizeof(a)))
# define MEMX(a)    (memset((a),0x7f,sizeof(a)))

using namespace std;

typedef long long           ll      ;
typedef unsigned long long  ull     ;
typedef unsigned int        uint    ;
typedef unsigned char       uchar   ;

int dir[6][2]={0,-1,0,1,-1,-1,-1,0,1,0,1,1};
char vis[1<<21];
int b[10][10],a[22][2];
int Count(int v)
{
    int num = 0;
    while(v)
    {
        v &= (v-1);
        num++;
    }
    return num;
}
int q[1<<21];
int bfs(int s,int mid)
{
    int f=0,r=0,tmp,u;
    memset(vis,0,sizeof(vis));
    q[r++]=s;
    vis[s]=1;
    while(f<r)
    {
        tmp=u=q[f++];
        if(Count(u)>=mid) return 1;
        while(tmp>0)
        {
            int k=tmp&(-tmp),kk=0;
            int v=u-k;
            while(k>0) kk++,k>>=1;
            int x=a[kk-1][0],y=a[kk-1][1];
            for(int i=0;i<6;i++)
            {
                int xx=x+dir[i][0], yy=y+dir[i][1];
                if(b[xx][yy]==-1) continue;
                v^=(1<<b[xx][yy]);
            }
            if(!vis[v])
            {
                q[r++]=v;
                vis[v]=1;
            }
            tmp-=tmp&(-tmp);
        }
    }
    return 0;
}
int main()
{
    int n,s;
    int i,j,k;
    char ch;
    while(scanf("%d",&n)!=EOF)
    {
        s=k=0;
        memset(b,-1,sizeof(b));
        for(i=1;i<=n;i++)
            for(j=1;j<=i;j++)
            {
                cin>>ch;
                if(ch=='.')
                    s=s|(1<<k);
                a[k][0]=i; a[k][1]=j;
                b[i][j]=k++;
            }
        int left=0, right=(n+1)*n/2;
        int ans=0;
        while(left<=right)
        {
            int mid=(left+right)>>1;
            if(bfs(s,mid))
            {
                ans=mid;
                left=mid+1;
            }
            else
                right=mid-1;
        }
        printf("%d\n",ans);
    }
}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值