Cash Cow【dfs较难题应用】【sdut2721】

Cash Cow

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

题目链接:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2721

Years before Candy Crush became the wildly popular game that may lead developer Saga to a multi-billion dollar IPO, there was an online game named Cash Cow, which remains part of the Webkinz platform.

This single-player game has a board with 12 rows and 10 columns, as shown in Figure 1. We label the rows 1 through 12, starting at the bottom, and the columns a through j, starting at the left. Each grid location can either have a colored circle or be empty. (We use uppercase characters to denote distinct colors, for example with B=blue, R=red, and Y=yellow.) On each turn, the player clicks on a circle. The computer determines the largest "cluster" to which that circle belongs, where a cluster is defined to include the initial circle, any of its immediate horizontal and vertical neighbors with matching color, those circles\' neighbors with matching colors, and so forth. For example, if a user were to click on the blue circle at cell (h10) in Figure 1, its cluster consists of those cells shown with empty circles in Figure 2.
               Cash Cow【dfs较难题应用】【sdut2721】

Processing a click on cell h10.

The player\'s turn is processed as follows. If the indicated grid cell belongs to a cluster of only one or two circles (or if there is no circle at that cell), the turn is wasted. Otherwise, with a cluster of 3 or more circles, all circles in the cluster are removed from the board. Remaining circles are then compacted as follows:

  1. Circles fall vertically, to fill in any holes in their column.
  2. If one or more columns have become empty, all remaining columns slide leftward (with each nonempty column remaining intact), such that they are packed against the left edge of the board.

For example, Figure 3 shows the board after the cluster of Figure 2 was removed after the click on (h10).

 

As another example, Figure 4 below, portrays the processing of a subsequent click on cell (j1). During that turn, column (e) becomes empty, and the resulting columns (f) through (j) slide to become columns (e) through (i), respectively. Figure 5 provides one further example in which several columns are compacted.
                       Cash Cow【dfs较难题应用】【sdut2721】
                             Cash Cow【dfs较难题应用】【sdut2721】

输入

 The input will consist of multiple games, each played with a new board. For each game, the input begins with a number T that denotes the number of turns that the player will be making, with 1 ≤ T ≤ 20. Following that will be an initial board configuration, which always has 12 rows and 10 columns per row, with uppercase letters used to denote distinct colors. There will never be empty cells within the initial board. Following the presentation of the initial board will be T additional lines of input, each designating a cell of the grid; we rely on the coordinate system illustrated in the above figures, with a lowercase letter, from a to j, denoting a column and a number from 1 to 12 that denotes a row. We note that if a player clicks on a grid cell that does not currently have any circle, that turn is simply wasted.

The end of the entire input will be designated by a line with the number 0.

输出

 For each game, output a single line designating the the number of circles that remain on the board after all of the player\'s turns are processed.

示例输入

3

RYBBRBYYRY

RRRBBBBBRR

YRRBRBBBBR

RYYBRYYRYY

BRBBRBRBRY

YYBYRBBRRB

RYBBBBRYYY

YBRBRBRYRB

RYBBBBBBBY

YBBRRRRRBB

RBBRRBRYRR

BBBRRYYYRR

h 10

j 1

g 2

3

YYYYYBBBBB

YYYYYBBBBB

YYYYYBBBBB

YYYYYBBBBB

YYYYYBBBBB

YYYYYBBBBB

YYYYYBBBBB

YYYYYBBBBB

YYYYYBBBBB

YYYYYBBBBB

YYBYYBBBBB

YYBYYBBBBB

c 2

c 12

g 1

2

YYYYYBBBBB

YYYYYBBBBB

YYYYYBBBBB

YYYYYBBBBB

YYYYYBBBBB

YYYYYBBBBB

YYYYYBBBBB

YYYYYBBBBB

YYYYYBBBBB

YYYYYBBBBB

YYBYYBBBBB

YYBYYBBBBB

g 1

c 12

0

示例输出

33

62

2

提示

 

来源

中国海洋大学第四届朗讯杯高级组

示例程序

 题目大意:
输入一个整数n,代表点击的次数;输入一个12行10列的矩阵,里面有120个小球,点击n次小球,若是与小球相邻的小球有两个或者两个以上和这个小球相同,则全部消掉这些小球,,让小球落下,观察小球落下以后的矩阵里面有没有空列,若是有,则把空列全部消掉,然后继续点击小球,重复n次,最后输出剩余小球的个数。
输入以0为结束标志。
做题过程:做这道题目最关键的是要分步做,一步一步来,不能一下子全部把代码敲出来,最后才调试找错,那样的话最后的出现的错误会非常多,这道题目并不难,一点一点做的话虽然很耗时间,但是毕竟还是能够做出来的,这道题目我总共花了4个小时左右才做出来~。
  1 #include<stdio.h>

  2 #include<iostream>

  3 #include<string.h>

  4 #include<string>

  5 #include<stdlib.h>

  6 using namespace std;

  7 void dfs(int x,int y);

  8 void dfs1(int x,int y);

  9 char f[50][50];

 10 int hx[1111];

 11 int zo,sum;

 12 int visited[50][50];

 13 int main()

 14 {

 15     int n;

 16     while(cin>>n)

 17     {

 18         if(n==0)

 19             break;

 20         zo=9;

 21         memset(f,'0',sizeof(f));

 22         memset(hx,0,sizeof(hx));

 23         int i,j,k;

 24         for(i=0; i<=11; i++)

 25             cin>>f[i];

 26         for(i=0; i<=11; i++)

 27             for(j=0; j<=9; j++)

 28                 hx[f[i][j]-'A']++;

 29         for(i=1; i<=n; i++) //输入n次横纵坐标,点击n次

 30         {

 31             sum=0;

 32             memset(visited,0,sizeof(visited));

 33             char ch;

 34             int y;

 35             cin>>ch>>y;

 36             dfs1(12-y,ch-'a');

 37             if(sum==3)

 38             dfs(12-y,ch-'a');

 39             else

 40             {

 41                 //cout<<"sum="<<sum<<endl;

 42                 continue;

 43             };

 44             /*cout<<"验证输出dfs"<<endl;

 45             for(j=0; j<=11; j++)

 46                 cout<<f[j]<<endl;

 47             cout<<"小球落下"<<endl;*/

 48             for(j=0; j<=9; j++) //zo+1列小球落下

 49             {

 50                 for(k=11; k>=0; k--)

 51                 {

 52                     if(f[k][j]=='0')

 53                     {

 54                         int t=k,s=k-1;

 55                         for(; s>=0; s--)

 56                         {

 57                             if(f[s][j]!='0')

 58                             {

 59                                 f[t][j]=f[s][j];

 60                                 f[s][j]='0';

 61                                 break;

 62                             }

 63                         }

 64                     }

 65                 }

 66             }

 67             /*for(j=0; j<=11; j++)

 68                 cout<<f[j]<<endl;

 69             cout<<"合并"<<endl;

 70             cout<<"原来的列数:"<<zo+1<<endl;*/

 71             for(j=0; j<=zo; j++) //从第一列开始找空列

 72             {

 73                 char ch=f[0][j];

 74                 if(ch=='0')

 75                 {

 76                     for(k=0; k<=11; k++)

 77                     {

 78                         if(ch!=f[k][j])

 79                             break;

 80                     }

 81                     if(k==12)//找到了空列,开始合并

 82                     {

 83                         int w,r;

 84                         for(w=0; w<=11; w++)

 85                         {

 86                             for(r=j; r<=zo-1; r++)

 87                             {

 88                                 f[w][r]=f[w][r+1];

 89                             }

 90                             f[w][zo]='\0';

 91                         }

 92                         zo--;

 93                         j--;

 94                     }

 95                 }

 96             }

 97             /*cout<<"现在的列数:"<<zo+1<<endl;

 98             for(j=0; j<=11; j++)

 99                 cout<<f[j]<<endl;*/

100         }

101         int sum=0;

102         for(j=0; j<=1000; j++)

103             if(hx[j]!=0)

104                 sum+=hx[j];

105         cout<<sum<<endl;

106     }

107 }

108 int h[]= {0,-1,0,1},z[]= {-1,0,1,0};

109 void dfs(int x,int y)

110 {

111     if(f[x][y]=='0')return ;

112     char ch=f[x][y];

113     hx[ch-'A']--;

114     f[x][y]='0';

115     int i,heng,zong;

116     for(i=0; i<=3; i++)

117     {

118         heng=h[i]+x;

119         zong=z[i]+y;

120         if(heng<0||heng>11||zong<0||zong>zo)

121             continue;

122         else

123         {

124             if(f[heng][zong]==ch)

125                 dfs(heng,zong);

126             else continue;

127         }

128     }

129 }

130 void dfs1(int x,int y)

131 {

132     if(sum==3)

133         return ;

134     else

135     {

136         sum++;

137         if(sum==3)return;

138     }

139     visited[x][y]=1;

140     int i;

141     int heng,zong;

142     char ch=f[x][y];

143     for(i=0;i<=3;i++)

144     {

145         heng=x+h[i];

146         zong=y+z[i];

147         if(heng<0||heng>11||zong<0||zong>zo)

148             continue;

149         else

150         {

151             if(visited[heng][zong]==0)

152             {

153                 if(ch==f[heng][zong])

154                     dfs1(heng,zong);

155                 else continue;

156             }

157         }

158     }

159 }
View Code

 

你可能感兴趣的:(cas)