(dfs) hdu 2821

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

2009 Multi-University Training Contest 1 - Host by TJU

代码让我写的比较挫。。。跟人比较像。。。。

#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;
}

  

时间: 2024-10-12 16:46:46

(dfs) hdu 2821的相关文章

D - DFS HDU - 2660

D - DFS HDU - 2660 I have N precious stones, and plan to use K of them to make a necklace for my mother, but she won't accept a necklace which is too heavy. Given the value and the weight of each precious stone, please help me find out the most valua

hdu 2821 学习一点dfs的小技巧吧。。 还是自己太弱了

#include<iostream> #include<cstdio> #include<cstring> using namespace std; int r,c,s,flag; int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}}; char mapp[25][25],road[1000],d[5] = {"DURL"}; bool check(int x,int y) { if(x < 0 || x &g

变形课(DFS hdu 1181)

变形课 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 19133    Accepted Submission(s): 6892 Problem Description 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个统一规

(水DFS) hdu 5024

D - Wang Xifeng's Little Plot Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 5024 Appoint description:  System Crawler  (2015-04-14) Description <Dream of the Red Chamber>(also <The Sto

DFS HDU 1342

很水的DFS Lotto Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1515    Accepted Submission(s): 752 Problem Description In a Lotto I have ever played, one has to select 6 numbers from the set {1,2

(dfs) hdu 2258

Continuous Same Game (1) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 465    Accepted Submission(s): 176 Problem Description Continuous Same Game is a simple game played on a grid of colored

DFS HDU 2614

很水的.但是wa了很多遍,因为写num[1][1]... 改成0之后就过了...虽然不知道为什么... Beat Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 659    Accepted Submission(s): 415 Problem Description Zty is a man that always full of

DFS/hdu 1010 Tempter of the Bone

题意 给出一个n*m的地图,s:起点,d:终点,x:障碍物,.:可以走的格子 给出一个时间t,求问是否可以恰好用t时间从起点走到终点.走一步为一个时间. 注意:走过的格子不能再走 注意2:是在时间t刚好到达,而不是时间t以内!也就是说可以在地图上绕远,反正就要在时间t到达! 分析 很普通的深搜,但是有2点剪枝: 1.在刚读入数据后剪枝:当可以走的格子本来就比时间t还短时,必然无法在t时间走到终点 1 if (m*n-xx<=t) //xx is the number of 'X' 2 { 3 p

(DFS) hdu 2208

唉,可爱的小朋友 Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1137    Accepted Submission(s): 309 Problem Description 唉,小朋友是比较麻烦的.在一个幼儿园里,老师要上一节游戏课,有N个小朋友要玩游戏,做游戏时要用小皮球,但是幼儿园里只有M个小皮球,而且有些小朋友不喜欢和一些小朋