hdu 2821 Pusher (dfs)
把这个写出来是不是就意味着把 http://www.hacker.org/push 这个游戏打爆了? ~啊哈哈哈
其实只要找到一个就可以退出了 所以效率也不算很低的 可以直接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.
.......
.......
*/
hdu 2821 Pusher (dfs)的更多相关文章
- hdu 2821 Pusher(dfs)
Problem Description PusherBoy is an online game http://www.hacker.org/push . There is an R * C grid, ...
- HDU 5965 扫雷(dfs)题解
题意:给你一个3*n的格子,中间那行表明的是周围8格(当然左右都没有)的炸弹数量,上下两行都可以放炸弹,问你有几种可能,对mod取模 思路:显然(不),当i - 1和i - 2确定时,那么i的个数一定 ...
- HDU 1518 Square(DFS)
Problem Description Given a set of sticks of various lengths, is it possible to join them end-to-end ...
- HDU 1015 Safecracker (DFS)
题意:给一个数字n(n<=12000000)和一个字符串s(s<=17),字符串的全是有大写字母组成,字母的大小按照字母表的顺序,比如(A=1,B=2,......Z=26),从该字符串中 ...
- Hdu 1175 连连看(DFS)
Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=1175 因为题目只问能不能搜到,没问最少要几个弯才能搜到,所以我采取了DFS. 因为与Hdu ...
- HDU 1501 Zipper(DFS)
Problem Description Given three strings, you are to determine whether the third string can be formed ...
- HDU 5305 Friends(dfs)
Friends Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Su ...
- HDU 2553 N皇后问题(dfs)
N皇后问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 在 ...
- HDU 5934 Bomb(炸弹)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
随机推荐
- ADO.NET+Access: 2,至少一个参数没有被指定值
ylbtech-Error-ADO.NET+Access: 2,至少一个参数没有被指定值. 1.A,错误代码返回顶部 2,至少一个参数没有被指定值. 1.B,出错原因分析返回顶部 未解决 1.C, ...
- 把php.exe加入系统环境变量-使用命令行可快速执行PHP命令
有时候在执行长时间运行的脚本程序的时候,浏览器是架不住的.我们就可以使用CMD命令行或者LINUX命令行执行PHP程序 1.把PHP.EXE加入到环境变量,不用每次都进入到PHP的目录 ① 右击我的 ...
- pdm 中怎么修改表的Name值时使Code值不变
修改方法:PowerDesign中的选项菜单里修改,在[Tool]-->[General Options]->[Dialog]->[Operating modes]->[Nam ...
- sql server 数据库的操作
Technorati 标记: sql截取,sql修改 把一个字段的部分取出来,插入到新字段 1 ) //方法二 sql server修改一个字段中的部分数据 update tablename se ...
- Substrings 子字符串-----搜索
Description You are given a number of case-sensitive strings of alphabetic characters, find the larg ...
- linux(centos7)下安装tomcat7
1.下载tomcat1.7.tar.gz 2.将文件放到/usr/local中 #cp tomcat1.7.tar.gz /usr/local 3.进入到/usr/local中,解压缩tomcat1. ...
- WebGoat学习——跨站脚本攻击(Cross‐Site Scripting (XSS))
跨站脚本攻击(Cross‐Site Scripting (XSS)) XSS(Cross Site Script)跨站脚本攻击.是指攻击者向被攻击Web 页面里插入恶意html代码,当用户浏览该页之时 ...
- 捣蛋phpwind控制器注入
在PwBaseController 里面,会有这个方法的存在 /** * action Hook 注册 * * @param string $registerKey 扩展点别名 * @param Pw ...
- MATLAB图像处理工具箱
下列表格中除了个别函数外,其余函数都是图像处理工具箱提供的关于图像处理的函数,现摘录到此以备查找. 表1 图像显示 函数名 功能说明 函数名 功能说明 colorbar 颜色条显示 montage 按 ...
- QCon2013上海站总结 -- 前端开发
选择这个专题开始主要有两个原因:一是这次会议关于前端开发的内容不多.二是我做过几年前端开发,这个专题对我来说会容易点:) 这次QCon上海关于前端开发有一个Keynote,一个Javascript专题 ...