hdu 3500 DFS(限定)
Fling
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 455 Accepted Submission(s): 190
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.

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.
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.
#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(限定)的更多相关文章
- hdu 2782 dfs(限定)
The Worm Turns Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 5143 DFS
分别给出1,2,3,4 a, b, c,d个 问能否组成数个长度不小于3的等差数列. 首先数量存在大于3的可以直接拿掉,那么可以先判是否都是0或大于3的 然后直接DFS就行了,但是还是要注意先判合 ...
- Snacks HDU 5692 dfs序列+线段树
Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...
- hdu 3500 Fling (dfs)
Fling Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submi ...
- HDU 5877 dfs+ 线段树(或+树状树组)
1.HDU 5877 Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...
- hdu 4751(dfs染色)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 思路:构建新图,对于那些两点连双向边的,忽略,然后其余的都连双向边,于是在新图中,连边的点是能不 ...
- HDU 1045 (DFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意:在不是X的地方放O,所有O在没有隔板情况下不能对视(横行和数列),问最多可以放多少个 ...
- HDU 1241 (DFS搜索+染色)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1241 题目大意:求一张地图里的连通块.注意可以斜着连通. 解题思路: 八个方向dfs一遍,一边df ...
- HDU 1010 (DFS搜索+奇偶剪枝)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给定起点和终点,问刚好在t步时能否到达终点. 解题思路: 4个剪枝. ①dep&g ...
随机推荐
- 特征变化--->索引到标签的转换(IndexToString)
package Spark_MLlib import org.apache.spark.ml.feature.{IndexToString, StringIndexer} import org.apa ...
- 91. Ext中获取combobox中的valueField和displayField的值
转自:https://blog.csdn.net/jcy472578/article/details/42113119Ext.getCmp("schemaVersion").val ...
- 父页面调用子页面js的方法
iframe子页面调用父页面javascript函数的方法今天遇到一个iframe子页面调用父页面js函数的需求,解决起来很简单,但是在chrome浏览器遇到一点小问题.顺便写一下iframe的父页面 ...
- input的type等于mage标签引发的页面提交!
主要原因是使用了<input type="image",而且放在了<form中. <input type="image"标签类似<inp ...
- 14款形态各异的超时尚HTML5时钟动画
14款超时尚的HTML5时钟动画(附源码) 时钟动画在网页应用中也非常广泛,在一些个人博客中,我们经常会看到一些相当个性化的HTML5时钟动画.今天我们向大家分享了14款形态各异的超时尚HTML5 ...
- [Apple开发者帐户帮助]八、管理档案(2)创建临时配置文件(iOS,tvOS,watchOS)
创建临时配置文件以在设备上运行您的应用程序而无需Xcode.在开始之前,您需要一个App ID,一个分发证书和多个注册设备. 有关完整的临时配置文件工作流程,请转到Xcode帮助中的分发到已注册设备( ...
- EF 批量插入,sqlhelper 批量插入
需添加一个using System.Linq; 引用 public void BulkInsert<T>(string connection, string tableName, ILis ...
- Java系列学习(十二)-开始Eclipse
1.用Eclipse来写一个HelloWorld (1)选择工作空间 工作空间其实就是我们写的源代码所在的目录 (2)创建一个Java项目 [File-New-Java Project] (3)创建包 ...
- [转帖]c++ 面试整理
1. 继承方式 public 父类的访问级别不变 protected 父类的public成员在派生类编程protected,其余的不变 private 父类的所有成员变成pr ...
- eclipse整合maven下载jar包速度慢问题解决
引用:http://blog.csdn.net/u010154380/article/details/70339538 开发过程中在pom.xml中添加pom的时候,默认是需要从中央仓库中下载,但是下 ...