码迷,mamicode.com
首页 > 其他好文 > 详细

(dfs) hdu 2821

时间:2015-03-20 21:38:55      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

Pusher

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Others)
Total Submission(s): 956    Accepted Submission(s): 348
Special Judge


Problem Description
PusherBoy is an online game http://www.hacker.org/push . There is an R * C grid, and there are piles of blocks on some positions. The goal is to clear the blocks by pushing into them. 

You should choose an empty area as the initial position of the PusherBoy. Then you can choose which direction (U for up, D for down, L for left and R for right) to push. Once the direction is chosen, the PusherBoy will walk ahead until he met a pile of blocks (Walking outside the grid is invalid). Then he remove one block from the pile (so if the pile contains only one block, it will become empty), and push the remaining pile of blocks to the next area. (If there have been some blocks in the next area, the two piles will form a new big pile.)

Please note if the pusher is right up against the block, he can‘t remove and push it. That is, there must be a gap between the pusher and the pile. As the following figure, the pusher can go up, but cannot go down. (The cycle indicates the pusher, and the squares indicate the blocks. The nested squares indicate a pile of two blocks.)
技术分享


And if a whole pile is pushed outside the grid, it will be considered as cleared.
 

 

Input
There are several test cases in each input. The first two lines of each case contain two numbers C and R. (R,C <= 25) Then R lines follow, indicating the grid. ‘.‘ stands for an empty area, and a lowercase letter stands for a pile of blocks. (‘a‘ for one block, ‘b‘ for two blocks, ‘c‘ for three, and so on.)

 

 

Output
Output three lines for each case. The first two lines contains two numbers x and y, indicating the initial position of the PusherBoy. (0 <= x < R, 0 <= y < C). The third line contains a moving sequence contains ‘U‘, ‘D‘, ‘L‘ and ‘R‘. Any correct answer will be accepted.
 

 

Sample Input
3 7 ... ... .b. ... ... .a. ...
 

 

Sample Output
4 1 UDU
Hint
Hint: The following figures show the sample. The circle is the position of the pusher. And the squares are blocks (The two nested squares indicating a pile of two blocks). And this is the unique solution for this case.
技术分享
 

 

Source
 
 
代码让我写的比较挫。。。跟人比较像。。。。
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
int n,m,sum;
char s[30][30];
int map[30][30];
int dic[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
char ss[4]={‘R‘,‘L‘,‘U‘,‘D‘},path[100010];
bool check(int x,int y)
{
    if(x<0||y<0||x>=n||y>=m)
        return false;
    return true;
}
bool dfs(int x,int y ,int ret)
{
    int xx,yy;
    if(ret==sum)
    {
        path[ret]=‘\0‘;
        return true;
    }
    for(int i=0;i<4;i++)
    {
        xx=x+dic[i][0],yy=y+dic[i][1];
        if(map[xx][yy]||!check(xx,yy))
            continue;
        while(!map[xx][yy]&&check(xx,yy))
        {
            xx+=dic[i][0];
            yy+=dic[i][1];
        }
        if(!check(xx,yy)) continue;
        int cnt=map[xx][yy];
        if(cnt>1&&!check(xx+dic[i][0],yy+dic[i][1])) continue;
        if(cnt>1) map[xx+dic[i][0]][yy+dic[i][1]]+=(cnt-1);
        map[xx][yy]=0;
        path[ret]=ss[i];
        if(dfs(xx,yy,ret+1)) return true;
        map[xx+dic[i][0]][yy+dic[i][1]]-=(cnt-1);
        map[xx][yy]=cnt;
    }
    return false;
}
int main()
{
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%s",s[i]);
            for(int j=0;j<m;j++)
            {
                if(s[i][j]==‘.‘)
                    map[i][j]=0;
                else
                    map[i][j]=s[i][j]-‘a‘+1;
                sum+=map[i][j];
            }
        }
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                {
                    if(map[i][j]==0)
                    {
                        if(dfs(i,j,0))
                        {
                            printf("%d\n%d\n%s\n",i,j,path);
                            i=n,j=m;
                        }
                    }
                }
            }
    }
    return 0;
}

  

(dfs) hdu 2821

标签:

原文地址:http://www.cnblogs.com/a972290869/p/4354460.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!