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 ;
}

最新文章

  1. openssl、x509、crt、cer、key、csr、ssl、tls 这些都是什么鬼?
  2. css布局模型
  3. Vcenter server 5.5克隆模板(创建ISO镜像)
  4. datagridview随窗体的大小而变,表格填满控件
  5. Sql语句里的递归查询(转)
  6. JAVA中获取工程路径的方法
  7. plot的实践。
  8. updatepanel刷新后重新加载js脚本问题
  9. Selenium2(java)环境搭建 一
  10. StackExchange.Redis学习笔记(四) 事务控制和Batch批量操作
  11. Visual Studio动态生成版权信息
  12. Java EE 开发环境搭建
  13. centos7与centos6命令区别
  14. python+appium的物理按键代码
  15. 第二十一天,pickle json xml shelve configparser模块
  16. mac os使用迁移助手之后运行php报:dyld相关错误,错误排错流程分析
  17. 关于 Nginx 配置 WebSocket 400 问题
  18. Arduino IDE for ESP8266 ()组网
  19. (转)用graph-easy描绘kubenetes描绘k8s组件逻辑图
  20. bzoj 4004 向量拟阵

热门文章

  1. idea2016的使用心得 --- 太棒了
  2. J - 玩游戏
  3. bzoj2538: [Ctsc2000]公路巡逻
  4. LuoguP4246 [SHOI2008]堵塞的交通
  5. AJAX异步实现
  6. Antenna Placement(二分图的最大匹配)
  7. 互斥的数(hash)
  8. Akka源码分析-Remote-收消息
  9. day03_12/13/2016_bean属性的设置之构造器方式注入
  10. 下载谷歌地图封锁IP解决办法