1. 程式人生 > >ural1519 Formula 1

ural1519 Formula 1

Background

Regardless of the fact, that Vologda could not get rights to hold the Winter Olympic games of 20**, it is well-known, that the city will conduct one of the Formula 1 events. Surely, for such an important thing a new race circuit should be built as well as hotels, restaurants, international airport - everything for Formula 1 fans, who will flood the city soon. But when all the hotels and a half of the restaurants were built, it appeared, that at the site for the future circuit a lot of gophers lived in their holes. Since we like animals very much, ecologists will never allow to build the race circuit over the holes. So now the mayor is sitting sadly in his office and looking at the map of the circuit with all the holes plotted on it.

Problem

Who will be smart enough to draw a plan of the circuit and keep the city from inevitable disgrace? Of course, only true professionals - battle-hardened programmers from the first team of local technical university!.. But our heroes were not looking for easy life and set much more difficult problem: "Certainly, our mayor will be glad, if we find how many ways of building the circuit are there!" - they said.

It should be said, that the circuit in Vologda is going to be rather simple. It will be a rectangle N* M cells in size with a single circuit segment built through each cell. Each segment should be parallel to one of rectangle's sides, so only right-angled bends may be on the circuit. At the picture below two samples are given for N

= M = 4 (gray squares mean gopher holes, and the bold black line means the race circuit). There are no other ways to build the circuit here.

Problem illustration

Input

The first line contains the integer numbers N and M (2 ≤ N, M ≤ 12). Each of the next N lines contains M characters, which are the corresponding cells of the rectangle. Character "." (full stop) means a cell, where a segment of the race circuit should be built, and character "*" (asterisk) - a cell, where a gopher hole is located. There are at least 4 cells without gopher holes.

Output

You should output the desired number of ways. It is guaranteed, that it does not exceed 2 63-1.

Example

input output
4 4
**..
....
....
....
2
4 4
....
....
....
....
6

kuangbin說是插頭dp入門題,可折煞我了233,看了半天題解又寫了半天不過A了還是很高興的,特此放一下ac程式碼留以紀念,雖然根本沒法看

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<queue>
using namespace std;
typedef long long ll ;
ll a[1594323+10];
ll newa[1594323+10];
char mg[15][15];
queue<int>QQ[2]; 
bool flag[1594323+10];
int my_min(int a,int b)
{
    return a<b ?a:b;
}
int main()
{
    int n,m;
    scanf("%d %d",&n,&m);
    {
        memset(newa,0,sizeof(newa));
        memset(a,0,sizeof(a));
        memset(flag,false,sizeof(flag));
        int tp=1;
        for(int i=0;i<=m;i++)
            tp*=3;
        int y=tp/3;
        int x=y/3;
        a[0]=1;
        int r,c;
        r=c=-1;
        QQ[0].push(0);
        for(int i=0;i<n;i++)
        {
            scanf("%s",mg[i]);
            for(int k=0;k<m;k++)
            {
               
                if(mg[i][k]=='.')
            	{
            		r=i;
					c=k; 
				} 
            }
        }
   //     printf("%d %d\n",r,c);
        if(r==-1)
            cout<<0<<endl;
        else
        {
            for(int i=0;i<n;i++)
                for(int k=0;k<m;k++)
                {
                        while(!QQ[0].empty())
                        {
                        	int p=QQ[0].front() ;
                        	QQ[0].pop();
                            int sjt=p%3;
                            int zjt=p/y;
                            int np=(p%y)/3;
                            if(mg[i][k]=='*')
                            {
                                if(sjt==zjt && sjt==0)
                                {
									newa[np]+=a[p];
                                	if(flag[np]==false)
                                	{
                                		flag[np]=true;
                                		QQ[1].push(np);
									}
                                    
                                }
                            }
                            else
                            {
                                if((sjt==0 &&zjt!=0 ) || (sjt!=0 && zjt==0))
                                {
                                    int ljt;
                                    if(zjt!=0)
                                        ljt=zjt;
                                    else
                                        ljt=sjt;
                                    newa[np+ljt*x]+=a[p];
                                    if(flag[np+ljt*x]==false)
                                	{
                                		flag[np+ljt*x]=true;
                                		QQ[1].push(np+ljt*x);
									}
                                    if(k!=m-1)
                                    {
                                        newa[np+ljt*y]+=a[p];
                                    	if(flag[np+ljt*y]==false)
	                                	{
	                                		flag[np+ljt*y]=true;
	                                		QQ[1].push(np+ljt*y);
										}
									}
                                }
                                else
                                    if(sjt==0 && zjt==0)
                                    {
                                        if(k!=m-1)
                                        {
											newa[np+y*2+x*1]+=a[p];
											if(flag[np+y*2+x*1]==false)
	                                		{
	                                			flag[np+y*2+x*1]=true;
	                                			QQ[1].push(np+y*2+x*1);
											}
										}
                                            
                                    }
                                    else
                                    {
                                        if(!(i==r && k==c))
                                        {
                                            if(!(sjt==2 && zjt==1))
                                            {
                                                if(sjt==2 && zjt==2)
                                                {
                                                    int yy=y;
                                                    int zkh=0;
                                                    int ykh=1;
                                                    while(1)
                                                    {
                                                        if((np%(yy*3))/yy==1)
                                                            zkh++;
                                                        if((np%(yy*3))/yy==2)
                                                            ykh++;
                                                        if(ykh==zkh)
                                                            break;
                                                        yy/=3;
                                                    }
                                                    newa[np+yy]+=a[p];
													if(flag[np+yy]==false)
			                                		{
			                                			flag[np+yy]=true;
			                                			QQ[1].push(np+yy);
													}
                                                }
                                                else
                                                    if(sjt==1 && zjt==1)
                                                    {
                                                        int yy=1;
                                                        int zkh=1;
                                                        int ykh=0;
                                                        while(1)
                                                        {
                                                            if((np%(yy*3)/yy)==1)
                                                                zkh++;
                                                            if((np%(yy*3)/yy)==2)
                                                                ykh++;
                                                            if(ykh==zkh)
                                                                break;
                                                            yy*=3;
                                                        }
                                                        newa[np-yy]+=a[p];
														if(flag[np-yy]==false)
				                                		{
				                                			flag[np-yy]=true;
				                                			QQ[1].push(np-yy);
														}
                                                    }
                                                    else
                                                    {
												
                                                        newa[np]+=a[p];
                                                        if(flag[np]==false)
				                                		{
				                                			flag[np]=true;
				                                			QQ[1].push(np);
														}
                                                    }
                                            }
                                        }
                                        else
                                        {
                                            newa[np]+=a[p];
                                            if(flag[np]==false)
				                            {
				                                flag[np]=true;
				                                QQ[1].push(np);
											}
                                        }
                                    }
                            }
                            a[p]=0;
                        }
                    while(!QQ[1].empty())
					{
						int num=QQ[1].front();
						QQ[1].pop();
						a[num]=newa[num];
						newa[num]=0;
						flag[num]=false;
						QQ[0].push(num);	
					}	
                }
            cout<<a[0]<<endl;
        }
    }
    return 0;
}