usaco 4.4 Shuttle Puzzle(字符hash+搜索)

Shuttle Puzzle
Traditional

The Shuttle Puzzle of size 3 consists of 3 white marbles, 3 black marbles, and a strip of wood with 7 holes. The marbles of the same color are placed in the holes at the opposite ends of the strip, leaving the center hole empty.

INITIAL STATE: WWW_BBB 
GOAL STATE: BBB_WWW

To solve the shuttle puzzle, use only two types of moves. Move 1 marble 1 space (into the empty hole) or jump 1 marble over 1 marble of the opposite color (into the empty hole). You may not back up, and you may not jump over 2 marbles.

A Shuttle Puzzle of size N consists of N white marbles and N black marbles and 2N+1 holes.

Here's one solution for the problem of size 3 showing the initial, intermediate, and end states:

WWW BBB
WW WBBB
WWBW BB
WWBWB B
WWB BWB
W BWBWB
 WBWBWB
BW WBWB
BWBW WB
BWBWBW 
BWBWB W
BWB BWW
B BWBWW
BB WBWW
BBBW WW
BBB WWW

Write a program that will solve the SHUTTLE PUZZLE for any size N (1 <= N <= 12) in the minimum number of moves and display the successive moves, 20 per line.

PROGRAM NAME: shuttle

INPUT FORMAT

A single line with the integer N.

SAMPLE INPUT (file shuttle.in)

3

OUTPUT FORMAT

The list of moves expressed as space-separated integers, 20 per line (except possibly the last line). Number the marbles/holes from the left, starting with one.

Output the the solution that would appear first among the set of minimal solutions sorted numerically (first by the first number, using the second number for ties, and so on).

SAMPLE OUTPUT (file shuttle.out)

3 5 6 4 2 1 3 5 7 6 4 2 3 5 4

题意:给你2*n个黑白棋,一开始白棋在左黑棋在右,要求你计算出把黑白棋对调的最小步数,并输出方案

分析:一开始就想到爆搜加上字符hash记录状态。。。结果显然会超时,本地输入7就出不来了= =

但是分析了下前7个数据,发现时有规律的,这时候完全可以找规律,不过我懒了,只用了个规律,就是步数一定等于n*(n+2)

这样就可以剪枝了,一运行,快了不少,10也可以出来,就是慢了点,后来用了优化,就是黑棋只能往左移,白棋往右移,然后就秒掉了所有数据= =

貌似是只要一条这样的路径吧。。。。

代码:

/*
ID: 15114582
PROG: shuttle
LANG: C++
*/
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int mm=100007;
struct hashTable
{
    int h[mm],p[mm],d[mm],size;
    char s[mm][33];
    int hash(char *a)
    {
        int h=0;
        while(*a)h=h*131+(*a++);
        return (h&0x7FFFFFFF)%mm;
    }
    int insert(char *a,int val)
    {
        int i,id=hash(a);
        for(i=h[id];i>=0;i=p[i])
            if(!strcmp(s[i],a))
            {
                if(d[i]>val)
                {
                    d[i]=val;
                    return 1;
                }
                return 0;
            }
        strcpy(s[size],a);
        d[size]=val,p[size]=h[id],h[id]=size++;
        return 1;
    }
    void clear()
    {
        size=0;
        memset(h,-1,sizeof(h));
    }
}g;
char s[33],e[33];
int q[mm],out[mm];
int i,n,r,ans;
bool dfs(int m)
{
    if(r>=ans)return 0;
    if(m==n&&!strcmp(s,e))
    {
        ans=r;
        for(int i=0;i<r;++i)
            out[i]=q[i];
        return 1;
    }
    if(m>0&&s[m-1]=='W')
    {
        s[m]=s[m-1],s[m-1]=' ';
        q[r++]=m-1;
        if(g.insert(s,r))
            if(dfs(m-1))return 1;
        --r,s[m-1]=s[m],s[m]=' ';
    }
    if(m<n+n+1&&s[m+1]=='B')
    {
        s[m]=s[m+1],s[m+1]=' ';
        q[r++]=m+1;
        if(g.insert(s,r))
            if(dfs(m+1))return 1;
        --r,s[m+1]=s[m],s[m]=' ';
    }
    if(m>1&&s[m-1]!=s[m-2]&&s[m-2]=='W')
    {
        s[m]=s[m-2],s[m-2]=' ';
        q[r++]=m-2;
        if(g.insert(s,r))
            if(dfs(m-2))return 1;
        --r,s[m-2]=s[m],s[m]=' ';
    }
    if(m<n+n&&s[m+1]!=s[m+2]&&s[m+2]=='B')
    {
        s[m]=s[m+2],s[m+2]=' ';
        q[r++]=m+2;
        if(g.insert(s,r))
            if(dfs(m+2))return 1;
        --r,s[m+2]=s[m],s[m]=' ';
    }
    return 0;
}
int main()
{
    freopen("shuttle.in","r",stdin);
    freopen("shuttle.out","w",stdout);
    while(~scanf("%d",&n))
    {
        for(i=0;i<n;++i)
            e[n+i+1]=s[i]='W',e[i]=s[n+i+1]='B';
        e[n]=s[n]=' ';
        e[n+n+1]=s[n+n+1]='\0';
        g.clear();
        g.insert(s,0);
        r=0;
        ans=n*(n+2)+1;
        dfs(n);
        for(i=0;i<ans;++i)
            printf("%d%c",out[i]+1,((i+1)%20)&&(i<ans-1)?' ':'\n');
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是P4087 [USACO17DEC]Milk Measurement的c++代码: ```c++ #include<bits/stdc++.h> using namespace std; int n,d,i,x,minn=1e9,maxn=-1e9,sum=7;//注意sum要初始化为7,因为一开始有三个人挤奶! map<int,int> mp; struct node{ int day,milk,id;//day表示某一天,milk表示这一天的产奶量,id表示这头牛的编号 }a[100010]; bool cmp(node x,node y){ return x.day<y.day; } int main(){ scanf("%d%d",&n,&d); for(i=1;i<=n;i++){ scanf("%d%d%d",&a[i].day,&a[i].id,&a[i].milk); minn=min(minn,a[i].id);//记录最小的牛的编号 maxn=max(maxn,a[i].id);//记录最大的牛的编号 } sort(a+1,a+n+1,cmp);//排序 for(i=1;i<=n;i++){ int p=a[i].id; mp[p]+=a[i].milk;//记录每头牛产奶总量 if(mp[p]-a[i].milk>=mp[minn]&&mp[p]>=mp[minn]){//如果这头牛的产奶总量减去这一天的产奶量后等于最小产奶量且这头牛的产奶总量大于等于最小产奶量 sum--; } if(mp[p]>=mp[maxn]&&mp[p]-a[i].milk<mp[maxn]){//如果这头牛的产奶总量大于等于最大产奶量且这头牛的产奶总量减去这一天的产奶量小于最大产奶量 sum++; } if(mp[p]-a[i].milk<mp[maxn]&&mp[p]>=mp[maxn]){//如果这头牛的产奶总量减去这一天的产奶量小于最大产奶量且这头牛的产奶总量大于等于最大产奶量 if(mp[maxn]-mp[p]+a[i].milk>0)sum++; } mp[p]-=a[i].milk;//减去这一天的产奶量 if(i==n||a[i].day!=a[i+1].day){//如果到了新的一天或者到了最后一天 if(mp[maxn]!=mp[a[i].id]&&mp[a[i].id]>=mp[maxn])sum++;//如果这头牛的产奶总量不等于最大产奶量且这头牛的产奶总量大于等于最大产奶量 if(mp[maxn]==mp[a[i].id]){//如果这头牛的产奶总量等于最大产奶量 if(a[i].id==maxn)sum+=0;//如果这头牛就是最大产奶量的牛,那么不需要增加计数器 else sum++;//否则需要增加计数器 } if(mp[minn]!=mp[a[i].id]&&mp[a[i].id]>=mp[minn])sum++;//如果这头牛的产奶总量不等于最小产奶量且这头牛的产奶总量大于等于最小产奶量 if(mp[minn]==mp[a[i].id]){ if(a[i].id==minn)sum+=0;//如果这头牛就是最小产奶量的牛,那么不需要增加计数器 else sum++;//否则需要增加计数器 } } } printf("%d\n",sum); return 0; } ``` 该题的解题思路是模拟,需要注意细节问题。我们可以首先将输入的数据按天数排序,然后模拟每一天挤奶的情况,并根据题目要求进行计数即可。具体细节请见代码注释。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值