• hdu 2821 Pusher(dfs)


    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
    题目大意:在R*C的网格里,有的地方有石块,用a~z表示1~26个石块,每次只能隔一个网格清除一个石块,然后把剩下的往前移,注意每次只能选一个方向,直到碰到石块位置,如果朝这个方向一直走没遇到石块而出界了,那么这个方向就是不对的,再选另一个方向,直到所有的石块全被处理掉,只要找到一种方法就直接输出,这题细节挺多的

    解题思路:这是典型的dfs,不过我自己还不会写,我看了别人的然后自己写的,有什么不当的望各位大牛指出;

    AC代码:

    #include<stdio.h>
    char p[4]={'R','L','U','D'};
    int dir[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
    char path[1024];//这个是用来记录每次行走的方向
    int map[30][30];//在每个点石块的数量
    char a[30][30];
    int r,c,sum;
    int check(int x,int y)//判断是否越界
    {
        if(x>=0&&x<r&&y>=0&&y<c)  return 1;
        else return 0;
    }
    int dfs(int x,int y,int s)
    {
        int i,tx,ty,k;
        if(s==sum)//全部清理完了
        {
            path[s]='';
            return 1;
        }
        for(i=0;i<4;i++)
        {
            tx=x+dir[i][0];
            ty=y+dir[i][1];
    		if(!check(tx,ty)||map[tx][ty])  continue;//越界了或者该点是石块
    		while(check(tx,ty)&&!map[tx][ty])//一直往前走遇到石块或者越界
    		{
    			tx+=dir[i][0];
    			ty+=dir[i][1];
    		}
    		if(check(tx,ty)==0)  continue;//越界了方向不对
    		k=map[tx][ty];
    		if(k>1&&check(tx+dir[i][0],ty+dir[i][1])==0)  continue;//不止一个石块而且往前一步就越界了
    		if(k>1)  map[tx+dir[i][0]][ty+dir[i][1]]+=k-1;
    		map[tx][ty]=0;
    		path[s]=p[i];//记录方向
    		if(dfs(tx,ty,s+1))  return 1;
    		map[tx][ty]=k;//还原
    		map[tx+dir[i][0]][ty+dir[i][1]]-=(k-1);
    	}
    	return 0;
    }
    int main()
    {
        int i,j;
        while(scanf("%d%d",&c,&r)!=EOF)
        {
            sum=0;
            for(i=0;i<r;i++)
            {
                getchar();
                scanf("%s",a[i]);
                for(j=0;j<c;j++)
                {
                    if(a[i][j]=='.') map[i][j]=0;
                    else map[i][j]=a[i][j]-'a'+1;
                    sum+=map[i][j];//表示十块的总数量
                }
            }
            for(i=0;i<r;i++)
            for(j=0;j<c;j++)
            {
                if(map[i][j]==0)
                if(dfs(i,j,0))
                {
                    printf("%d
    %d
    %s
    ",i,j,path);
    				i=r;
    				j=c;
                }
            }
        }
        return 0;
    }
    


  • 相关阅读:
    用外部表的方式查询当天数据库alert日志文件
    比较数据泵和exp/imp对相同数据导出/导入的性能差异
    理解Oracle TM和TX锁
    Rocky4.2下安装金仓v7数据库(KingbaseES)
    理解listagg函数
    sql*loader的直接加载方式和传统加载方式的性能差异
    Rocky4.2下安装达梦(DM)6数据库
    演示对sys用户和普通用户进行审计的示例
    演示一个通过触发器进行审计的示例
    演示一个使用db vault进行安全控制的示例
  • 原文地址:https://www.cnblogs.com/pangblog/p/3257840.html
Copyright © 2020-2023  润新知