您的位置:首页 > 其它

URAL 1519 Formula 1 插头DP

2016-03-04 11:17 441 查看
开始刷cdq ppt的题。。

不过毕竟插头DP写起来很长,所以为了写短一点我就没有在意运行时间了。。

学到了位运算清零。。

#include <cstdio>
#include <algorithm>
#include <map>
#define rep(i,j,k) for(int i=j;i<k;++i)
using namespace std;
const int N = 20001, M = 5001;
typedef long long ll;
typedef map<ll, ll>::iterator mll;
const int state[] = {0, -1, 1, 0};

int get_bit(int sta, int i) { return (sta >> (i << 1)) & 3; }
int bracket(int sta, int i, int rate) {
int ret = i, cnt = rate;
for (int cnt = rate; cnt; cnt += state[get_bit(sta, ret)]) ret -= rate;
return ret;
}
int left(int sta, int i) { return bracket(sta, i, 1); }
int right(int sta, int i) { return bracket(sta, i, -1); }
int set_bit(int &sta, int i, int x) {
sta = (sta & ~(3 << (i << 1))) | (x << (i << 1));
}

map<ll, ll> hash[2], *cur, *last;
int bx, by, n, m;
char mp[16][16];

void update(int x, int y, int sta, ll tv) {
int l = y == 0 ? 0 : get_bit(sta, y), s;
int t = x == 0 ? 0 : get_bit(sta, y + 1);
#define create(i,j) s=sta,set_bit(s,y,i),set_bit(s,y+1,j),(*cur)[s]+=tv
if (mp[x][y] == '*') {
if (!l && !t) create(0, 0);
return;
}
if (!l && !t) {
if (x != n - 1 && y != m - 1) create(1, 2);
} else if (!l || !t) {
if (x < n - 1) create(l + t, 0);
if (y < m - 1) create(0, l + t);
} else {
s = sta; set_bit(s, y, 0); set_bit(s, y + 1, 0);
if (l == 1 && t == 1) set_bit(s, right(s, y + 1), 1);
else if (l == 1 && t == 2) { if (x != bx || y != by) return; }
else if (l == 2 && t == 1);
else if (l == 2 && t == 2) set_bit(s, left(s, y + 1), 2);
(*cur)[s]+=tv;
}
}

ll solve() {
int sz;
cur = hash; last = hash + 1;
last->clear(); (*last)[0] = 1;
rep(i,0,n) {
int rate = 2;
rep(j,0,m) {
cur->clear();
for(mll k=last->begin();k!=last->end();++k)
update(i, j, k->first << rate, k->second);
rate = 0;
swap(cur, last);
}
}
ll ans = 0;
for(mll k=last->begin();k!=last->end();++k)
if (k->first == 0) {
ans = k->second; break;
}
return ans;
}

int main() {
while (scanf("%d%d", &n, &m) != EOF) {
rep(i,0,n) scanf("%s", mp[i]);
bx = by = -1;
for(int i=n-1;i>=0;--i) for(int j=m-1;j>=0;--j)
if (mp[i][j] == '.') bx = i, by = j, i = -1, j = -1;
printf("%I64d\n", solve());
}
return 0;
}


URAL 1519 Formula 1

Description

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.

Output

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

Sample

Sample 1

Input
4 4
**..
....
....
....
Output
2


Sample 2

Input
4 4
....
....
....
....
Output
6
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: