UVA 10651 Pebble Solitaire 状态压缩dp
一开始还在纠结怎么表示一个状态,毕竟是一个串。后来搜了一下题解发现了这里用一个整数的前12位表示转态就好了 ,1~o,0~'-',每个状态用一个数来表示,然后dp写起来就比较方便了。
代码:
#include <iostream>
#include <sstream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#define esp 1e-6
#define pb push_back
#define in freopen("in.txt", "r", stdin);
#define out freopen("out.txt", "w", stdout);
#define print(a) printf("%d\n",(a));
#define bug puts("********))))))");
#define Rep(i, c) for(__typeof(c.end()) i = c.begin(); i != c.end(); i++)
#define inf 0x0f0f0f0f
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii,int> VII;
typedef vector<int>:: iterator IT;
#define N 50000
int dp[N];
int ans;
void f(int x)
{
if(dp[x])
return ;
int num = ;
for(int i = ; i < ; i++)
if(x&(<<i))
num++;
ans = min(ans, num);
dp[x] = ;
for(int i = ; i <= ; i++)
if(((x&(<<i)) && (x&(<<(i+))) && !(x&(<<(i+))))
||(!(x&(<<i)) && (x&(<<(i+))) && (x&(<<(i+)))))
f(x^(<<(i+))^(<<i)^(<<(i+)));
}
int main(void)
{ char s[];
int T, t;
for(t = scanf("%d", &T), gets(s); t <= T; t++)
{
memset(dp, , sizeof(dp));
gets(s);
int n = ;
ans = ;
for(int i = ; i < ; i++)
if(s[i] - '-')
n ^= (<<i);
f(n);
printf("%d\n", ans);
}
return ;
}
或者可以用map+string的方法,一直都没怎么学过STL,看了http://www.myexception.cn/ai/1243266.html 这里的方法表示又涨了不少知识,拿来重新写了一遍(其实还是“剽窃”,没办法先当一个"搬运工”吧)。
#include <iostream>
#include <sstream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
#include <queue>
#include <algorithm>
#define esp 1e-6
#define pb push_back
#define in freopen("in.txt", "r", stdin);
#define out freopen("out.txt", "w", stdout);
#define print(a) printf("%d\n",(a));
#define bug puts("********))))))");
#define Rep(i, c) for(__typeof(c.end()) i = c.begin(); i != c.end(); i++)
#define inf 0x0f0f0f0f
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii,int> VII;
typedef vector<int>:: iterator IT;
map<string, bool> my;
int ans;
void dfs(string cur)
{
if(my.find(cur) != my.end())
return;
int len = cur.size(), num = ;
for(int i = ; i < len; i++)
if(cur[i] == 'o')
num++;
ans = min(ans, num);
my[cur] = true;
for(int i = ; i <= ; i++)
{
if(cur.substr(i, ) == "-oo")
{
string temp = cur;
temp.replace(i, , "o--");
dfs(temp);
}
if(cur.substr(i, ) == "oo-")
{
string temp = cur;
temp.replace(i, , "--o");
dfs(temp);
}
}
}
int main(void)
{
int T;
string s;
for(int t = scanf("%d", &T); t <= T; t++)
{
cin>>s;
my.clear();
ans = ;
dfs(s);
printf("%d\n", ans);
}
return ;
}
UVA 10651 Pebble Solitaire 状态压缩dp的更多相关文章
- uva 10651 - Pebble Solitaire(记忆化搜索)
题目链接:10651 - Pebble Solitaire 题目大意:给出一个12格的棋盘,‘o'代表摆放棋子,’-‘代表没有棋子, 当满足’-oo'时, 最右边的棋子可以跳到最左边的位子,而中间的棋 ...
- UVa 10651 Pebble Solitaire(DP 记忆化搜索)
Pebble Solitaire Pebble solitaire is an interesting game. This is a game where you are given a board ...
- UVA 10651 Pebble Solitaire(bfs + 哈希判重(记忆化搜索?))
Problem A Pebble Solitaire Input: standard input Output: standard output Time Limit: 1 second Pebble ...
- UVA 11825 - Hackers' Crackdown 状态压缩 dp 枚举子集
UVA 11825 - Hackers' Crackdown 状态压缩 dp 枚举子集 ACM 题目地址:option=com_onlinejudge&Itemid=8&page=sh ...
- hoj2662 状态压缩dp
Pieces Assignment My Tags (Edit) Source : zhouguyue Time limit : 1 sec Memory limit : 64 M S ...
- POJ 3254 Corn Fields(状态压缩DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4739 Accepted: 2506 Descr ...
- [知识点]状态压缩DP
// 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...
- HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP
题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...
- DP大作战—状态压缩dp
题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...
随机推荐
- 快速了解Scala技术栈
http://www.infoq.com/cn/articles/scala-technology/ 我无可救药地成为了Scala的超级粉丝.在我使用Scala开发项目以及编写框架后,它就仿佛凝聚成为 ...
- 20150503-struts2入门-标签
一.几个标签介绍 1.property标签 property标签用于输出指定值: <s:set name="name" value="'kk'" /> ...
- Android——简单音乐播放器
使用MediaPlayer做的简单音乐播放器,更多内容请到百度经验查看 http://jingyan.baidu.com/article/60ccbceb63452364cab197f1.html ...
- TCP/IP协议三次握手与四次握手流程解析(转载及总结)
原文地址:http://www.2cto.com/net/201310/251896.html,转载请注明出处: TCP/IP协议三次握手与四次握手流程解析 一.TCP报文格式 TCP/IP协议的详 ...
- 使用 vmstat 监测系统性能
在linux/unix下,vmstat是常用的系统性能监测工具.常用用法如下 vmstat 1 10 表示以1秒为间隔,做相关参数的采样,一共10次.输出范例如下: procs ----------- ...
- ASP.NET 常识
1..NET是什么? .Net全称.NET Framework是一个开发和运行环境, 该战略是微软的一项全新创意, 它将使得"互联网行业进入一 ...
- aspx文件移动到新建的文件夹中设置路径的问题
项目中仅仅把aspx移动到想要的文件夹内是会出错的,不用想也知道是路径问题.这里我就说这个路径该如何去修改. 两个地方需要修改:1.母版路径修改方法: <link href="Styl ...
- Easyui tabs的herf和content属性
在backstage.jsp页面中我写了一个方法,用于在指定位置添加面板(id为msg的地方)function addTab(t,h){ if($('#msg').tabs('exists',t)){ ...
- Java多线程同步代码块
/*多线程的安全问题1.为什么会出现安全问题?因为程序在运行时,会出现一个线程在判断条件满足后,具备了执行资格,但没有运行代码后一个线程也判断了条件,也具备了执行资格,后一个线程运行了代码,但这时候, ...
- ZOJ 1202 Divide and Count(排列组合)
Divide and Count 题目大意:给定箱子的数量和每个箱子的容量,在每个箱子里都装满对应容量的宝石,每颗宝石都是独一无二的,求一共有多少种放置方式.但是如果两个箱子的容量相同,则认为是 同一 ...