Fling

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 455    Accepted Submission(s): 190

Problem Description
Fling is a kind of puzzle games available on phone.
This game is played on a board with 7 rows and 8 columns. Each puzzle consists of a set of furballs placed on the board. To solved a puzzle, you need to remove the furballs from board until there is no more than one furball on the board. You do this by ´flinging´ furballs into other furballs, to knock them off the board. You can fling any furballs in four directions (up, left, right, down). The flung furball stops at the front grid of another one as soon as knocking it. And the knocked furball continues to rolling in the same direction until the last knocked one goes off the board. For instance, A furball at (0, 0) rolls right to the furball at (0, 5), then it will stop at (0, 4). Moreover, the latter will roll to right. You cannot fling a furball into a neighbouring furball, the one next to in any of four directions. However, it is permitted for a rolling ball knocks into a ball with a neighbour in that direction.

 
Input
The input contains multiple test cases.
For each case, the 7 lines with 8 characters describe the board. ´X´ represents a empty grid and ´O´ represents a grid with a furball in it. There are no more than 12 furballs in any board.
Each case separated by a blank line.

 
Output
For each case, print a line formatted as "CASE #NUM:", where NUM is the number of current case.
Then every ´fling´ prints a line. Each line contains two integers X, Y and a character Z. The flung furball is located at grid (X, Y), the top-left grid is (0, 0). And Z represents the direction this furball towards: U (Up), L (Left), R (Right) and D (Down);
Print a blank line between two cases.
You can assume that every puzzle could be solved.
If there are multiple solve sequences, print the smallest one. That is, Two sequences A (A1, A2, A3 ... An) and B (B1, B2, B3 ... Bn). Let k be the smallest number that Ak != Bk.
Define A < B :
(1) X in Ak < X in Bk;
(2) Y in Ak < Y in Bk and X in Ak = X in Bk;
(3) Z in Ak < Z in Bk and (X,Y) in Ak = (X,Y) in Bk;
The order of Z: U < L < R < D.

 
Sample Input
XXXXXXXX XXOXXXXX XXXXXXXX XXXXXXXX XOXXXXOX XXXXXXXX XXXXXXXX XXXXXXXX XOXOXOOX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
Sample Output
CASE #1:
4 6 L
1 2 D
CASE #2:
1 1 R
1 4 L
1 3 R
 
给出在一个7*8方块上的很多毛球,允许的操作只有用一个毛球取弹另一个毛球,最终的状态是只剩下一个球
其中:
不能直接把毛球弹出去
假如相邻也是一个毛球,则不能往这个方向弹(可理解为无法蓄力)。
使用dfs()进行搜索,对一个球弹或者不弹进行搜索,记住要使用一个空地图来存储决策前的地图,以便在搜索方向出错是进行回退
 
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
struct node
{
int x,y,z;
}ans[];
char mat[][];
char direc[]={'U','L','R','D'};
int nx[]={-, , , };
int ny[]={ ,-, , };
void fling(int x,int y,int k)
{
int flag=;
int nxtx=x+nx[k],nxty=y+ny[k];
if(nxtx<||nxtx>=||nxty<||nxty>=) {mat[x][y]='X';return;}
while(mat[nxtx][nxty]=='X')
{
nxtx+=nx[k];nxty+=ny[k];
if(nxtx<||nxtx>=||nxty<||nxty>=) {flag=;break;}
}
if(flag) {mat[x][y]='X';return;}
mat[x][y]='X';
mat[nxtx-nx[k]][nxty-ny[k]]='O';
fling(nxtx,nxty,k);
} bool dfs(int sum,int cnt)
{
if(sum==)
{
return ;
}
int nxtx,nxty;
char t_mat[][];
memcpy(t_mat,mat,sizeof(mat));
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
if(mat[i][j]!='O') continue;
for(int k=;k<;k++)
{
nxtx=i+nx[k];nxty=j+ny[k];
if(nxtx<||nxtx>=||nxty<||nxty>=) continue;
if(mat[nxtx][nxty]=='O') continue;
int flag=;
while(mat[nxtx][nxty]=='X')
{
nxtx+=nx[k];nxty+=ny[k];
if(nxtx<||nxtx>=||nxty<||nxty>=)
{
flag=;break;
}
}
if(flag) continue;
mat[i][j]='X';
mat[nxtx-nx[k]][nxty-ny[k]]='O';
fling(nxtx,nxty,k);
ans[cnt].x=i;
ans[cnt].y=j;
ans[cnt].z=k;
if(dfs(sum-,cnt+)) return ;
memcpy(mat,t_mat,sizeof(t_mat));
}
}
}
return ; }
int main()
{
int ca=;
while(scanf("%s",mat[])!=EOF)
{
int sum=;
for(int i=;i<;i++) scanf("%s",mat[i]);
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
if(mat[i][j]=='O')
{
sum++;
}
}
}
memset(ans,-,sizeof(ans));
dfs(sum,);
if(ca!=) puts("");
printf("CASE #%d:\n",ca++);
for(int i=;ans[i].x!=-;i++)
{
printf("%d %d %c\n",ans[i].x,ans[i].y,direc[ans[i].z]);
}
}
return ;
}

hdu 3500 DFS(限定)的更多相关文章

  1. hdu 2782 dfs(限定)

    The Worm Turns Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. HDU 5143 DFS

    分别给出1,2,3,4   a, b, c,d个 问能否组成数个长度不小于3的等差数列. 首先数量存在大于3的可以直接拿掉,那么可以先判是否都是0或大于3的 然后直接DFS就行了,但是还是要注意先判合 ...

  3. Snacks HDU 5692 dfs序列+线段树

    Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...

  4. hdu 3500 Fling (dfs)

    Fling Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submi ...

  5. HDU 5877 dfs+ 线段树(或+树状树组)

    1.HDU 5877  Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...

  6. hdu 4751(dfs染色)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 思路:构建新图,对于那些两点连双向边的,忽略,然后其余的都连双向边,于是在新图中,连边的点是能不 ...

  7. HDU 1045 (DFS搜索)

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

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

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

  9. HDU 1010 (DFS搜索+奇偶剪枝)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给定起点和终点,问刚好在t步时能否到达终点. 解题思路: 4个剪枝. ①dep&g ...

随机推荐

  1. java 多线程——同步 学习笔记

      一.实例的同步方法 public synchronized void add(int value){ this.count += value; } Java 实例方法同步是同步在拥有该方法的对象上 ...

  2. 树的遍历 迭代算法——思路:初始化stack,pop stack利用pop的node,push new node to stack,可以考虑迭代一颗树 因为后序遍历最后还要要访问根结点一次,所以要访问根结点两次是难点

    144. Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' ...

  3. bzoj1023 [SHOI2008]cactus仙人掌图 & poj3567 Cactus Reloaded——求仙人掌直径

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1023    http://poj.org/problem?id=3567 仙人掌!直接模仿 ...

  4. java线程系列---Runnable和Thread的区别 (转载)

    转自:http://blog.csdn.net/wwww1988600/article/details/7309070 在java中可有两种方式实现多线程,一种是继承 Thread类,一种是实现Run ...

  5. U盘在电脑上安装CentOS 7 系统过程详解

    U盘制作CentOS系统启动盘 在电脑上下载并安装UltraISO软件,如百度云:http://pan.baidu.com/s/1hrGtvEG 打开UltraISO软件,找到CentOS.iso的映 ...

  6. php phppowerpoint

    今天早上从订阅的 Zend DevZone 看到篇很有意思的文章. Creating PowerPoint 2007 files using PHP. 试了一下. 果然很又意思, 分享给大家吧. 程序 ...

  7. [Swift通天遁地]八、媒体与动画-(4)给相机添加CoreImage滤镜效果

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  8. SpringBoot集成CAS单点登录,SSO单点登录,CAS单点登录(视频资料分享篇)

    单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一.SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统. 很早期的公司 ...

  9. $P5240 Derivation$

    神仙题. 第一场月赛的题目我到第二场月赛完了才写[由此可见我是真的菜 题目就是个大模拟加乘显然,幂的话需要将原函数.导函数的函数值用扩展欧拉定理展开 \(log\) 层.时间复杂度 \(O(T |S| ...

  10. jmeter关联、下载文件、简单压测

    关联 一.什么是关联 关联是请求与请求之间存在数据依赖关系,需要从上一个请求获取下一个请求需要回传回去的数据. 简单地说就是在测试过程中有些数据的值会经常发生变化,要获取并使用这些数据,把这个动态的信 ...