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. vue cli webstorm

    贫富差距的产生 – 目光博客 http://eyehere.net/2017/1947/ https://github.com/vuejs/vue Vue 2.0 Hello World - JSFi ...

  2. JDK8中函数式流编程推荐

    强烈推荐使用Java8中函数流API库来处理集合相关的数据,今天又看来一个项目代码里面用到来很多这样的处理,基本上可以解决大部分遍历问题.并且代码简洁清晰. JAVA8与lambda表达式 JDK8  ...

  3. LIBTOOL is undefined 问题的解决方法

    configure.ac:10: error: possibly undefined macro: AC_PROG_LIBTOOL If this token and others are legit ...

  4. 【已解决】Makefile执行过程中出错:make: *** No rule to make target ` ‘, needed by xxx. Stop(转载)

    转自: http://www.crifan.com/makefile_error_make_no_rule_to_make_target_needed_by_stop/ [问题] 有个已有的Makef ...

  5. codeforces——数学

    codeforces 805A     http://codeforces.com/problemset/problem/805/A /* 题意:输入两个整数l,r,让你找一个因子 使得[l,r]里面 ...

  6. idea ssm项目移包报错问题

    写完代码之后发现包结构太乱了  想要规划一下  结果报错 这里面的包路径都可以点进去,还是报找不到com.lf.company.entity.Business 后来发现是 在移动前和移动后都存在这个m ...

  7. JavaScript--编程挑战

    小伙伴们,请编写"改变颜色"."改变宽高"."隐藏内容"."显示内容"."取消设置"的函数,点击相应 ...

  8. BZOJ 3625 多项式求逆+多项式开根

    思路: RT //By SiriusRen #include <bits/stdc++.h> using namespace std; <<,mod=; int A[N],C[ ...

  9. dedecms:解析Robots.txt 协议标准

    Robots.txt 是存放在站点根目录下的一个纯文本文件.虽然它的设置很简单,但是作用却很强大.它可以指定搜索引擎蜘蛛只抓取指定的内容,或者是禁止搜索引擎蜘蛛抓取网站的部分或全部内容. 下面我们就来 ...

  10. Android 微信分享图片

    "; //微信 APPID private IWXAPI iwxapi; private void regToWx() { iwxapi = WXAPIFactory.createWXAPI ...