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

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

#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. flask 设置配置文件的方式

    from flask import Flask from flask import current_app """ 配置参数设置与读取 """ ...

  2. 课下选做作业MyOD

    2019-2020-1 20175227 <信息安全系统设计基础> 课下选做作业MyOD 要求 复习c文件处理内容 编写myod.c 用myod XXX实现Linux下od -tx -tc ...

  3. 在Winform中屏蔽UnityWebPlayer的右键以及自带Logo解决方案整理

    根据项目的需要,对已经完成的Unity三维模型以及游戏要使用Winform进行包装,也就是使用Winform做一层外壳.因此在展示Unity的时候使用到了UnityWebPlayer这个插件,对于此插 ...

  4. memcached出现:Fatal error: Call to undefined method Memcached::connect()

    今天安装了memcached的服务端和客户端 装好试了一下 $mem = new Memcached;  $mem -> connect("127.0.0.1",11211) ...

  5. (六)爬虫之使用selenium

    selenium是使用javascript编写,主要用来进行web应用程序测试,在python爬虫中可以用来进行动态网页爬取,解决爬虫中的javascript渲染(执行js语句).总结记录下,以备后面 ...

  6. rc.local 注意事項,call python script, file position

    如果要在 rc.local 呼叫 python script python script 的位置需使用絕對路徑 其 python script 裡的有關 file 的位置也需使用 絕對路徑 如果要在 ...

  7. as 什么意思?

    You can denote particular console messages and variable values as having different types using four ...

  8. MongoDB作为windows服务来安装-2

    首先区官网下载对应版本的安装文件,我本地的环境是win7 bit64 我下载的版本是:mongodb-win32-x86_64-2.4.6 ok, 文件下载后,开始安装,这里要说一下,如果直接启动Mo ...

  9. 001-多线程-JUC线程池-线程池架构-Executor、ExecutorService、ThreadPoolExecutor、Executors

    一.概述 1.1.线程池架构图 1. Executor 它是"执行者"接口,它是来执行任务的.准确的说,Executor提供了execute()接口来执行已提交的 Runnable ...

  10. JAXB序列化对象与反序列化XML

    1.什么是JAXB JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术. 该过程中,JAXB也提供 ...