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 ...
随机推荐
- ios-UI1
// AppDelegate.h // 01-HelloWorld的实现 // /* 新建工程的时候,Use Core Data:比如本地缓存, Include Unit test:增加单元测试 In ...
- bzoj3894: 文理分科(还是那道最小割)
3894: 文理分科 题目:传送门 感谢波老师没有来D飞我,让我做出了这题... 题解: 这题其实和我做的上一题(bzoj2132)很像,所以就不写题意了. 依然是那最小割... 这题给出了四个利益矩 ...
- 使用自定义的AlertDialog。
使用自定义的AlertDialog.1.核心的方法是setView(View view):把自己的view放置到dialog中填充.2.setCanceledOnTouchOutside(boolea ...
- codevs地鼠游戏(贪心)
1052 地鼠游戏 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 王钢是一名学习成绩优异的学生,在平时的学习中,他 ...
- [Swift通天遁地]八、媒体与动画-(3)实现视频播放的水印、Overlay、暂停时插入广告等效果
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- redis取经之路
redis基本数据结构 Redis使用的是自己构建的简单动态字符串(SDS)[simple dynamic string,SDS]的抽象类型,并将SDS用做Rdis的默认字符串表示 redis> ...
- @RequestParam 和 @RequestBody 接受的时间格式
这两个接受的时间格式不相同 首先看一下他们的区别 @RequestParam用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容.(Ht ...
- Kafka详解与总结(一)
1. Kafka概述 1.1. 消息队列 1)点对点模式(一对一,消费者主动拉取数据,消息收到后消息清除) 点对点模型通常是一个基于拉取或者轮询的消息传送模型,这种模型从队列中请求信息,而不是将消息推 ...
- IIS Express配置多站点同时运行
环境:Win10 Pro.Visual Studio 2015 Community.IIS Express 10 VS2015集成IIS Express,所以无需单独下载, 默认安装位置:C:\Pro ...
- Laravel 5.4.36 session 生效问题
在测试过程中发现 如果方法有echo 等函数输出到PHP的输出缓存中 存在 sessionID 不会放到http的请求头中 下次请求也就拿不到sessionid问题 问题的原因 代码位置:publ ...