zoukankan      html  css  js  c++  java
  • HDU 2821 Pusher

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2821

    推箱子,往一个方向推,碰到单个箱子只消除,多个箱子则消除一个,其余往该方向位移一个单位.(2个以上箱子在边角不合法)

    不懂题意的话,去那个网址玩玩游戏就理解了.测试数据可以从那个游戏关卡来,基本上程序过个几关就OK了.

    #include <iostream>
    using namespace std;
    int dir[4][2] = {-1,0,0,1,1,0,0,-1};
    char map[25][25];
    int R,C,top,n;
    int path[100]; 
    bool flag ;
    void dfs(int t , int x,int y)
    {
        if(flag) return;
        
        if(t == n)
        {
            
            flag = true;
    
            return;
        }
        for(int i = 0;i < 4;i++)
        {
            int tx,ty;
            int gap = 0;
            tx = x + dir[i][0];
            ty = y + dir[i][1];
            while(map[tx][ty]=='.')
            {
                tx = tx + dir[i][0];
                ty = ty + dir[i][1];
                gap++;
            }
            if(tx<0 || tx>=R || ty<0 || ty >=C)
                continue;
            if(gap) //有空隙
            {
                if(map[tx][ty] - 'a' + 1 > 1) //两个或以上箱子
                {
                    int ttx = tx + dir[i][0];//再增加一个单位位移,看是否能把剩余箱子推过去
                    int tty = ty + dir[i][1];
                    if(ttx>=0 && ttx<R && tty >=0 && tty<C)
                    {
                        char c  = map[tx][ty];
                        char nextc = map[ttx][tty];
                        if(nextc == '.')
                            map[ttx][tty] = map[tx][ty] - 1;
                        else 
                            map[ttx][tty] = map[tx][ty] + nextc - 'a';
                        map[tx][ty] = '.';
                        path[top++] = i;
                        dfs(t+1,tx,ty);
                        if(flag) return;
                        --top;
                        map[tx][ty] = c;
                        map[ttx][tty] = nextc;
                    }
                }else 
                {
                    map[tx][ty] = '.';
                    path[top++] = i;
                    dfs(t+1,tx,ty);
                    if(flag) return;
                    --top;
                    map[tx][ty] = 'a';
                }
            }
        }
    
    }
    
    int main(int argc, const char *argv[])
    {
    
        //freopen("input.txt","r",stdin);
        while(cin>>C)
        {
            n = 0;
            cin>>R;
            for(int i=0;i<R;i++)
            {
                cin>>map[i];
                //cout<<map[i]<<endl;
                for(int j=0;j<C;j++)
                {
                    if(map[i][j] != '.')
                    {
                        n += map[i][j] - 'a' + 1;
                    }
                }
            }
            flag = false;
            for( i=0;i<R;i++)
            {
                for(int j=0;j<C;j++)
                {
                    if(map[i][j] != '.')
                        continue;
                    top = 0;
                    dfs(0,i,j);
                    if(flag)
                    {
                        cout<<i<<endl;
                        cout<<j<<endl;
                        for(int k=0;k<top;k++)
                        {
                            if(path[k]<2)
                              printf("%c",path[k]==0?'U':'R');
                            else printf("%c",path[k]==2?'D':'L');
                        }
                        printf("
    ");
                        break;
                    }
                }
                if(flag)
                    break;
            }
    
        }
        return 0;
    }
  • 相关阅读:
    推荐几款Silverlight Tools【转载】
    Emit Vs CodeDom
    Silverlight 中实现Service同步调用
    一个配置文件的Mapping
    Silverlight:获取ContentTemplate中的命名控件
    关于计划
    巧用异步委托解决异步并发问题
    我是如何学习NodeJs的 – 笔记1
    应用HttpHandler, Json, JQuery, ASP.Net UserControl等技术处理 Ajax 的解决方案
    关于程序员的那些事一个五年程序员的总结
  • 原文地址:https://www.cnblogs.com/destino74/p/3323236.html
Copyright © 2011-2022 走看看