Fling!

本文介绍了一款名为Flingis的手机谜题游戏,玩家需通过移动毛球将其从棋盘上移除,直到只剩下一个或没有毛球。采用深度优先搜索算法解决了游戏中的谜题,并提供了输入输出样例及核心代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

算法:深搜

很不错的一道题!!!

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
代码:

 #include <iostream>
 #include <string>
 #include <iomanip>
 #include <cstring>
 #include <algorithm>
 #include <stdio.h>
 using namespace std;
 char ch[10][10];
 int cur[4][2] = {{-1,0},{0,-1},{0,1},{1,0}}; //U、L、R、D 
 char ch1[] = "ULRD";
 int n=7,m=8,cnt;
 int path[15];int pathc[15];
 int cmp(int bx,int by)  
{  
    if(bx<0||by<0||bx>=n||by>=m)  
        return 0;  
    return 1;  
}  
 int dfs(int ax)
 {
 	if(ax==cnt-1)
 		return 1;

	int tx[15],ty[15],i,j,k,dx,dy;
	for( i=0;i<n;i++)
	{
	    for(j=0;j<m;j++)
	    if(ch[i][j]=='O')
	    {
	        
	    	for(k=0;k<4;k++)
	    	{
			    int mo=0;int cd=0;
	    	    dx=i+cur[k][0];
	    	    dy=j+cur[k][1];
	    		if(ch[dx][dy]=='O')
	    		     continue;
	    		while(cmp(dx,dy))
	    		{
	    			if(ch[dx][dy]=='O')
	    			{
	    				mo=1;
	    				tx[cd]=dx;
	    				ty[cd++]=dy;
					}
					dx+=cur[k][0];
	    			dy+=cur[k][1];
				}
				if(mo)
				{
				    ch[i][j]='X';
					for(int ii=0;ii<cd;ii++)
					{
						ch[tx[ii]][ty[ii]]='X';
						ch[tx[ii]-cur[k][0]][ty[ii]-cur[k][1]]='O';
					}
				    
					path[ax]=i*m+j;
					pathc[ax]=k;
					if(dfs(ax+1)) return 1;
					ch[i][j]='O';
					dx=i+cur[k][0];
					dy=j+cur[k][1];
					while(cmp(dx,dy))
					{
						if(ch[dx][dy]=='O')
							ch[dx][dy]='X';
						dx+=cur[k][0];
						dy+=cur[k][1];
					}
					for(int ii=0;ii<cd;ii++)  
                            ch[tx[ii]][ty[ii]]='O';
				}
			}
		}
	}
	return 0;
 }
 int main()
 {
 	int i,j,p=0;
 	while(~scanf("%s",&ch[0]))
 	{
 		for(i=1;i<n;i++)
		     cin>>ch[i];
		cnt=0;
	    for(i=0;i<n;i++)
	        for(j=0;j<m;j++)
	            if(ch[i][j]=='O')
	            cnt++;
	    dfs(0);
	    if(p) cout<<endl;
	    cout<<"CASE #"<<++p<<":"<<endl;
	    for(i=0;i<cnt-1;i++)
	    cout<<path[i]/m<<" "<<path[i]%m<<" "<<ch1[pathc[i]]<<endl;
	}
	return 0;
 }
 
  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值