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.
               

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.
                       
                             

输入

 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个小时左右才做出来~。
 #include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<stdlib.h>
using namespace std;
void dfs(int x,int y);
void dfs1(int x,int y);
char f[][];
int hx[];
int zo,sum;
int visited[][];
int main()
{
int n;
while(cin>>n)
{
if(n==)
break;
zo=;
memset(f,'',sizeof(f));
memset(hx,,sizeof(hx));
int i,j,k;
for(i=; i<=; i++)
cin>>f[i];
for(i=; i<=; i++)
for(j=; j<=; j++)
hx[f[i][j]-'A']++;
for(i=; i<=n; i++) //输入n次横纵坐标,点击n次
{
sum=;
memset(visited,,sizeof(visited));
char ch;
int y;
cin>>ch>>y;
dfs1(-y,ch-'a');
if(sum==)
dfs(-y,ch-'a');
else
{
//cout<<"sum="<<sum<<endl;
continue;
};
/*cout<<"验证输出dfs"<<endl;
for(j=0; j<=11; j++)
cout<<f[j]<<endl;
cout<<"小球落下"<<endl;*/
for(j=; j<=; j++) //zo+1列小球落下
{
for(k=; k>=; k--)
{
if(f[k][j]=='')
{
int t=k,s=k-;
for(; s>=; s--)
{
if(f[s][j]!='')
{
f[t][j]=f[s][j];
f[s][j]='';
break;
}
}
}
}
}
/*for(j=0; j<=11; j++)
cout<<f[j]<<endl;
cout<<"合并"<<endl;
cout<<"原来的列数:"<<zo+1<<endl;*/
for(j=; j<=zo; j++) //从第一列开始找空列
{
char ch=f[][j];
if(ch=='')
{
for(k=; k<=; k++)
{
if(ch!=f[k][j])
break;
}
if(k==)//找到了空列,开始合并
{
int w,r;
for(w=; w<=; w++)
{
for(r=j; r<=zo-; r++)
{
f[w][r]=f[w][r+];
}
f[w][zo]='\0';
}
zo--;
j--;
}
}
}
/*cout<<"现在的列数:"<<zo+1<<endl;
for(j=0; j<=11; j++)
cout<<f[j]<<endl;*/
}
int sum=;
for(j=; j<=; j++)
if(hx[j]!=)
sum+=hx[j];
cout<<sum<<endl;
}
}
int h[]= {,-,,},z[]= {-,,,};
void dfs(int x,int y)
{
if(f[x][y]=='')return ;
char ch=f[x][y];
hx[ch-'A']--;
f[x][y]='';
int i,heng,zong;
for(i=; i<=; i++)
{
heng=h[i]+x;
zong=z[i]+y;
if(heng<||heng>||zong<||zong>zo)
continue;
else
{
if(f[heng][zong]==ch)
dfs(heng,zong);
else continue;
}
}
}
void dfs1(int x,int y)
{
if(sum==)
return ;
else
{
sum++;
if(sum==)return;
}
visited[x][y]=;
int i;
int heng,zong;
char ch=f[x][y];
for(i=;i<=;i++)
{
heng=x+h[i];
zong=y+z[i];
if(heng<||heng>||zong<||zong>zo)
continue;
else
{
if(visited[heng][zong]==)
{
if(ch==f[heng][zong])
dfs1(heng,zong);
else continue;
}
}
}
}

Cash Cow【dfs较难题应用】【sdut2721】的更多相关文章

  1. 中国海洋大学第四届朗讯杯高级组 Cash Cow(模拟)

    题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2721 题意: 给定n个左标,跟那n个坐标 ...

  2. Anti-pattern

    https://en.wikipedia.org/wiki/Anti-pattern https://zh.wikipedia.org/wiki/反面模式 An anti-pattern is a c ...

  3. Anti-pattern(反面模式)

    转自维基百科 http://zh.wikipedia.org/wiki/%E5%8F%8D%E9%9D%A2%E6%A8%A1%E5%BC%8F 在软件工程中,一个反面模式(anti-pattern或 ...

  4. CET4

    Directions: For this part, you are allowed 30 minutes to write a short essay on the challenges of st ...

  5. BZOJ 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐( dfs )

    直接从每个奶牛所在的farm dfs , 然后算一下.. ----------------------------------------------------------------------- ...

  6. 【BZOJ】1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐(dfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1648 水题.. dfs记录能到达的就行了.. #include <cstdio> #in ...

  7. POJ 1985.Cow Marathon-树的直径-树的直径模板(BFS、DFS(vector存图)、DFS(前向星存图))

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 7536   Accepted: 3559 Case ...

  8. [USACO06DEC]牛的野餐Cow Picnic DFS

    题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ 100) cows is grazing in one of N ...

  9. UVa 12118 检查员的难题(dfs+欧拉回路)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

随机推荐

  1. Python 命名空间

    通俗的来说,Python中所谓的命名空间可以理解为一个容器.在这个容器中可以装许多标识符.不同容器中的同名的标识符是不会相互冲突的.理解python的命名空间需要掌握三条规则: 第一,赋值(包括显式赋 ...

  2. Python类的特点 (3) :静态方法与类方法

    Python中的方法有4种: 1)模块中的全局方法,不属于任何类,用"模块名.方法名"形式调用. 2)类中定义的实例方法,也被称为绑定方法(Bound method),这种方法的第 ...

  3. C# 毕业证书打印《六》

    整理思路,从新出发. 加载模版 public void loadtemplate(Label lable) { string p_tempateFile = @"fomate.xml&quo ...

  4. Rubix - ReactJS Powered Admin Template 后台管理框架

    Rubix - ReactJS Powered Admin Template  后台管理框架,使用 ReactJS. http://rubix400.sketchpixy.com/ltr/charts ...

  5. Context.managedQuery()和context.getContentResolver()获取Cursor关闭注意事项

    在获取图片缩略图时,获取游标并进行相关的操作. Cursor cursor = context.getContentResolver().query(MediaStore.Images.Thumbna ...

  6. UIScrollView 的 delaysContentTouches

    UIScrollView 的一段说明: Because a scroll view has no scroll bars, it must know whether a touch signals a ...

  7. hibernate xx(tableName) is not mapped

    数据库中表名是:book,数据库表名不区分大小写的 之后我在hibernate 使用book, String sql="from book"; Query query=sessio ...

  8. 3.nodejs权威指南--文件

    1. 文件 1.1 读写整个文件 1.1.1 读 var fs = require('fs'); fs.readFile('./test.txt',function(err,data){ if(err ...

  9. 多国语言文档识别 ABBYY FineReader Corporate v12.0.101.388.7z 绿色破解版

    ABBYY 是一家俄罗斯软件公司,在文档识别,数据捕获和语言技术的开发中居世界领先地位.其获奖产品 FineReader OCR 软件可以把静态纸文件和 PDF 文件转换成可管理的电子数据,可以大大节 ...

  10. 【linux】linux下运行java程序

    参考了http://www.cnblogs.com/howard-queen/archive/2012/01/30/2331795.html 第一步:用vim先写一个java程序  first.jav ...