hdu 2821 Pusher (dfs)

时间:2023-03-08 23:59:13
hdu 2821 Pusher (dfs)

把这个写出来是不是就意味着把   http://www.hacker.org/push  这个游戏打爆了? ~啊哈哈哈hdu 2821 Pusher (dfs)

其实只要找到一个就可以退出了  所以效率也不算很低的  可以直接DFS呀呀呀呀

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector> using namespace std; int dx[] = {-1,1,0,0};
int dy[] = {0,0,1,-1}; vector <char> ans;
int n,m;
char map[30][30];
char t[30][30];
bool found;
void copy() //每一次都需要复制一下地图
{
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
map[i][j]=t[i][j];
} void debug(int ax,int ay) //调试函数~~~
{
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(i==ax && j==ay){printf("@");continue;}
printf("%c",map[i][j]);
}
puts("");
}
puts("");
} bool ok(int xx,int yy)
{
if(xx<n && xx>=0 && yy<m && yy>=0)return true; return false;
} bool tag() //全部砖块都清除了
{
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(map[i][j]!='.')return false; return true;
} bool dfs(int posx,int posy)
{
if(found)return true;
if(tag())
{
found=true;
return true;
}
//debug(posx,posy);
for(int k=0;k<4;k++)
{
if(map[posx+dx[k]][posy+dy[k]]!='.' || !ok(posx+dx[k],posy+dy[k]))continue;
for(int i=1;i<=25;i++)
{
if(map[posx+i*dx[k]][posy+i*dy[k]]!='.' && ok(posx+i*dx[k],posy+i*dy[k]))
{
int tx=posx+i*dx[k];
int ty=posy+i*dy[k];
char cur1=map[tx][ty];
char cur2=map[tx+dx[k]][ty+dy[k]];
map[tx][ty]='.';
if(cur1!='a')
{
if(cur2=='.')
{
map[tx+dx[k]][ty+dy[k]]=cur1-1; //没有就减一级
}
else
{
map[tx+dx[k]][ty+dy[k]]=cur1-'a'+cur2; //如果后面有东西就要合体
}
} if(k==0)ans.push_back('U');
else if(k==1)ans.push_back('D');
else if(k==2)ans.push_back('R');
else ans.push_back('L'); dfs(tx,ty);
if(found)return true;
map[tx][ty]=cur1;
map[tx+dx[k]][ty+dy[k]]=cur2;
ans.pop_back(); break;
}
}
}
return false;
} int main()
{
while(scanf("%d%d",&m,&n)!=EOF)
{
found=false;
ans.clear();
for(int i=0;i<n;i++)
scanf("%s",t[i]); for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(t[i][j]!='.')continue;
copy();
if(dfs(i,j))
{
printf("%d\n%d\n",i,j);
break;
}
}
if(found)break;
}
for(int sb=0;sb<ans.size();sb++)
printf("%c",ans[sb]);
puts("");
}
return 0;
} /*
13 3
.............
...bb..bc....
............. 7 8
.......
.......
.....b.
.......
.aa.b..
.....a.
.......
.......
*/