hdu 2821 Pusher(dfs)

本文深入探讨在线游戏《推石者》的解题策略与算法实现,玩家需通过推动物块,逐步清空游戏板上的障碍物。文章详细解析游戏规则、解题思路,并提供AC代码实例,帮助理解如何通过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]='\0';
        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\n%d\n%s\n",i,j,path);
				i=r;
				j=c;
            }
        }
    }
    return 0;
}


 

 

转载于:https://www.cnblogs.com/pangblog/p/3257840.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值