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.
                ulral1519 Formula 1

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

Sample Input

Sample1

4 4
**..
....
....
....

Sample2

4 4
....
....
....
....

Sample Output

Sample1

2

Sample2

6

Translation

有一个$n\times m$的棋盘,'$.$'能走,'*'不能走,请问有多少种不同的走法能够经过所有点并且每个每个点不重复并且路径形成一个 回路 ,即有多少个 哈密顿回路

Problem Link

👉vjudge-ural_1519_Formula 1

Thoughts

kuangbin系列插头dp入门题。佩服cdq大佬高中就能够想出这种算法。

#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;
}
评论区
头像