ural 1519 Formula 1 插头dp 一条回路
2014-08-13 15:58
351 查看
1519. Formula 1
Time limit: 1.0 secondMemory limit: 64 MB
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 importantthing 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 technicaluniversity!.. 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 rectangleN*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.
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.
Output
You should output the desired number of ways. It is guaranteed, that it does not exceed 263-1.Samples
input | output |
---|---|
4 4 **.. .... .... .... | 2 |
4 4 .... .... .... .... | 6 |
#include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> const int MAXD=15; const int HASH=30007; const int STATE=1000010; using namespace std; int N,M,cas=1; int maze[MAXD][MAXD]; int code[MAXD]; int ch[MAXD]; int ex,ey; struct HASHMAP { int head[HASH],next[STATE],size; long long state[STATE]; long long f[STATE]; void init(){ size=0; memset(head,-1,sizeof(head)); } void push(long long st,long long ans){ int i; int h=st%HASH; for(i=head[h];i!=-1;i=next[i]) if(state [i]==st){ f[i]+=ans; return; } state[size]=st; f[size]=ans; next[size]=head[h]; head[h]=size++; } }hm[2]; void decode(int *code,int m,long long st) { for(int i=m;i>=0;i--){ code[i]=st&7; st>>=3; } } long long encode(int *code,int m) { int cnt=1; memset(ch,-1,sizeof(ch)); ch[0]=0; long long st=0; for(int i=0;i<=m;i++){ if(ch[code[i]]==-1)ch[code[i]]=cnt++; code[i]=ch[code[i]]; st<<=3; st|=code[i]; } return st; } void shift(int *code,int m) { for(int i=m;i>0;i--)code[i]=code[i-1]; code[0]=0; } void dpblank(int i,int j,int cur) { int k,left,up; for(k=0;k<hm[cur].size;k++){ decode(code,M,hm[cur].state[k]); left=code[j-1]; up=code[j]; if(left&&up){ if(left==up){ if(i==ex&&j==ey){ code[j-1]=code[j]=0; if(j==M)shift(code,M); hm[cur^1].push(encode(code,M),hm[cur].f[k]); } } else{ code[j-1]=code[j]=0; for(int t=0;t<=M;t++) if(code[t]==up)code[t]=left; if(j==M)shift(code,M); hm[cur^1].push(encode(code,M),hm[cur].f[k]); } } else if(left||up){ int t=up; if(left)t=left; if(maze[i][j+1]){ code[j-1]=0; code[j]=t; hm[cur^1].push(encode(code,M),hm[cur].f[k]); } if(maze[i+1][j]){ code[j-1]=t; code[j]=0; if(j==M)shift(code,M); hm[cur^1].push(encode(code,M),hm[cur].f[k]); } } else{ if(maze[i][j+1]&&maze[i+1][j]){ code[j-1]=code[j]=13; hm[cur^1].push(encode(code,M),hm[cur].f[k]); } } } } void dpblock(int i,int j,int cur) { int k; for(k=0;k<hm[cur].size;k++) { decode(code,M,hm[cur].state[k]); code[j-1]=code[j]=0; if(j==M)shift(code,M); hm[cur^1].push(encode(code,M),hm[cur].f[k]); } } char s[15]; void init() { ex=0; memset(maze,0,sizeof(maze)); for(int i=1;i<=N;i++){ scanf("%s",&s); for(int j=0;j<M;j++){ if(s[j]=='.'){ maze[i][j+1]=1; ex=i,ey=j+1; } } } } void solve() { int i,j,cur=0; long long ans=0; hm[cur].init(); hm[cur].push(0,1); for(i=1;i<=N;i++) for(j=1;j<=M;j++){ hm[cur^1].init(); if(maze[i][j])dpblank(i,j,cur); else dpblock(i,j,cur); cur^=1; } for(i=0;i<hm[cur].size;i++) ans+=hm[cur].f[i]; printf("%I64d\n",ans); } int main() { while(scanf("%d%d",&N,&M)!=EOF){ init(); solve(); } return 0; }
相关文章推荐
- HDU 1693 Eat the Trees(插头DP、棋盘哈密顿回路数)+ URAL 1519 Formula 1(插头DP、棋盘哈密顿单回路数)
- 【BZOJ1814】Ural 1519 Formula 1 插头DP
- ural 1519 Formula 1(插头dp)
- [URAL1519]-Formula 1-插头DP
- Ural1519 Formula 1 插头dp入门
- URAL - 1519 Formula 1 (插头DP)
- Ural 1519 Formula 1 插头DP(单回路)
- [ural1519]Formula 1 && 插头DP(括号表示法)
- URAL 1519 Formula 1 【插头DP模板题】
- [BZOJ]1814 Ural 1519 Formula 1 插头DP
- Ural1519 Formula 1 插头dp
- URAL 1519 Formula 1(插头DP,入门题)
- ural 1519 Formula 1(插头dp)
- URAL1519 Formula 1(插头dp的基础题 ——详细解释)
- URAL1519 Formula 1 【插头dp】
- ural 1519 Formula 1(轮廓线|插头DP|括号配对)
- bzoj 1814: Ural 1519 Formula 1 插头dp经典题
- URAL 1519 Formula 1(插头DP)
- 【Ural1519】Formula 1 插头DP模板
- URAL 1519 Formula 1 (插头DP,常规)