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("\n");
                    break;
                }
            }
            if(flag)
                break;
        }

    }
    return 0;
}

 

转载于:https://www.cnblogs.com/destino74/p/3323236.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值