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

时间:2023-02-16 21:17:13

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个小时左右才做出来~。
 #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&colon; &lbrack;Usaco2006 Dec&rsqb;Cow Picnic 奶牛野餐&lpar; dfs &rpar;

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

  6. 【BZOJ】1648&colon; &lbrack;Usaco2006 Dec&rsqb;Cow Picnic 奶牛野餐(dfs)

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

  7. POJ 1985&period;Cow Marathon-树的直径-树的直径模板&lpar;BFS、DFS&lpar;vector存图&rpar;、DFS&lpar;前向星存图&rpar;&rpar;

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

  8. &lbrack;USACO06DEC&rsqb;牛的野餐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&plus;欧拉回路)

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

随机推荐

  1. JavaScript 正则表达式语法

    定义 JavaScript定义正则表达式有两种方法. 1.RegExp构造函数 var pattern = new RegExp("[bc]at","i"); ...

  2. C&num; lock用法

    当我们使用线程的时候,效率最高的方式当然是异步,即各个线程同时运行,其间不相互依赖和等待.但当不同的线程都需要访问某个资源的时候,就需要同步机制了,也就是说当对同一个资源进行读写的时候,我们要使该资源 ...

  3. Ta-lib函数功能列表

    import tkinter as tk from tkinter import ttk import matplotlib.pyplot as plt import numpy as np impo ...

  4. Oracle Global Finanicals Technical Reference(二)

    Skip Headers Oracle Global Finanicals Oracle Global Financials Technical Reference Manual Release 11 ...

  5. C&num;中的&quest;和&quest;&quest;&comma;null和Nullable

    from : https://www.cnblogs.com/appleyrx520/p/7018610.html C#单问号(?)与双问号(??)   1.单问号(?) 1.1 单问号运算符可以表示 ...

  6. IO流(6)—转换流

    1.处理流之二:转换流 InputStreamReader和OutputStreamWriter 2.当作用的文件就是一个文本文件且使用字节流传输时,需要把它转换成字符流,再在外面加上缓冲流以加速传输 ...

  7. Mysql授权root用户远程登录

    默认情况下Mysql的root用户不支持远程登录,使用以下命令授权   [Charles@localhost ~]$ mysql -uroot -p123 MariaDB [(none)]> u ...

  8. js计算斐波拉切

    function feibo(a){ if(!a || a <= 0){ throw new Error("参数错误,必须大于0"); }else if(a == 1){ r ...

  9. DOM之一些小实验demo

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  10. SPOJ3713——Primitive Root

    终于有一个SPOJ题目是我自己独立做出来的,ORZ,太感动了. 题目意思是给你一个素数,问你一个数r是否满足,r,r^2,r^3,……,r^p-1,全不相同. 以前做过这种类型的题目额.是这样的. 根 ...