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. OAuth 2.0攻击面与案例总结

    本文整理了OAuth 2.0的攻击面+实际案例+辅助测试代码. OAuth流程 本文以两种广泛使用的方案为标准展开..如对流程不了解,请先移步学习: 理解OAuth 2.0 Authorization ...

  2. 7.在AngularJS视图中实现指令

    指令扩展了HTML的行为.可以创建自定义的HTML元素,属性和特定于应用程序的类与功能. 1.了解指令 指令是AngularJS模板标记和用于支持的JavaScript代码的组合.AngularJS指 ...

  3. 前端之js的常用用法

    js生成标签 // 将标签添加到i1里面 var tag = document.createElement('input'); tag.setAttribute('type', 'text'); ta ...

  4. Java微博搜索关键字采集

    import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...

  5. PHP网页缓存技术

    http://blog.sina.com.cn/s/blog_646e51c40100weu9.html 前台静态化:把动态页面解析后保存为静态页面 文件缓存:把查询结果保存为文件,XML 内存缓存: ...

  6. 基础03 JVM到底在哪里?

    1.Java是编译型语言还是解释型语言? 是解释型定义: 编译型语言:把做好的源程序全部编译成二进制代码的可运行程序.然后,可直接运行这个程序. 解释型语言:把做好的源程序翻译一句,然后执行一句,直至 ...

  7. Delphi中Format与FormatDateTime函数详解

    copy:http://hi.baidu.com/yunfanleo/blog/item/0c51d9cdbc0531550eb34558.html Format是一个很常用,却又似乎很烦的方法,本人 ...

  8. mybatis,批量新增、修改,删除

    转载自:http://blog.csdn.net/sanyuesan0000/article/details/19998727 最近需要用到Mybatis批量新增oracle数据库,刚开始在网上找到的 ...

  9. asp.net mvc htmlHelper

    ASP.NET MVC 3.0 HTML辅助方法   HTML辅助方法(html helper)是用来帮助生成HTML的方法. 1.HTML辅助方法应用实例 ◊ 生成form元素 @using (Ht ...

  10. 【hadoop2.6.0】安装+例子运行

    由于下载的是hadoop的最新版,网上各种杂七杂八的东西都不适用.好在官网上说的也够清楚了.如果有人看这篇文章的话,最大的忠告就是看官网. 官网2.6.0的安装教程:http://hadoop.apa ...