hdu2821

/*
分析:
    简单DFS,做到我心都酥了。。。最后才发现,
如果这个东西挨着墙,我们要把它朝着墙外推的话,
这个动作是禁止的。。。(我是怎么读题的,记得
读出来说的是推到墙外了,那么被推到墙外的那部
分直接不必再考虑了。。。囧)
    没什么好说的,注意细节就行了。


                                           2013-03-01
*/








#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"math.h"
#define N 33

int n,m,count,ans;
int s_x,s_y,K;
char str[N*N];
int dir[4][2]={0,1, 0,-1, 1,0, -1,0};
char map[N][N],tt[N][N];

void DFS(int x,int y,int tot,int len)
{
	if(!tot)	{ans=1;K=len;return ;}
	if(ans)		return ;
	char ttemp[N][N];

	int i;
	int flag;
	int next_x,next_y;
	for(i=0;i<4;i++)
	{
		flag=0;
		next_x=x;next_y=y;
		while(1)
		{
			next_x+=dir[i][0];
			next_y+=dir[i][1];
			if(next_x<0 || next_x>=n || next_y<0 || next_y>=m)	{flag=1;break;}
			if(tt[next_x][next_y]!='.')	break;
		}
		if(flag)	continue;
		if(abs(next_x-x)+abs(next_y-y)<2)	continue;
		int t1,t2;
		t1=next_x+dir[i][0],t2=next_y+dir[i][1];
		if(t1<0 || t1>=n || t2<0 || t2>=m)	continue;
		t1=x;t2=y;
		while(t1>=0 && t1<n && t2>=0 && t2<m)
		{
			ttemp[t1][t2]=tt[t1][t2];
			t1+=dir[i][0];t2+=dir[i][1];
		}
		t1=x;t2=y;
		while(t1>=0 && t1<n && t2>=0 && t2<m)
		{
			ttemp[t1][t2]=tt[t1][t2];
			t1-=dir[i][0];t2-=dir[i][1];
		}

		if(tt[next_x][next_y]>'a')
		{
			if(tt[next_x+dir[i][0]][next_y+dir[i][1]]!='.')
				tt[next_x+dir[i][0]][next_y+dir[i][1]]+=tt[next_x][next_y]-'a';
			else
				tt[next_x+dir[i][0]][next_y+dir[i][1]]=tt[next_x][next_y]-1;
		}
		tt[next_x][next_y]='.';
		if(i==0)		str[len]='R';
		else if(i==1)	str[len]='L';
		else if(i==2)	str[len]='D';
		else			str[len]='U';
		DFS(next_x,next_y,tot-1,len+1);
		if(ans)	break;

		t1=x;t2=y;
		while(t1>=0 && t1<n && t2>=0 && t2<m)
		{
			tt[t1][t2]=ttemp[t1][t2];
			t1+=dir[i][0];t2+=dir[i][1];
		}
		t1=x;t2=y;
		while(t1>=0 && t1<n && t2>=0 && t2<m)
		{
			tt[t1][t2]=ttemp[t1][t2];
			t1-=dir[i][0];t2-=dir[i][1];
		}
	}
}
int main()
{
	int i,l,j;
	int next_x,next_y;
	while(scanf("%d%d",&m,&n)!=-1)
	{
		for(i=0;i<n;i++)	scanf("%s",map[i]);
		count=0;
		for(i=0;i<n;i++)for(l=0;l<m;l++)
			if(map[i][l]!='.')	count+=map[i][l]-'a'+1;

		ans=0;
		for(i=0;i<n;i++)
		{
			if(ans)	break;
			for(l=0;l<m;l++)
			{
				if(ans)	break;
				if(map[i][l]=='.')	continue;
				for(j=0;j<4;j++)
				{
					next_x=i+2*dir[j][0];
					next_y=l+2*dir[j][1];
					if(next_x<0 || next_x>=n || next_y<0 || next_y>=m)	continue;
					if(map[next_x-dir[j][0]][next_y-dir[j][1]]!='.')	continue;
					if(map[next_x][next_y]!='.')						continue;
					s_x=next_x;s_y=next_y;
					for(int z=0;z<n;z++)	strcpy(tt[z],map[z]);
					DFS(next_x,next_y,count,0);
					if(ans)	break;
				}
			}
		}
		printf("%d\n%d\n",s_x,s_y);
		for(i=0;i<K;i++)	printf("%c",str[i]);
		printf("\n");
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值