您的位置:首页 > 其它

URAL 1016. Cube on the Walk(搜索)比较难

2012-08-27 09:53 471 查看
题意:给你一个立方体,每一个面都有一个数字。。从一个坐标滚到另一个坐标,使底面的数字之和最小。。

思路:搜索,对每一个坐标记录起底面和前面,,就有24种状态。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>

using namespace std;
const int INF = 0x3f3f3f3f;
#define up 3,2,5,1
#define down 3,1,5,2
#define left 3,6,5,4
#define right 3,4,5,6
struct cub{
    int a[7];
    int v[7];
    void oper(int i1,int i2,int i3,int i4)
    {
        int tmp = a[i1];
        a[i1]=a[i4],a[i4]=a[i3],a[i3]=a[i2],a[i2]=tmp;
        tmp = v[i1];
        v[i1]=v[i4],v[i4]=v[i3],v[i3]=v[i2],v[i2]=tmp;
    }
};
struct nod{
    int x,y,dis;
    cub t;
    bool operator<(const nod t) const
    {
        return dis>t.dis;
    }
};
int dis[10][10][10][10];
int pre[10][10][10][10];
int Prev[10][10][10][10];
priority_queue<nod> que;
bool oor(int x,int y)
{
    if(x<0||x>7) return false;
    if(y<0||y>7) return false;
    return true;
}
int ans[109][2],cnt;
int main()
{
    freopen("in.txt","r",stdin);
    int stx,sty,enx,eny;
    char ch[5];
    scanf("%s",ch);
    stx = ch[0]-'a';sty =ch[1]-'1';
    scanf("%s",ch);
    enx = ch[0]-'a';eny =ch[1]-'1';

    cub tmp;
    for(int i=1;i<=6;i++) scanf("%d",&tmp.a[i]),tmp.v[i]=i;
    nod e,t;
    e.x=stx,e.y=sty,e.dis=tmp.a[5];e.t = tmp;
    while(!que.empty()) que.pop();
    que.push(e);
    memset(dis,-1,sizeof(dis));
    dis[stx][sty][5][1] = tmp.a[5];
    int anst1,anst2;
    while(!que.empty())
    {
        e = que.top();que.pop();
      //  cout<<e.x<<" "<<e.y<<"  "<<e.dis<<" v "<<e.t.v[5]<<endl;
      //  for(int i=1;i<7;i++) cout<<e.t.a[i]<<" ";cout<<endl;
        //dis[e.x][e.y] = e.dis;
        if(e.x==enx&&e.y==eny)
        {
            anst1 = e.t.v[5];
            anst2 = e.t.v[1];
            break;
        }
        if(oor(e.x-1,e.y))
        {
            t = e;
            t.x = e.x-1;
            t.t.oper(left);
            t.dis+=t.t.a[5];
            if(dis[t.x][t.y][t.t.v[5]][t.t.v[1]]>t.dis||dis[t.x][t.y][t.t.v[5]][t.t.v[1]]==-1)
            {
                dis[t.x][t.y][t.t.v[5]][t.t.v[1]] = t.dis;
                pre[t.x][t.y][t.t.v[5]][t.t.v[1]] = 0;
                Prev[t.x][t.y][t.t.v[5]][t.t.v[1]] = e.t.v[5]*10+e.t.v[1];
                que.push(t);
            }
        }
        if(oor(e.x+1,e.y))
        {
            t = e;
            t.x = e.x+1;
            t.t.oper(right);
            t.dis+=t.t.a[5];
            if(dis[t.x][t.y][t.t.v[5]][t.t.v[1]]>t.dis||dis[t.x][t.y][t.t.v[5]][t.t.v[1]]==-1)
            {
                dis[t.x][t.y][t.t.v[5]][t.t.v[1]] = t.dis;
                pre[t.x][t.y][t.t.v[5]][t.t.v[1]] = 1;
                Prev[t.x][t.y][t.t.v[5]][t.t.v[1]] =e.t.v[5]*10+e.t.v[1];
                que.push(t);
            }
        }
        if(oor(e.x,e.y-1))
        {
            t = e;
            t.y = e.y-1;
            t.t.oper(down);
            t.dis+=t.t.a[5];
            if(dis[t.x][t.y][t.t.v[5]][t.t.v[1]]>t.dis||dis[t.x][t.y][t.t.v[5]][t.t.v[1]]==-1)
            {
                dis[t.x][t.y][t.t.v[5]][t.t.v[1]] = t.dis;
                pre[t.x][t.y][t.t.v[5]][t.t.v[1]] = 2;
                Prev[t.x][t.y][t.t.v[5]][t.t.v[1]] = e.t.v[5]*10+e.t.v[1];
                que.push(t);
            }
        }
        if(oor(e.x,e.y+1))
        {
            t = e;
            t.y = e.y+1;
            t.t.oper(up);
            t.dis+=t.t.a[5];
            if(dis[t.x][t.y][t.t.v[5]][t.t.v[1]]>t.dis||dis[t.x][t.y][t.t.v[5]][t.t.v[1]]==-1)
            {
                dis[t.x][t.y][t.t.v[5]][t.t.v[1]] = t.dis;
                pre[t.x][t.y][t.t.v[5]][t.t.v[1]] = 3;
                Prev[t.x][t.y][t.t.v[5]][t.t.v[1]] = e.t.v[5]*10+e.t.v[1];
                que.push(t);
            }
        }
    }
    //return 0;
    int tx = enx,ty = eny,td1 = anst1,td2=anst2;
   // cout<<enx<<"   ++ "<<eny<<endl;
    //ans[cnt][0]=enx,ans[cnt][1]=eny;
    while(tx!=stx||ty!=sty||td1!=5||td2!=1)
    {
       // cout<<tx<<" "<<ty<<"  "<<endl;
        int tp1,tp2;
        ans[cnt][0]=tx,ans[cnt][1]=ty;
        cnt++;
        if(pre[tx][ty][td1][td2]==0)
        tp1=Prev[tx][ty][td1][td2]/10,tp2=Prev[tx][ty][td1][td2]%10,tx++;
        else if(pre[tx][ty][td1][td2]==1)
        tp1=Prev[tx][ty][td1][td2]/10,tp2=Prev[tx][ty][td1][td2]%10,tx--;
        else if(pre[tx][ty][td1][td2]==2)
        tp1=Prev[tx][ty][td1][td2]/10,tp2=Prev[tx][ty][td1][td2]%10,ty++;
        else if(pre[tx][ty][td1][td2]==3)
        tp1=Prev[tx][ty][td1][td2]/10,tp2=Prev[tx][ty][td1][td2]%10,ty--;
        td1=tp1,td2=tp2;
    }
    ans[cnt][0]=stx,ans[cnt][1]=sty;
    printf("%d",dis[enx][eny][anst1][anst2]);
    for(;cnt>=0;cnt--)
    printf(" %c%d",ans[cnt][0]+'a',ans[cnt][1]+1);
    printf("\n");
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: