ural 1016 Cube on the Walk

一道搜索题,,,,,,,一说是最短路题(每个位置都会有24种不同的正方体放置情况,,,求最短路,,)我用的模拟,,而且没用bfs用的dfs,,,,,,,先是预算出直接不拐弯到终点需要多少,,,,,然后深搜的时候大于的直接就舍弃了.....

忘了说题意,,,,,,,,就是一个正方体六个面有六个数,,,,,滚来滚去,,最底下的那个数加和,,问到终点和最小是多少........


#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>


int near,far,top,right,bottom,left;
int qishi[3],zhongzhi[3];
int i,j,k,minn;
int w[10000]={0},b[10000];
int qw,ewq=1;
int flag;

int dfs(int heng, int shu,int xxx,int qw)
{
    //printf("%d**\n",xxx);
    if(ewq=0) return 0;
    if(xxx>=minn) return 0;
    if(heng<1||heng>8||shu<1||shu>8) return 0;
    if(heng==zhongzhi[1]&&shu==zhongzhi[2])
    {
        //printf("%d\n",xxx);
        if(minn>xxx) {minn=xxx; for(i=0;i<qw;i++) {b[i]=w[i]; b[qw]='\0';}
        printf("%d",minn);
        printf(" %c%d",qishi[2]-1+'a',qishi[1]);
        for(i=0;i<qw;i++)
        {
            if(b[i]==1)
            {
                qishi[2]-=1;
            }
            else if(b[i]==2)
            {
                qishi[2]+=1;
            }
            else if(b[i]==3)
            {
                qishi[1]-=1;
            }
            else if(b[i]==4)
            {
                qishi[1]+=1;
            }
            printf(" %c%d",qishi[2]-1+'a',qishi[1]);
        }
         ewq=0;}
        return 0;
    }

    if(heng>1&&flag!=2)
    {

        xxx+=near;
        int t=near;
        near=top;
        top=far;
        far=bottom;
        bottom=t;
        w[qw]=1;
        int cx=flag;
        flag=1;
        dfs(heng-1,shu,xxx,qw+1);
        xxx-=near;
        t=near;
        near=bottom;
        bottom=far;
        far=top;
        top=t;
        flag=cx;
    }
    if(heng<8&&flag!=1)
    {   int cx=flag;
        flag=2;
        xxx+=far;
        int t=near;
        near=bottom;
        bottom=far;
        far=top;
        top=t;
        w[qw]=2;
        dfs(heng+1,shu,xxx,qw+1);
        xxx-=far;
        t=near;
        near=top;
        top=far;
        far=bottom;
        bottom=t;
        flag=cx;
    }
    if(shu>1&&flag!=4)
    {
        xxx+=right;
        int t=right;
        right=top;
        top=left;
        left=bottom;
        bottom=t;
        w[qw]=3;
        int cx=flag;
        flag=3;
        dfs(heng,shu-1,xxx,qw+1); //printf("%d***\n",xxx);
        xxx-=right;
        t=right;
        right=bottom;
        bottom=left;
        left=top;
        top=t;
        flag=cx;
    }
    if(shu<8&&flag!=3)
    {   //printf("%d %d %d %d %d %d **(1)\n",near,far,top,right,bottom,left);
        int cx=flag;
        flag=4;
        xxx+=left;
        int t=left;
        left=top;
        top=right;
        right=bottom;
        bottom=t;
        w[qw]=4;
        dfs(heng,shu+1,xxx,qw+1);
        qw--;
        xxx-=left;
        t=left;
        left=bottom;
        bottom=right;
        right=top;
        top=t;
        flag=cx;
        //printf("%d %d %d %d %d %d **(2)\n",near,far,top,right,bottom,left);
    }
    return 0;
}





int main()
{
    char s1[3],s2[3];
    scanf("%s %s",s1,s2);
    qishi[1]=s1[1]-'0';
    qishi[2]=s1[0]-'a'+1;
    zhongzhi[1]=s2[1]-'0';
    zhongzhi[2]=s2[0]-'a'+1;
    scanf("%d %d %d %d %d %d",&near,&far,&top,&right,&bottom,&left);
    minn=0;
    qw=0;
    flag=0;
    //printf("%d***\n",abs(zhongzhi[1]-qishi[1]));
    //printf("%d %d*\n",qishi[2],qishi[1]);
    for(i=0;i<abs(zhongzhi[2]-qishi[2]);i++)
    {
        if(qishi[0]<zhongzhi[0])
        {
            minn+=abs(zhongzhi[2]-qishi[2])/4*(top+right+bottom+left);
            if(abs(zhongzhi[2]-qishi[2])%4==1) minn+=right;
            if(abs(zhongzhi[2]-qishi[2])%4==2) minn+=right+top;
            if(abs(zhongzhi[2]-qishi[2])%4==3) minn+=right+top+left;
        }
        else if(qishi[2]>zhongzhi[2])
        {
            minn+=abs(zhongzhi[2]-qishi[2])/4*(top+right+bottom+left);
            if(abs(zhongzhi[2]-qishi[2])%4==1) minn+=left;
            if(abs(zhongzhi[2]-qishi[2])%4==2) minn+=left+top;
            if(abs(zhongzhi[2]-qishi[2])%4==3) minn+=right+top+left;
        }
    }
    for(i=0;i<abs(zhongzhi[1]-qishi[1]);i++)
    {
        if(qishi[1]>zhongzhi[1])
        {
            minn+=(abs(zhongzhi[1]-qishi[1])/4)*(top+near+bottom+far);
            if(abs(zhongzhi[1]-qishi[1])%4==1) minn+=near;
            if(abs(zhongzhi[1]-qishi[1])%4==2) minn+=near+top;
            if(abs(zhongzhi[1]-qishi[1])%4==3) minn+=near+top+far;
        }
        else if(qishi[1]<zhongzhi[1])
        {
            minn+=abs(zhongzhi[1]-qishi[1])/4*(top+near+bottom+far);
            if(abs(zhongzhi[1]-qishi[1])%4==1) minn+=far;
            if(abs(zhongzhi[1]-qishi[1])%4==2) minn+=far+top;
            if(abs(zhongzhi[1]-qishi[1])%4==3) minn+=far+top+near;
        }
    }
    //printf("%d\n",minn);
    dfs(qishi[1],qishi[2],0,0);
    //printf("%d\n",minn);
    //printf(" lujin");
    return 0;
}


这题的思路特别简单,,,,,我写的也很傻瓜,,,,,,但是你没有看错,,,,,,我就是不知道哪里错了整出了死循环.................无奈了................


乖乖去看别人的代码.........


AC代码:

#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const ll d6=1023,d5=d6<<10,d4=d5<<10,d3=d4<<10,d2=d3<<10,d1=d2<<10;
#define tl(x) ((x&d1)|(x&d2)|(x&d4)<<10|(x&d5)<<10|(x&d6)<<10|(x&d3)>>30)
#define tr(x) ((x&d1)|(x&d2)|(x&d6)<<30|(x&d3)>>10|(x&d4)>>10|(x&d5)>>10)
#define tu(x) ((x&d5)<<40|(x&d3)<<10|(x&d1)>>20|(x&d4)|(x&d2)>>30|(x&d6))
#define td(x) ((x&d3)<<20|(x&d5)<<30|(x&d2)>>10|(x&d4)|(x&d1)>>40|(x&d6))
map<ll,int>mm;
struct Nod
{
    int x,y,d;
    ll s;
    Nod() {}
    Nod(int x,int y,int d,ll s):x(x),y(y),d(d),s(s) {}
    bool operator < (const Nod &p)const{return d>p.d;}
};
int d[10][10][30];
bool vis[10][10][30];
Nod pre[10][10][30];
int dx[]= {0,-1,0,1};
int dy[]= {-1,0,1,0}; //d l u r
int dij(int sx,int sy,ll ss,int tx,int ty,ll &ts)
{
    priority_queue<Nod>q;
    d[sx][sy][mm[ss]]=(ss&d5)>>10;
    q.push(Nod(sx,sy,(ss&d5)>>10,ss));
    int ux,uy,ud,vx,vy,vd;
    ll us,vs;
    while(!q.empty())
    {
        ux=q.top().x;uy=q.top().y;
        ud=q.top().d;us=q.top().s;
        q.pop();
        if(ux==tx&&uy==ty){ts=us;return ud;}
        if(vis[ux][uy][mm[us]]) continue;
        vis[ux][uy][mm[us]]=1;
        for(int i=0; i<4; i++)
        {
            vx=ux+dx[i];vy=uy+dy[i];
            if(vx<1||vx>8||vy<1||vy>8) continue;
            if(i==0) vs=td(us);
            if(i==1) vs=tl(us);
            if(i==2) vs=tu(us);
            if(i==3) vs=tr(us);
            vd=ud+((vs&d5)>>10);
            if(vd<d[vx][vy][mm[vs]])
            {
                d[vx][vy][mm[vs]]=vd;
                q.push(Nod(vx,vy,vd,vs));
                pre[vx][vy][mm[vs]]=Nod(ux,uy,mm[us],0);
            }
        }
    }
}
void path(Nod p)//print path
{
    if(p.x==-1) return;
    path(pre[p.x][p.y][p.d]);
    printf(" %c%c",p.x+96,p.y+48);
}
int main()
{
    memset(d,63,sizeof(d));
    memset(vis,0,sizeof(vis));
    memset(pre,-1,sizeof(pre));
    char a,b,c,f;
    ll e[6];
    cin>>a>>b>>c>>f;
    a-=96,b-=48,c-=96,f-=48;
    for(int i=0; i<6; i++) cin>>e[i];
    ll st,x=e[0]<<50|e[1]<<40|e[2]<<30|e[3]<<20|e[4]<<10|e[5],xx=x;
    int i,j,k,cnt=0;
    for(i=0; i<4; i++,xx=tl(xx))
        for(j=0; j<4; j++,xx=tu(xx))
            for(k=0; k<4; k++,xx=tl(xx))
                if(mm.find(xx)==mm.end()) mm[xx]=cnt++;//find all states
    cout<<dij(a,b,x,c,f,st);
    path(Nod(c,f,mm[st],0));
    return 0;
}

位运算加速了,,,,,,但是比较烦,,,,,还是就那四种动弹方式..........恩.............回去再多看看把,,,你还差的多呢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用代码解决这个问题The program committee of the school programming contests, which are often held at the Ural State University, is a big, joyful, and united team. In fact, they are so united that the time spent together at the university is not enough for them, so they often visit each other at their homes. In addition, they are quite athletic and like walking. Once the guardian of the traditions of the sports programming at the Ural State University decided that the members of the program committee spent too much time walking from home to home. They could have spent that time inventing and preparing new problems instead. To prove that, he wanted to calculate the average distance that the members of the program committee walked when they visited each other. The guardian took a map of Yekaterinburg, marked the houses of all the members of the program committee there, and wrote down their coordinates. However, there were so many coordinates that he wasn't able to solve that problem and asked for your help. The city of Yekaterinburg is a rectangle with the sides parallel to the coordinate axes. All the streets stretch from east to west or from north to south through the whole city, from one end to the other. The house of each member of the program committee is located strictly at the intersection of two orthogonal streets. It is known that all the members of the program committee walk only along the streets, because it is more pleasant to walk on sidewalks than on small courtyard paths. Of course, when walking from one house to another, they always choose the shortest way. All the members of the program committee visit each other equally often. Input The first line contains the number n of members of the program committee (2 ≤ n ≤ 105). The i-th of the following n lines contains space-separated coordinates xi, yi of the house of the i-th member of the program committee (1 ≤ xi, yi ≤ 106). All coordinates are integers. Output Output the average distance, rounded down to an integer, that a member of the program committee walks from his house to the house of his colleague.
05-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值