[URAL1519]Formula 1
Posted xjr_01
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[URAL1519]Formula 1相关的知识,希望对你有一定的参考价值。
[URAL1519]Formula 1
试题描述
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.
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 \times 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.
一个 \(N \times M\)($ \le 12$)的棋盘,有的格子存在障碍,求经过所有非障碍格子的哈密顿回路个数。
输入
The first line contains the integer numbers \(N\) and \(M\) (\(2 \le N, M \le 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.
输出
You should output the desired number of ways. It is guaranteed, that it does not exceed \(2^{63}-1\).
输入示例1
4 4
**..
....
....
....
输出示例1
2
输入示例2
4 4
....
....
....
....
输出示例2
6
数据规模及约定
对于 100% 的数据,\(0 < c_i \le p \le 10\); \(0 < u_i,v_i,d_i \le n \le 1000\); \(0 \le m \le 3000\); \(0 \le w_i \le 20000\)。
题解
裸的轮廓线 dp,练练括号序列表示法。
注意这题的坑,比如开 long long,最后连成的必须是一个连通分量等。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;
#define rep(i, s, t) for(int i = (s); i <= (t); i++)
#define dwn(i, s, t) for(int i = (s); i >= (t); i--)
int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == ‘-‘) f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - ‘0‘; c = getchar(); }
return x * f;
}
#define maxn 16
#define maxs 1594333
#define LL long long
int n, m;
LL f[2][maxs];
bool islst[maxn][maxn];
char Map[maxn][maxn];
void encode(int& s, int A[]) {
s = 0;
dwn(i, m + 1, 1) s = s * 3 + A[i];
return ;
}
void decode(int A[], int s) {
rep(i, 1, m + 1) A[i] = s % 3, s /= 3;
return ;
}
int main() {
n = read(); m = read();
rep(i, 1, n) scanf("%s", Map[i] + 1);
dwn(i, n, 1) {
bool has = 0;
dwn(j, m, 1) if(Map[i][j] == ‘.‘){ has = islst[i][j] = 1; break; }
if(has) break;
}
int all = 1, cur = 0, A[maxn], half[maxn], S[maxn], top;
rep(i, 1, m + 1) all *= 3; all--;
f[cur][0] = 1;
rep(i, 1, n) rep(j, 1, m) {
memset(f[cur^1], 0, sizeof(f[cur^1]));
rep(s, 0, all) if(f[cur][s]) {
decode(A, s);
// printf("f[(%d, %d)][", i, j); rep(k, 1, m + 1) printf("%d%c", A[k], k < m + 1 ? ‘ ‘ : ‘]‘); printf(" = %d\n", f[cur][s]);
top = 0;
rep(k, 1, m + 1) {
if(A[k] == 1) S[++top] = k;
if(A[k] == 2) half[S[top]] = k, half[k] = S[top--];
}
int ts;
if((A[j] || A[j+1]) && Map[i][j] == ‘*‘) continue;
if(j < m) {
if(A[j] && A[j+1]) {
int a = half[j], b = half[j+1]; if(a > b) swap(a, b);
if(A[j] == 1 && A[j+1] == 2 && !islst[i][j]) continue;
A[a] = 1; A[b] = 2;
A[j] = A[j+1] = 0;
encode(ts, A);
f[cur^1][ts] += f[cur][s];
}
else if(A[j]) {
A[j] = 3 - A[half[j]];
encode(ts, A);
f[cur^1][ts] += f[cur][s];
A[j] = 0;
A[j+1] = 3 - A[half[j]];
encode(ts, A);
f[cur^1][ts] += f[cur][s];
}
else if(A[j+1]) {
A[j+1] = 3 - A[half[j+1]];
encode(ts, A);
f[cur^1][ts] += f[cur][s];
A[j+1] = 0;
A[j] = 3 - A[half[j+1]];
encode(ts, A);
f[cur^1][ts] += f[cur][s];
}
else {
if(Map[i][j] == ‘*‘) f[cur^1][s] += f[cur][s];
else A[j] = 1, A[j+1] = 2, encode(ts, A), f[cur^1][ts] += f[cur][s];
}
}
else {
if(A[j] && A[j+1]) {
int a = half[j], b = half[j+1]; if(a > b) swap(a, b);
if(A[j] == 1 && A[j+1] == 2 && !islst[i][j]) continue;
A[a] = 1; A[b] = 2;
A[j] = A[j+1] = 0;
dwn(k, m + 1, 2) A[k] = A[k-1]; A[1] = 0;
encode(ts, A);
f[cur^1][ts] += f[cur][s];
}
else if(A[j]) {
A[j] = 3 - A[half[j]];
dwn(k, m + 1, 2) A[k] = A[k-1]; A[1] = 0;
encode(ts, A);
f[cur^1][ts] += f[cur][s];
}
else if(A[j+1]) {
A[j+1] = 0;
A[j] = 3 - A[half[j+1]];
dwn(k, m + 1, 2) A[k] = A[k-1]; A[1] = 0;
encode(ts, A);
f[cur^1][ts] += f[cur][s];
}
else if(Map[i][j] == ‘*‘) {
dwn(k, m + 1, 2) A[k] = A[k-1]; A[1] = 0;
encode(ts, A);
f[cur^1][ts] += f[cur][s];
}
}
}
cur ^= 1;
}
printf("%lld\n", f[cur][0]);
return 0;
}
以上是关于[URAL1519]Formula 1的主要内容,如果未能解决你的问题,请参考以下文章
bzoj1814: Ural 1519 Formula 1 2011-12-20
BZOJ1814Ural 1519 Formula 1 插头DP