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. dp重拾-完全背包--HDU 4508

    减肥记 湫湫给了你每日食物清单,上面描述了当天她想吃的每种食物能带给她的幸福程度,以及会增加的卡路里量. Input 输入包含多组测试用例. 每组数据以一个整数n开始,表示每天的食物清单有n种食物. ...

  2. Python自动化之多进程

    多进程multiprocessing from multiprocessing import Process import os def info(title): print(title) print ...

  3. bufferedimage 转换成 inputstream并保存文件

    BufferedImage img = removeBackgroud(file);//去除重影 //bufferedimage 转换成 inputstream ByteArrayOutputStre ...

  4. 2.2---找链表倒数第K个结点

    答案,注意,一种是递归,另一种是迭代,那么巧妙利用双指针: 迭代: public static LinkedListNode nthToLast(LinkedListNode head, int n) ...

  5. ndk学习16: unix domain socket

    一.UNIX Domain Socket 概念: UNIX Domain Socket是在socket架构上发展起来的用于同一台主机的进程间通讯(IPC) 特点: 1. 它不需要经过网络协议栈,不需要 ...

  6. sone2(未完成)

    先留个半完成代码 边看论文边看题解写的...难受.. #include<cstdio> #include<cstring> namespace utils{ inline in ...

  7. 一个简单的log

    #pragma once #include <windows.h> #include <process.h> class CLogger { public: static CR ...

  8. python脚本-开机自动联网

    办公室的电脑开机之后,需要打开浏览器登录校园用户认证网页,输入用户名密码登录认证之后才能联网,每次都这样搞很麻烦,写了个Python脚本自动完成这个工作. onStartup.py #file:onS ...

  9. Oracle中创建MD5方法

    create or replace function MD5(passwd in varchar2) return varchar2 is retval ); begin retval := utl_ ...

  10. Oracle语句优化之一

    public List<TdDepartment> createZtreeDep(String compId) { List<TdDepartment> dd = new Ar ...