hdu 3500 Fling (dfs)

Fling

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 354    Accepted Submission(s): 143


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
 
Author
EvilSeraph
 
Source
 
Recommend
zhouzeyong   |   We have carefully selected several similar problems for you:  3504 3506 3507 3501 3502 
 
 
这题和hdu
2821 Pusher
类似。
开始时不知道怎么处理比较好,先打算用个图保存,然后每个’O‘点另用数组保存,再深搜保存的点,这是处于对时间复杂度的优化;后来发现很难实现,于是就用下面这种方法,
比较暴力,直接暴力7*8的图,这题和之前做的2821 Pusher有点类似,而且数据也不难过,就是写的时候出了点小问题,后来1Y了,感觉还行。
 
 
 1 //625MS    248K    2295 B    G++ 
 2 #include<stdio.h>
 3 #include<string.h>
 4 char g[10][10];
 5 int ans[15][3];
 6 char op[5]="ULRD";
 7 int mov[4][2]={-1,0,0,-1,0,1,1,0};
 8 int n,flag;
 9 int judge(int x,int y)
10 {
11     if(x>=0 && x<7 && y>=0 && y<8) return 1;
12     return 0;
13 }
14 void dfs(int cnt)
15 {
16     if(cnt==n-1) flag=1;
17     if(flag) return;
18     for(int i=0;i<7;i++)
19         for(int j=0;j<8;j++)
20             if(g[i][j]=='O'){
21                 for(int k=0;k<4;k++){
22                     int x=i;
23                     int y=j;
24                     if(!judge(x+mov[k][0],y+mov[k][1]) || g[x+mov[k][0]][y+mov[k][1]]=='O') continue;
25                     int tx[10],ty[10],tt=0;
26                     while(judge(x,y)){
27                         if(g[x][y]=='O'){
28                             tx[tt]=x;
29                             ty[tt++]=y;
30                         }
31                         x+=mov[k][0];
32                         y+=mov[k][1];
33                     }
34                     if(tt==1) continue;    
35                          
36                     for(int ii=1;ii<tt;ii++){
37                         g[tx[ii-1]][ty[ii-1]]='X';
38                         g[tx[ii]-mov[k][0]][ty[ii]-mov[k][1]]='O';
39                     }
40                     g[tx[tt-1]][ty[tt-1]]='X';
41                     ans[cnt][0]=i;
42                     ans[cnt][1]=j;
43                     ans[cnt][2]=k;
44 
45                     dfs(cnt+1);
46                     if(flag) return;
47                     
48                     x=i;y=j;
49                     while(judge(x,y)){
50                         g[x][y]='X';
51                         x+=mov[k][0];
52                         y+=mov[k][1];
53                     }
54                     for(int ii=0;ii<tt;ii++)
55                         g[tx[ii]][ty[ii]]='O';
56                 }
57             }
58     return;
59 }
60 int main(void)
61 {
62     int m,k=1;
63     while(scanf("%s",g[0])!=EOF)
64     {
65         for(int i=1;i<7;i++) scanf("%s",g[i]);
66         if(k>1) printf("
");
67         n=0;
68         flag=0;
69         for(int i=0;i<7;i++)
70             for(int j=0;j<8;j++)
71                 if(g[i][j]=='O')
72                     n++;
73         dfs(0);
74         printf("CASE #%d:
",k++);
75         for(int i=0;i<n-1;i++){
76             printf("%d %d %c
",ans[i][0],ans[i][1],op[ans[i][2]]);
77         }
78     }
79     return 0;
80 }
原文地址:https://www.cnblogs.com/GO-NO-1/p/3628182.html