HDU--1584--蜘蛛牌--深搜版本

蜘蛛牌

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1476    Accepted Submission(s): 569

Problem Description

蜘蛛牌是windows xp操作系统自带的一款纸牌游戏,游戏规则是这样的:只能将牌拖到比她大一的牌上面(A最小,K最大),如果拖动的牌上有按顺序排好的牌时,那么这些牌也跟着一起移动,游戏的目的是将所有的牌按同一花色从小到大排好,为了简单起见,我们的游戏只有同一花色的10张牌,从A到10,且随机的在一行上展开,编号从1到10,把第i号上的牌移到第j号牌上,移动距离为abs(i-j),现在你要做的是求出完成游戏的最小移动距离。

Input

第一个输入数据是T,表示数据的组数。

每组数据有一行,10个输入数据,数据的范围是[1,10],分别表示A到10,我们保证每组数据都是合法的。

Output

对应每组数据输出最小移动距离。

Sample Input

1
1 2 3 4 5 6 7 8 9 10

Sample Output

9

用数组把每个数字在哪个位置记录下来,然后爆搜就行,注意剪枝,因为我要求的是移动最快的,所以当你移动一步的时候要判断是否比已经出来的方案更慢,更慢的话我肯定不用再继续了,对啵?

#include <iostream>
using namespace std;
int s,x[11],visit[11];
int cmp(int a,int b)	//求绝对值
{
    return a>b?a-b:b-a;
}
void dfs(int n,int step)
{
    int i,j,k;
     if(n==9)
    {
        if(step<s)s=step;
        return;
    }
    for(i=0;i<10;i++)	//用来移动的牌
    if(!visit[i])
    for(j=i+1;j<10;j++)	//移动到那个位置
    {
        if(!visit[j])
        {
            k=step+cmp(x[i],x[j]);
            if(k>=s)break;	//如果还没到终点就已经比先前的方案步数多,那么直接over
            visit[i]=1;	//标记当前移动的牌已经就位了,因为i到了j的位置了,那么移动j就不管i的鸟事了
            dfs(n+1,k);
            visit[i]=0;
            break;
        }
    }
}
int main (void)
{
    int i,j,k,l,n,a;
    cin>>n;
    while(n--)
    {
        for(i=0;i<10;i++)
            cin>>a,x[a-1]=i,visit[i]=0;	//用x记录每个牌出现的位置
        s=999999999;
        dfs(0,0);
        cout<<s<<endl;
    }
    return 0;
}

HDU--1584--蜘蛛牌--深搜版本

时间: 2024-08-23 20:55:30

HDU--1584--蜘蛛牌--深搜版本的相关文章

HDU 1584(蜘蛛牌 DFS)

题意是在蜘蛛纸牌的背景下求 10 个数的最小移动距离. 在数组中存储 10 个数字各自的位置,用深搜回溯的方法求解. 代码如下: 1 #include <bits/stdc++.h> 2 using namespace std; 3 int ans,a[20]; 4 bool vis[20]; 5 void dfs(int num,int sum) 6 { 7 if(sum > ans) return; 8 if(num == 9) 9 { 10 ans = sum; 11 return

hdu 1584 蜘蛛牌

陷入了思维误区,以为要用数组下标来记录牌的位置,看了别人的代码才发现是要用数组的值来记录牌的位置,转过这道弯之后就是道水题了 #include<iostream> #include<cmath> #include<cstring> #define maxn 10+2 using namespace std; int mapp[maxn],visit[maxn]; int re; void dfs(int t,int sum) { if(t==9){re=min(re,s

【DFS】hdu 1584 蜘蛛牌

看代码: #include <iostream> #include <cstdio> #include <fstream> using namespace std; const int INF=100000000; const int MAXN=1000000; int ans; int pos[11]; bool vis[11]; int abs(int a,int b){ if(a>b)return a-b; return b-a; } void dfs(in

hdu 1175 连连看 (深搜)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1175 题目大意:如果某两个相同的棋子,可以通过一条线连起来(这条线不能经过其它棋子)这样的两个棋子可以消掉.还有一个要注意的地方的就是转弯.转弯的次数不超过两次,这两个棋子才可以在棋盘上消去~ 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 int

hdu 1518 Square(深搜dfs)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1518 --------------------------------------------------------------------------------------------------------------------------------------------

hdu 1518 Square(深搜+剪枝)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1518 题目大意:根据题目所给的几条边,来判断是否能构成正方形,一个很好的深搜应用,注意剪枝,以防超时! 1 #include <iostream> 2 #include <cstdio> 3 #include<algorithm> 4 #include <cstring> 5 using namespace std; 6 int ap[30],visit[30]

hdu 1342 记忆化深搜

Lotto Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1657    Accepted Submission(s): 811 Problem Description In a Lotto I have ever played, one has to select 6 numbers from the set {1,2,...,49

HDU 1036 Robot Motion 深搜

 Description A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are N north (up the page) S south (down the page) E east (t

HDU 5325 Crazy Bobo(深搜)

题意:给一个n,接着输入n个数,表示n个点的值,接着输入n-1条边形成一个生成树 问最大有多少个点的集合使得该集合内的所有点都满足如下:对于集合内点大小相邻的两个点,该两点之间经过的所有点的大小都小于该两点 eg  7 3 30 350 100 200 300 400 1 2 2 3 3 4 4 5 5 6 6 7 该无向图可表示为1--2--3--4--5--6--7   取点6对于集合(3-7)来说,正好比他大的点为点3,且他们之间的所有点的 大小都小于该两点,满足条件   同理取该集合内的