4.3.8 Fling

Fling

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

Total Submission(s): 124    Accepted Submission(s): 56

 
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
 
 
      
#include<cstdio>
#include<cstring>
using namespace std;

const int MAXN = 60;
const int n = 7, m = 8;
char g[n][m];
int xx[MAXN], yy[MAXN], num, cnt;
char dd[MAXN], d[] = "ULRD"; 

int dir[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};


bool judge(int x, int y){
	if(x >= n || x < 0 || y >= m || y < 0) return false;
	return true;
}
int dfs(int cur){
	if(cur >= num-1) return 0;
	int x, y;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			if(g[i][j] == 'O'){
				for(int k = 0; k < 4; k++){
					int ok = 0;
					x = dir[k][0] + i;
					y = dir[k][1] + j;	
					if(g[x][y] == 'O') continue;	
					while(judge(x, y)){
						if(g[x][y] == 'O'){
							g[x][y] = 'X';
							g[x - dir[k][0]][y-dir[k][1]] = 'O';
							ok = 1;
						}
						x += dir[k][0];
						y += dir[k][1];
					}	
					
					if(ok){
						xx[cnt] = i;
						yy[cnt] = j;
						dd[cnt] = d[k];
						cnt++;	
						g[i][j] = 'X';
						if(!dfs(cur+1))
							return 0;
						x -= dir[k][0];
						y -= dir[k][1];
						while(x != i || y != j){
							if(g[x][y] == 'O'){
							
								g[x][y] = 'X';
								g[x + dir[k][0]][y + dir[k][1]] = 'O';
							}
							x -= dir[k][0];
							y -= dir[k][1];
						}
						cnt--;
						g[i][j] = 'O';
					}
				}
			}
		}
	}
	return 1;
}
int main(){
	int kase = 0;
	while(scanf("%s", g[0]) != EOF){
		num = cnt = 0;
		for(int i = 1; i < 7; i++){
			scanf("%s", g[i]);
		}
		for(int i = 0; i < n; i++){
			for(int j = 0; j < m; j++){
				if(g[i][j] == 'O') num++;
			}
		}
		dfs(0);
		if(kase > 0) printf("\n");
		printf("CASE #%d:\n", ++kase);
		for(int i = 0; i < cnt; i++){
			printf("%d %d %c\n", xx[i], yy[i], dd[i]);
		}
	}
	return 1;
}

猜你喜欢

转载自blog.csdn.net/bruce_teng2011/article/details/38520199