zoukankan      html  css  js  c++  java
  • bzoj1054

    移动玩具

     HYSBZ - 1054 

     在一个4*4的方框内摆放了若干个相同的玩具,某人想将这些玩具重新摆放成为他心中理想的状态,规定移动
    时只能将玩具向上下左右四个方向移动,并且移动的位置不能有玩具,请你用最少的移动次数将初始的玩具状态移
    动到某人心中的目标状态。
    Input
      前4行表示玩具的初始状态,每行4个数字1或0,1表示方格中放置了玩具,0表示没有放置玩具。接着是一个空
    行。接下来4行表示玩具的目标状态,每行4个数字1或0,意义同上。
    Output

      一个整数,所需要的最少移动次数。

    Sample Input
    1111
    0000
    1110
    0010
     
    1010
    0101
    1010
    0101

    Sample Output 

    4

    sol:直接bfs吧,挺好写的

    #include <bits/stdc++.h>
    using namespace std;
    typedef int ll;
    inline ll read()
    {
        ll s=0;
        bool f=0;
        char ch=' ';
        while(!isdigit(ch))
        {
            f|=(ch=='-'); ch=getchar();
        }
        while(isdigit(ch))
        {
            s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
        }
        return (f)?(-s):(s);
    }
    #define R(x) x=read()
    inline void write(ll x)
    {
        if(x<0)
        {
            putchar('-'); x=-x;
        }
        if(x<10)
        {
            putchar(x+'0'); return;
        }
        write(x/10);
        putchar((x%10)+'0');
        return;
    }
    #define W(x) write(x),putchar(' ')
    #define Wl(x) write(x),putchar('
    ')
    int Step[65536],Sta=0,End=0,Now;
    queue<int>Queue;
    inline void Move(int From,int To)
    {
        if(Now&(1<<To)) return;
        int tmp=(Now^(1<<From))|(1<<To);
        if(~Step[tmp]) return;
        Step[tmp]=Step[Now]+1;
        Queue.push(tmp);
    }
    int main()
    {
        memset(Step,-1,sizeof Step);
        int i,j;
        string S;
        for(i=1;i<=4;i++)
        {
            cin>>S;
            for(j=0;j<=3;j++) Sta=(Sta<<1)+(S[j]-'0');
        }
        for(i=1;i<=4;i++)
        {
            cin>>S;
            for(j=0;j<=3;j++) End=(End<<1)+(S[j]-'0');
        }
        Queue.push(Sta); Step[Sta]=0;
        while(!Queue.empty())
        {
            Now=Queue.front(); Queue.pop();
            if(Now==End)
            {
                return Wl(Step[Now]),0;
            }
            for(i=0;i<16;i++) if((1<<i)&Now)
            {
                if(i<12) Move(i,i+4);
                if(i>3) Move(i,i-4);
                if(i%4<3) Move(i,i+1);
                if(i%4>0) Move(i,i-1);
            }
        }
        return 0;
    }
    /*
    input
    1111
    0000
    1110
    0010
    
    1010
    0101
    1010
    0101
    output
    4
    */
    View Code
  • 相关阅读:
    [转] Linux下crontab命令的用法
    [转] Try to use one var statement per scope in JavaScript
    [转] 主流JS框架中DOMReady事件的实现
    MySQL 表复制语句
    [转] MySQL中的运算符展示
    [转] mysql分组取每组前几条记录(排名)
    MySQL 连接结果集
    [转] mysql show processlist命令 详解
    [转] mysql 5.0存储过程学习总结
    jquery中使用event.target的几点
  • 原文地址:https://www.cnblogs.com/gaojunonly1/p/11167266.html
Copyright © 2011-2022 走看看