这道题目由于每走一步的时候毛毛球是可以变换的 换言之 主体不唯一 所以这里搜索的设计有变化

再就是几个回溯的过程要注意。,。  小心使得万年船

#include <iostream>
#include <cstdio>
using namespace std;
char
map[][];
int
num;
int
flag;
int
mmove;
int
dir[][] = {{-,},{,-},{,},{,}}; //U、L、R、D
char tow[] = {'U','L','R','D'}; struct node
{

int
x,y;
int
direction;
}
path[]; int is_in(int x, int y) //判断是否在游戏界面中
{
if
(x >= && x <= && y >= && y <=)
return
;
else
return
;
}
void dfs(int cur)
{

if
(cur == num)
{

//走了num-1步之后直接返回
/*
没成功走一次,都会把一个毛毛球推出游戏界面,所以最多只可能走num-1步
*/
flag =; //已经走完了相应的步数
return;
}
for(int i=; i<=; i++)
{

for
(int j=; j<=; j++)
{

if
(map[i][j] == 'O') //按顺序进行搜索,如果搜索到了一个毛毛球,就看它是否能够移动
{
mmove =;
for
(int d=; d<; d++)
{

int
nx = i + dir[d][];
int
ny = j + dir[d][];
if
(is_in(nx, ny) && map[nx][ny] == 'O') //前方有毛毛球阻碍无法移动,直接continue选取下一个方向
continue; /*
while循环这部分处理是关键,这里主要要处理好手拨动的毛毛球遇到另外一个球时要处理的状况,以及
撞击力量的传递处理。
*/
while
(is_in(nx, ny))
{

nx += dir[d][];
ny += dir[d][]; /* 遇到毛毛球k,其前面那个必然要为'O',而这个毛毛球k必须变为'X'。
通过while循环就可以处理好遇到毛毛球的情况,并且处理好撞击传递
的情况,例如第二组数据的撞击传递,所以这里设计的还是很巧妙的。
*/ if(map[nx][ny] == 'O')
{

mmove =; //手拨动的毛毛球map[i][j]是可以移动的
map[nx][ny] = 'X';
map[nx - dir[d][]][ny - dir[d][]] = 'O';
}
}
if(mmove) //可以移动,进行记录
{
map[i][j] = 'X'; //移动之后原来的位置当然要置为'X'
path[cur].x = i;
path[cur].y = j;
path[cur].direction = d; dfs(cur +);
//回溯之后
if(flag ==) //已经走完了就应该直接返回
return; //反方向走
nx -= dir[d][];
ny -= dir[d][];
while
(nx != i || ny != j) //一直会推到手拨动球的起点map[i][j]
{
if
(map[nx][ny] == 'O') /*这说明是前面的球移动到此处的一个位置,
所以该位置还原成'X',而它后面的那个位置应该
还原成'O'
*/
{

map[nx][ny] = 'X';
map[nx + dir[d][]][ny + dir[d][]] = 'O';
}

nx -= dir[d][];
ny -= dir[d][]; }
map[i][j] = 'O'; //起点还原

} } }
}
}
}
int main()
{

int
kase =;
while
(scanf("%s",map[]) != EOF)
{

num = flag =;
for
(int i=; i<=; i++)
{

scanf("%s",map[i]);
}
for(int i=; i<=; i++)
{

for
(int j=; j<=; j++)
{

if
(map[i][j] == 'O')
{

num++; //记录毛毛球的个数
}
}
}
dfs();
if
(kase !=)
cout<<endl;
cout<<"CASE #"<<kase++<<":"<<endl;
for
(int i =; i<=num-; i++)
{

cout<<path[i].x<<" "<<path[i].y<<" "<<tow[path[i].direction]<<endl;
}
}

return
;
}

hdu 3500 还是搜索的更多相关文章

  1. hdu 5468(莫比乌斯+搜索)

    hdu 5468 Puzzled Elena   /*快速通道*/ Sample Input 5 1 2 1 3 2 4 2 5 6 2 3 4 5   Sample Output Case #1: ...

  2. HDU 4499.Cannon 搜索

    Cannon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Subm ...

  3. HDU 1045 (DFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意:在不是X的地方放O,所有O在没有隔板情况下不能对视(横行和数列),问最多可以放多少个 ...

  4. HDU 1180 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...

  5. HDU 2531 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...

  6. HDU 1026 (BFS搜索+优先队列+记录方案)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...

  7. HDU 1312 (BFS搜索模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:问迷宫中有多少个点被访问. 解题思路: DFS肯定能水过去的.这里就拍了一下BFS. ...

  8. HDU 1242 (BFS搜索+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目大意:多个起点到一个终点,普通点耗时1,特殊点耗时2,求到达终点的最少耗时. 解题思路: ...

  9. HDU 1241 (DFS搜索+染色)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1241 题目大意:求一张地图里的连通块.注意可以斜着连通. 解题思路: 八个方向dfs一遍,一边df ...

随机推荐

  1. 互联网IT当线上出现 bug 时,是怎么处理的?

    线上BUG说处理方法:1.关于线上BUG问题,目前公司有一整套线上故障流程规范,包括故障定义.定级.处理流程.故障处理超时升级机制.故障处理小组.故障处罚(与故障存在时长有关)等:2.最主要的是,线上 ...

  2. fsLayui数据表格使用

    fsLayui 是一个基于layui的快速开发插件,支持数据表格增删改查操作,提供通用的组件,通过配置html实现数据请求,减少前端js重复开发的工作. GitHub下载 码云下载 测试环境地址:ht ...

  3. error:Cannot pull with rebase

    原文文链接:https://blog.csdn.net/u012385190/article/details/70670213git 执行git pull –rebase报错误如下: error: C ...

  4. ubuntu 上开发.netcore

    ubuntu需要安装的软件: 1.sudo apt-get install openssh-server openssh-client 2.sudo apt-get git 3.安装vscode 4. ...

  5. Rect和RectF函数

    1.是否包含点,矩形 判断是否包含某个点 boolean contains(int x,int y)  函数用于判断某个点是否在当前矩形中,如果在,则返回true ,不在则false 2.判断是否包含 ...

  6. for-update与for-update nowait

    1.for update 和 for update nowait 的区别: 首先一点,如果只是select 的话,Oracle是不会加任何锁的,也就是Oracle对 select 读到的数据不会有任何 ...

  7. C++ STL 排序

    #include <iostream>#include <algorithm>#include <deque>#include <vector>#inc ...

  8. jemter 90%line的解释

    假如: 有10个数: 1.2.3.4.5.6.7.8.9.10    按由大到小将其排列. 求它的第90%百分位,也就是第9个数刚好是9 ,那么他的90%Line 就是9 . 另一组数: 2.2.1. ...

  9. kettle的用法

    一: 从一个数据库导入表的数据到另一个 数据库的表中(表数据同步) 1:在 主对象树-- DB连接 中新建 连接: 在选项中 设置字符集: 2: 在 核心对象中 先增加一个  表输入: 再增加一个 插 ...

  10. ORA-03114: not connected to ORACLE

    PlSql Developer出现这个问题的时候,只要重新连接一些数据库就行了!