一开始还在纠结怎么表示一个状态,毕竟是一个串。后来搜了一下题解发现了这里用一个整数的前12位表示转态就好了 ,1~o,0~'-',每个状态用一个数来表示,然后dp写起来就比较方便了。

代码:

  1. #include <iostream>
  2. #include <sstream>
  3. #include <cstdio>
  4. #include <climits>
  5. #include <cstring>
  6. #include <cstdlib>
  7. #include <string>
  8. #include <cmath>
  9. #include <vector>
  10. #include <queue>
  11. #include <algorithm>
  12. #define esp 1e-6
  13. #define pb push_back
  14. #define in freopen("in.txt", "r", stdin);
  15. #define out freopen("out.txt", "w", stdout);
  16. #define print(a) printf("%d\n",(a));
  17. #define bug puts("********))))))");
  18. #define Rep(i, c) for(__typeof(c.end()) i = c.begin(); i != c.end(); i++)
  19. #define inf 0x0f0f0f0f
  20. using namespace std;
  21. typedef long long LL;
  22. typedef vector<int> VI;
  23. typedef pair<int, int> pii;
  24. typedef vector<pii,int> VII;
  25. typedef vector<int>:: iterator IT;
  26. #define N 50000
  27. int dp[N];
  28. int ans;
  29. void f(int x)
  30. {
  31. if(dp[x])
  32. return ;
  33. int num = ;
  34. for(int i = ; i < ; i++)
  35. if(x&(<<i))
  36. num++;
  37. ans = min(ans, num);
  38. dp[x] = ;
  39. for(int i = ; i <= ; i++)
  40. if(((x&(<<i)) && (x&(<<(i+))) && !(x&(<<(i+))))
  41. ||(!(x&(<<i)) && (x&(<<(i+))) && (x&(<<(i+)))))
  42. f(x^(<<(i+))^(<<i)^(<<(i+)));
  43. }
  44. int main(void)
  45. {
  46.  
  47. char s[];
  48. int T, t;
  49. for(t = scanf("%d", &T), gets(s); t <= T; t++)
  50. {
  51. memset(dp, , sizeof(dp));
  52. gets(s);
  53. int n = ;
  54. ans = ;
  55. for(int i = ; i < ; i++)
  56. if(s[i] - '-')
  57. n ^= (<<i);
  58. f(n);
  59. printf("%d\n", ans);
  60. }
  61. return ;
  62. }

或者可以用map+string的方法,一直都没怎么学过STL,看了http://www.myexception.cn/ai/1243266.html 这里的方法表示又涨了不少知识,拿来重新写了一遍(其实还是“剽窃”,没办法先当一个"搬运工”吧)。

  1. #include <iostream>
  2. #include <sstream>
  3. #include <cstdio>
  4. #include <climits>
  5. #include <cstring>
  6. #include <cstdlib>
  7. #include <string>
  8. #include <cmath>
  9. #include <stack>
  10. #include <map>
  11. #include <vector>
  12. #include <queue>
  13. #include <algorithm>
  14. #define esp 1e-6
  15. #define pb push_back
  16. #define in freopen("in.txt", "r", stdin);
  17. #define out freopen("out.txt", "w", stdout);
  18. #define print(a) printf("%d\n",(a));
  19. #define bug puts("********))))))");
  20. #define Rep(i, c) for(__typeof(c.end()) i = c.begin(); i != c.end(); i++)
  21. #define inf 0x0f0f0f0f
  22. using namespace std;
  23. typedef long long LL;
  24. typedef vector<int> VI;
  25. typedef pair<int, int> pii;
  26. typedef vector<pii,int> VII;
  27. typedef vector<int>:: iterator IT;
  28. map<string, bool> my;
  29. int ans;
  30. void dfs(string cur)
  31. {
  32. if(my.find(cur) != my.end())
  33. return;
  34. int len = cur.size(), num = ;
  35. for(int i = ; i < len; i++)
  36. if(cur[i] == 'o')
  37. num++;
  38. ans = min(ans, num);
  39. my[cur] = true;
  40. for(int i = ; i <= ; i++)
  41. {
  42. if(cur.substr(i, ) == "-oo")
  43. {
  44. string temp = cur;
  45. temp.replace(i, , "o--");
  46. dfs(temp);
  47. }
  48. if(cur.substr(i, ) == "oo-")
  49. {
  50. string temp = cur;
  51. temp.replace(i, , "--o");
  52. dfs(temp);
  53. }
  54. }
  55. }
  56. int main(void)
  57. {
  58. int T;
  59. string s;
  60. for(int t = scanf("%d", &T); t <= T; t++)
  61. {
  62. cin>>s;
  63. my.clear();
  64. ans = ;
  65. dfs(s);
  66. printf("%d\n", ans);
  67. }
  68. return ;
  69. }

UVA 10651 Pebble Solitaire 状态压缩dp的更多相关文章

  1. uva 10651 - Pebble Solitaire(记忆化搜索)

    题目链接:10651 - Pebble Solitaire 题目大意:给出一个12格的棋盘,‘o'代表摆放棋子,’-‘代表没有棋子, 当满足’-oo'时, 最右边的棋子可以跳到最左边的位子,而中间的棋 ...

  2. UVa 10651 Pebble Solitaire(DP 记忆化搜索)

    Pebble Solitaire Pebble solitaire is an interesting game. This is a game where you are given a board ...

  3. UVA 10651 Pebble Solitaire(bfs + 哈希判重(记忆化搜索?))

    Problem A Pebble Solitaire Input: standard input Output: standard output Time Limit: 1 second Pebble ...

  4. UVA 11825 - Hackers&#39; Crackdown 状态压缩 dp 枚举子集

    UVA 11825 - Hackers' Crackdown 状态压缩 dp 枚举子集 ACM 题目地址:option=com_onlinejudge&Itemid=8&page=sh ...

  5. hoj2662 状态压缩dp

    Pieces Assignment My Tags   (Edit)   Source : zhouguyue   Time limit : 1 sec   Memory limit : 64 M S ...

  6. POJ 3254 Corn Fields(状态压缩DP)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4739   Accepted: 2506 Descr ...

  7. [知识点]状态压缩DP

    // 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...

  8. HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP

    题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...

  9. DP大作战—状态压缩dp

    题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...

随机推荐

  1. OC1_类与对象

    // // main.m // OC1_类与对象 // // Created by zhangxueming on 15/6/9. // Copyright (c) 2015年 zhangxuemin ...

  2. Java JDK1.5、1.6、1.7新特性整理(转)

    原文链接:http://www.cnblogs.com/tony-yang-flutter/p/3503935.html 一.Java JDK1.5的新特性 1.泛型: List<String& ...

  3. OpenJudge/Poj 2001 Shortest Prefixes

    1.链接地址: http://bailian.openjudge.cn/practice/2001 http://poj.org/problem?id=2001 2.题目: Shortest Pref ...

  4. mysql大数据高并发处理

    一.数据库结构的设计 如果不能设计一个合理的数据库模型,不仅会增加客户端和服务器段程序的编程和维护的难度,而且将会影响系统实际运行的性能.所以,在一个系统开始实施之前,完备的数据库模型的设计是必须的. ...

  5. plsql通过instantclient连接oracle数据库报连接超时

    配置:数据库oracle10.2;服务器操作系统centos5.5:客户机操作系统win7 32位:plsql版本10.0.5 配置前提必须关闭客户机与服务器操作系统的防火墙,否则会出现“连接超时”的 ...

  6. javascript中的sort()方法

    现在在学习javascript中,发现sort()函数是有点奇怪的东西(可能是本人水平的问题-_-!),于是就在这里记录一下自己找到的东西吧.sort()这个方法的参数很奇怪,必须是函数,但也是可选参 ...

  7. 少年Vince之遐想

    本文999纯水贴,然转载仍需注明: 转载至http://www.cnblogs.com/VinceYang1994/ 昨天去姑姑家拜年,表哥房间的角落里有一架缠有蜘蛛网的遥控直升飞机. 打开飞机及遥控 ...

  8. win7下 mysql主从配置实现

    win7下学习 mysql主从复制 一.环境: 主服务器(master):192.168.1.23 mysql版本:5.5 从服务器(slave):192.168.1.24 mysql版本:5.5   ...

  9. C#对word、excel、pdf等格式文件的操作总结

    一.word 这是我以前工作时写过的一个业务逻辑处理类,里面有不少文件操作的方法,这里主要关注一下C#对word的操作.里面的方法可以直接拿出来用,主要是通过word的dot模版来进行创建word.替 ...

  10. Your branch and 'origin/master' have diverged

    git fetch origin git reset --hard origin/master