ural 1519 Formula 1(轮廓线|插头DP|括号配对)
来源:互联网 发布:路由器网速控制软件 编辑:程序博客网 时间:2024/04/29 03:37
题意:给你一个m * n的棋盘,有的格子是障碍,问共有多少条回路使得经过每个非障碍格子恰好一次.m, n ≤ 12。
如图,m = n = 4,(1, 1), (1, 2)是障碍,共有2条满足要求的回路.
http://blog.sina.com.cn/s/blog_51cea4040100gmky.html
ps:一直做到五点然而一会还有课,快被自己感动哭了。。。。。。
#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<iostream>#include<algorithm>#include<vector>#include<map>#include<queue>#include<stack>#include<string>#include<map>#include<set>#include<ctime>#define eps 1e-6#define LL long long#define pii pair<int, int>//#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;//const int maxn = 100 + 5;//const int INF = 0x3f3f3f3f;const int hashSize = 2000000;//总状态有3^13个,size取得大有利于减小冲突,但是会增加清空内存的时间。 int Hash[hashSize]; int n, m, index, ex, ey;int total[2], state[2][hashSize], Bit[100];LL dp[2][hashSize], ans;int Map[15][15];void init() {memset(Map, 0, sizeof Map);index = 0;total[0] = 1;state[0][1] = 0;dp[index][1] = 1;ans = 0;}void hashCal(int s, LL num) {int hashPos = s % hashSize;while(Hash[hashPos] != -1) {if(state[index][Hash[hashPos]] == s) {dp[index][Hash[hashPos]] += num;return;}hashPos++;if(hashPos==hashSize) hashPos = 0;}total[index]++;dp[index][total[index]] = num;Hash[hashPos] = total[index];state[index][total[index]] = s;}void DP() {for(int i = 1; i <= n; i++) {for(int k = 1; k <= total[index]; k++) state[index][k] <<= 2;for(int j = 1; j <= m; j++) {index ^= 1;total[index] = 0;memset(Hash, -1, sizeof Hash);for(int k = 1; k <= total[index^1]; k++) {int s = state[index^1][k];LL num = dp[index^1][k];int p = (s>>Bit[j-1]) % 4;int q = (s>>Bit[j]) % 4;//if(num==0) cout << i << " " << j << " " << s << endl;if(!Map[i][j]) {if(p+q == 0) hashCal(s, num);} else if(p+q == 0) {if(!Map[i+1][j] || !Map[i][j+1]) continue;s += (1<<Bit[j-1]) + (1<<(Bit[j]+1));hashCal(s, num);}else if(!p && q) {if(Map[i][j+1]) hashCal(s, num);if(Map[i+1][j]) {s += (1<<Bit[j-1])*q - (1<<Bit[j])*q;hashCal(s, num);}}else if(!q && p) {if(Map[i+1][j]) hashCal(s, num);if(Map[i][j+1]) {s += (1<<Bit[j])*p - (1<<Bit[j-1])*p;hashCal(s, num);}}else if(p+q == 2) { //合并连通块 int b = 1; //寻找最近的右括号 for(int t = j+1; t <= m; t++) {int v = (s>>Bit[t]) % 4;if(v == 1) b++;if(v == 2) b--;if(!b) {s -= (1<<Bit[t]);break;} }s = s - (1<<Bit[j-1])-(1<<Bit[j]); hashCal(s, num);}else if(p+q == 4) {int b=1; for(int t = j - 2; t >= 0; t--){//寻找最近的匹配括号 int v = (s>>Bit[t]) % 4; if(v == 2) ++b; if(v == 1) --b; if(!b){ s += (1<<Bit[t]);//将左括号变为右括号 break; } } s = s - 2*(1<<Bit[j-1]) - 2*(1<<Bit[j]); hashCal(s, num);}else if(p==2 && q==1) {s = s - (1<<(Bit[j-1]+1)) - (1<<(Bit[j]));hashCal(s, num);}else if(p==1 && q==2) {//cout << i << j << endl;if(i==ex && j==ey) ans += num;//cout << ans << endl;}}}}}int main() { //freopen("input.txt", "r", stdin);for(int i = 0; i <= 25; i++) Bit[i] = i<<1; //求第i个插头需要右移的位置 while(scanf("%d%d", &n, &m)==2) {init();char op;for(int i = 1; i <= n; i++) {getchar();for(int j = 1; j <= m; j++) {scanf("%c", &op);if(op=='.') Map[i][j] = 1, ex = i, ey = j;} }//cout << ex << ey << endl;DP();printf("%I64d\n", ans);} return 0;}
0 0
- ural 1519 Formula 1(轮廓线|插头DP|括号配对)
- Ural 1519 Formula 1 插头DP
- ural 1519 Formula 1(插头dp)
- 【URAL 1519】【插头dp模板】Formula 1
- URAL - 1519 Formula 1 (插头DP)
- URAL 1519 Formula 1 插头DP
- URAL 1519 Formula 1 dp(插头)
- URAL 1519 Formula 1(插头DP)
- Ural 1519 Formula --插头DP
- URAL 1519 Formula 1 (插头)
- ural 1519 Formula 1 插头dp 一条回路
- [省选前题目整理][URAL 1519]Formula 1(插头DP)
- URAL 1519 Formula 1 【插头DP模板题】
- [BZOJ]1814 Ural 1519 Formula 1 插头DP
- Ural 1519. Formula 1 ------ 插头dp
- Ural 1519. Formula 1 优美的插头DP
- Ural 1519. Formula 1 优美的插头DP
- [ural1519]Formula 1 && 插头DP(括号表示法)
- OpenGL教程
- Java 反射,通过反射调用所有的方法,包括private方法
- opengl 学习笔记(1)setting up opengl
- Bitwise AND of Numbers Range
- Biometric-----Face Recognition
- ural 1519 Formula 1(轮廓线|插头DP|括号配对)
- Android Api Demos登顶之路(六十一)Content-->PickContacts
- 【小熊刷题】Binary Tree Right Side View <Leetcode 199, Java>
- Leetcode: Search a 2D Matrix II
- Leetcode: Different Ways to Add Parentheses
- Leetcode: Median of Two Sorted Arrays
- Leetcode: Combinations
- Struts2中action获取request、response、session的方式
- Get Session in Spring AOP