标签:des style http color os io for cti

3 7 ... ... .b. ... ... .a. ...
4 1 UDUHintHint: 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.
思路:简单DFS。给若干个可重叠的格子,起点可以任意选择,可以往四个方向一直走到有格子的地方为止,每碰一次就消掉一个格子,并且把剩下的移到下一个位置。
注意:①起点不能有格子。②必须要隔一个位置才能碰。③碰的格子在边上时,剩下的格子不能移出矩形范围。④格子在边上时,碰之后如果还有格子剩余,pusher的位置就不在边上,否则就在边上。
#include <stdio.h>
int n,m,total,sx,sy;
char mp[25][26],ans[10000];
bool flag;
void dfs(int x,int y,int cnt)
{
int i,t1,t2;
if(flag) return;
if(cnt==total)
{
printf("%d\n%d\n",sx,sy);
ans[cnt]=0;
puts(ans);
flag=1;
return;
}
if(x-1>0 && !mp[x-1][y])//U
{
for(i=x-2;i>=0;i--) if(mp[i][y]>0) break;
if(i>0)
{
t1=mp[i-1][y];
t2=mp[i][y];
mp[i-1][y]+=mp[i][y]-1;
mp[i][y]=0;
ans[cnt]='U';
dfs(i,y,cnt+1);
mp[i-1][y]=t1;
mp[i][y]=t2;
}
else if(!i)
{
mp[i][y]--;
ans[cnt]='U';
if(mp[i][y]) dfs(1,y,cnt+1);
else dfs(0,y,cnt+1);
mp[i][y]++;
}
}
if(y-1>0 && !mp[x][y-1])//L
{
for(i=y-2;i>=0;i--) if(mp[x][i]>0) break;
if(i>0)
{
t1=mp[x][i-1];
t2=mp[x][i];
mp[x][i-1]+=mp[x][i]-1;
mp[x][i]=0;
ans[cnt]='L';
dfs(x,i,cnt+1);
mp[x][i-1]=t1;
mp[x][i]=t2;
}
else if(!i)
{
mp[x][i]--;
ans[cnt]='L';
if(mp[x][i]) dfs(x,1,cnt+1);
else dfs(x,0,cnt+1);
mp[x][i]++;
}
}
if(y+1<m-1 && !mp[x][y+1])//R
{
for(i=y+2;i<m;i++) if(mp[x][i]>0) break;
if(i<m-1)
{
t1=mp[x][i+1];
t2=mp[x][i];
mp[x][i+1]+=mp[x][i]-1;
mp[x][i]=0;
ans[cnt]='R';
dfs(x,i,cnt+1);
mp[x][i+1]=t1;
mp[x][i]=t2;
}
else if(i==m-1)
{
mp[x][i]--;
ans[cnt]='R';
if(mp[x][i]) dfs(x,m-2,cnt+1);
else dfs(x,m-1,cnt+1);
mp[x][i]++;
}
}
if(x+1<n-1 && !mp[x+1][y])//D
{
for(i=x+2;i<n;i++) if(mp[i][y]>0) break;
if(i<n-1)
{
t1=mp[i+1][y];
t2=mp[i][y];
mp[i+1][y]+=mp[i][y]-1;
mp[i][y]=0;
ans[cnt]='D';
dfs(i,y,cnt+1);
mp[i+1][y]=t1;
mp[i][y]=t2;
}
else if(i==n-1)
{
mp[i][y]--;
ans[cnt]='D';
if(mp[i][y]) dfs(n-2,y,cnt+1);
else dfs(n-1,y,cnt+1);
mp[i][y]++;
}
}
}
int main()
{
int i,j;
while(~scanf("%d%d",&m,&n))
{
for(i=0;i<n;i++) scanf("%s",mp[i]);
total=flag=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(mp[i][j]!='.')
{
total+=mp[i][j]-'a'+1;
mp[i][j]=mp[i][j]-'a'+1;
}
else mp[i][j]=0;
}
}
for(i=0;i<n && !flag;i++)
{
for(j=0;j<m && !flag;j++)
{
if(!mp[i][j])
{
sx=i;
sy=j;
dfs(i,j,0);
}
}
}
}
}
HDU-2821-Pusher(DFS),布布扣,bubuko.com
标签:des style http color os io for cti
原文地址:http://blog.csdn.net/faithdmc/article/details/38352683