您的位置:首页 > 其它

HDU 2821 Pusher

2013-09-15 20:58 344 查看
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2821

推箱子,往一个方向推,碰到单个箱子只消除,多个箱子则消除一个,其余往该方向位移一个单位.(2个以上箱子在边角不合法)

不懂题意的话,去那个网址玩玩游戏就理解了.测试数据可以从那个游戏关卡来,基本上程序过个几关就OK了.

#include <iostream>
using namespace std;
int dir[4][2] = {-1,0,0,1,1,0,0,-1};
char map[25][25];
int R,C,top,n;
int path[100];
bool flag ;
void dfs(int t , int x,int y)
{
if(flag) return;

if(t == n)
{

flag = true;

return;
}
for(int i = 0;i < 4;i++)
{
int tx,ty;
int gap = 0;
tx = x + dir[i][0];
ty = y + dir[i][1];
while(map[tx][ty]=='.')
{
tx = tx + dir[i][0];
ty = ty + dir[i][1];
gap++;
}
if(tx<0 || tx>=R || ty<0 || ty >=C)
continue;
if(gap) //有空隙
{
if(map[tx][ty] - 'a' + 1 > 1) //两个或以上箱子
{
int ttx = tx + dir[i][0];//再增加一个单位位移,看是否能把剩余箱子推过去
int tty = ty + dir[i][1];
if(ttx>=0 && ttx<R && tty >=0 && tty<C)
{
char c  = map[tx][ty];
char nextc = map[ttx][tty];
if(nextc == '.')
map[ttx][tty] = map[tx][ty] - 1;
else
map[ttx][tty] = map[tx][ty] + nextc - 'a';
map[tx][ty] = '.';
path[top++] = i;
dfs(t+1,tx,ty);
if(flag) return;
--top;
map[tx][ty] = c;
map[ttx][tty] = nextc;
}
}else
{
map[tx][ty] = '.';
path[top++] = i;
dfs(t+1,tx,ty);
if(flag) return;
--top;
map[tx][ty] = 'a';
}
}
}

}

int main(int argc, const char *argv[])
{

//freopen("input.txt","r",stdin);
while(cin>>C)
{
n = 0;
cin>>R;
for(int i=0;i<R;i++)
{
cin>>map[i];
//cout<<map[i]<<endl;
for(int j=0;j<C;j++)
{
if(map[i][j] != '.')
{
n += map[i][j] - 'a' + 1;
}
}
}
flag = false;
for( i=0;i<R;i++)
{
for(int j=0;j<C;j++)
{
if(map[i][j] != '.')
continue;
top = 0;
dfs(0,i,j);
if(flag)
{
cout<<i<<endl;
cout<<j<<endl;
for(int k=0;k<top;k++)
{
if(path[k]<2)
printf("%c",path[k]==0?'U':'R');
else printf("%c",path[k]==2?'D':'L');
}
printf("\n");
break;
}
}
if(flag)
break;
}

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: