您的位置:首页 > 其它

选择题(codevs 2919)

2016-06-13 19:44 232 查看

2919 选择题

时间限制: 1 s

空间限制: 16000 KB

题目等级 : 黄金 Gold

题解
查看运行结果

题目描述 Description

某同学考试,在N*M的答题卡上写了A,B,C,D四种答案。

他做完了,又不能交,一看表,离打铃还有N久。

他开始玩一个游戏:选一个格子X,Y,从这个格子出发向4个方向找相同的选项,找到的再如此。

求形成的图形的面积。(一个选项占一个单位面积)

输入描述 Input Description

N M X Y

答题卡(矩阵)

输出描述 Output Description

面积

样例输入 Sample Input

3 3 1 2

A C B

C C C

D C A

样例输出 Sample Output

5

数据范围及提示 Data Size & Hint

N,M<=15.

对于33%数据,只有A。

#include<cstdio>
#include<iostream>
#define M 16
using namespace std;
char map[M][M];
int vis[M][M],n,m,ans,flag;
int a[5]={0,0,0,1,-1};
int b[5]={0,-1,1,0,0};
char cc;
int out(int x,int y)
{
if(x>n||x<1||y>m||y<1)return 1;
if(map[x][y]!=cc||vis[x][y])return 1;
return 0;
}
void dfs(int x,int y)
{
if(out(x+1,y)&&out(x,y+1)&&out(x-1,y)&&out(x,y-1))
return;
for(int i=1;i<=4;i++)
{
int xx=x+a[i];
int yy=y+b[i];
if(!out(xx,yy))
{
ans++;
vis[xx][yy]=1;
dfs(xx,yy);
}
}
}
int main()
{
int x,y;
scanf("%d%d%d%d",&n,&m,&x,&y);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>map[i][j];
cc=map[x][y];
dfs(x,y);
printf("%d",ans);
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: