hdu1584(状态压缩DP)
蜘蛛牌
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2068 Accepted Submission(s): 844
蛛牌是windows
xp操作系统自带的一款纸牌游戏,游戏规则是这样的:只能将牌拖到比她大一的牌上面(A最小,K最大),如果拖动的牌上有按顺序排好的牌时,那么这些牌也
跟着一起移动,游戏的目的是将所有的牌按同一花色从小到大排好,为了简单起见,我们的游戏只有同一花色的10张牌,从A到10,且随机的在一行上展开,编
号从1到10,把第i号上的牌移到第j号牌上,移动距离为abs(i-j),现在你要做的是求出完成游戏的最小移动距离。
每组数据有一行,10个输入数据,数据的范围是[1,10],分别表示A到10,我们保证每组数据都是合法的。
1 2 3 4 5 6 7 8 9 10
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#define maxn 20
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
int d[<<maxn]; //
int a[maxn];
int n;
int Min(int x,int y)
{
if(x<=y)
return x;
else
return y;
}
void init()
{
for(int i=;i<(<<maxn);i++)
{
d[i]=inf;
}
for(int i=;i<;i++)
d[(<<i)]=; }
int dist[maxn][maxn];
int p[maxn];
int pos[maxn];
void work()
{
for(int s=;s<(<<);s++)
{
int i,j;
for(i=;i<;i++)
if(s & (<<i))
{
int MIN=inf,ID=-;
for(j=;j<;j++)
{
if(s & (<<j))
{
if(i-j<=)
continue;
if(i-j<=MIN)
{
MIN=i-j;
ID=j;
}
}
}
if(ID!=-)
d[s]=Min(d[s],d[s ^ (<<ID)]+dist[i][ID]);
}
}
}
void input()
{
for(int i=;i<;i++)
{
scanf("%d",&a[i]);
a[i]--;
pos[a[i]]=i;
}
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
dist[i][j]=abs(pos[i]-pos[j]);
}
// cout<<endl;
} }
int main()
{
// freopen("test.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)
{
init();
input();
// solve();
work();
printf("%d\n",d[(<<)-]);
}
return ;
}
hdu1584(状态压缩DP)的更多相关文章
- 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
题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...
- 状态压缩dp问题
问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...
- BZOJ-1226 学校食堂Dining 状态压缩DP
1226: [SDOI2009]学校食堂Dining Time Limit: 10 Sec Memory Limit: 259 MB Submit: 588 Solved: 360 [Submit][ ...
- Marriage Ceremonies(状态压缩dp)
Marriage Ceremonies Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- HDU 1074 (状态压缩DP)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:有N个作业(N<=15),每个作业需耗时,有一个截止期限.超期多少天就要扣多少 ...
随机推荐
- poj 1163 数塔
#include<stdio.h> #include<string.h> #define N 110 int dp[N][N]; int a[N][N]; int Max(in ...
- React学习之State
本文基于React v16.4.1 初学react,有理解不对的地方,欢迎批评指正^_^ 一.定义组件的两种方式 1.函数定义组件 function Welcome(props) { return & ...
- 2018 11.1 PION 模拟赛
期望:250 100+100+50 实际:210 80+100+30 期望:100 实际:80 最后:两个点T了.可能是求逆元的方法太慢了,也可能是闲的又加了一个快速乘的原因. #inclu ...
- Spring Data JPA 入门篇
Spring Data JPA是什么 它是Spring基于ORM框架(如hibernate,Mybatis等).JPA规范(Java Persistence API)封装的一套 JPA应用框架,可使开 ...
- DELPHI最新的产品路线图
1)根据众多像您一样的客户要求,我们改为一年一个重大版本及更多更新.这个计划回到一年发布周期并提供额外的2或3个包含附加功能及支持期间发布的新版操作系统的更新. 2)在 RAD Studio 10. ...
- send-mail: fatal: parameter inet_interfaces: no local interface found for ::1
转载:http://blog.csdn.net/csdnones/article/details/50717934 发送邮件: [root@iZ23whn33jnZ log]# echo '这是邮件标 ...
- linux定时重启节约内存
linux服务器上运行的一些程序,比较消耗内存,需要定时重启,进行内存定期释放 0 2 * * * sudo /sbin/reboot && echo $(date) '重启成功' ...
- Ubuntu更换主板之后 网络重新配置
Ubuntu更换主板之后,网络不能用,需要重新配置 1. 首要要查看新主板的mac地址, dmesg | grep eth 2. 修改网络信息,该配置文件是/etc/udev/rules.d, 文 ...
- 解决安装oracle11g r2时提示pdksh conflicts with ksh-20100621-2.el6.i686问题
http://blog.csdn.net/linghao00/article/details/7943740 http://www.2cto.com/os/201306/218566.html 在Ce ...
- ubuntu 16.04上安装php5.6
php --ini 按下面的步骤,在ubuntu 16.04上面安装成功了 php5.6 dpkg -l | grep php| awk '{print $2}' |tr "\n" ...