6B - President's Office
2016-06-16 21:42
239 查看
B. President's Office
time limit per test
2 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output
President of Berland has a very vast office-room, where, apart from him, work his subordinates. Each subordinate, as well as President himself, has his own desk of a unique colour. Each desk is rectangular, and its sides are parallel to the office walls. One
day President decided to establish an assembly, of which all his deputies will be members. Unfortunately, he does not remember the exact amount of his deputies, but he remembers that the desk of each his deputy is adjacent to his own desk, that is to say,
the two desks (President's and each deputy's) have a common side of a positive length.
The office-room plan can be viewed as a matrix with n rows and m columns.
Each cell of this matrix is either empty, or contains a part of a desk. An uppercase Latin letter stands for each desk colour. The «period» character («.»)
stands for an empty cell.
Input
The first line contains two separated by a space integer numbers n, m (1 ≤ n, m ≤ 100)
— the length and the width of the office-room, and ccharacter — the President's desk colour. The following n lines
contain m characters each — the office-room description. It is guaranteed that the colour of each desk is unique, and each desk represents
a continuous subrectangle of the given matrix. All colours are marked by uppercase Latin letters.
Output
Print the only number — the amount of President's deputies.
Examples
input
output
input
output
给出一个二维数组,每个字母表示一种颜色,问指定的颜色周围有几种不同的颜色,已知相同的颜色一定有公共边
直接暴力遍历就行,注意标记遇到过的颜色
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,vis[105][105],ans=0;
char map[105][105];
int dx[]={0,-1,0,1},dy[]={1,0,-1,0};
int s[105];
void dfs(int x,int y,char ch)
{
vis[x][y]=1;
if(map[x][y]!=ch)
{
if(!s[map[x][y]])
{
++ans;
}
s[map[x][y]]=1;
return;
}
for(int i=0;i<4;++i)
{
int tx=x+dx[i],ty=y+dy[i];
if(tx<0||tx>=n||ty<0||ty>=m||map[tx][ty]=='.'||s[map[tx][ty]])
{
continue;
}
if(!vis[tx][ty])
{
dfs(tx,ty,ch);
}
}
}
void slove(char x)
{
for(int i=0;i<n;++i)
{
for(int j=0;j<m;++j)
{
if(map[i][j]==x)
{
dfs(i,j,x);
return;
}
}
}
}
int main()
{
char x;int bx,by;
scanf("%d%d %c",&n,&m,&x);
for(int i=0;i<n;++i)
{
scanf("%s",map[i]);
}
slove(x);
printf("%d\n",ans);
return 0;
}
time limit per test
2 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output
President of Berland has a very vast office-room, where, apart from him, work his subordinates. Each subordinate, as well as President himself, has his own desk of a unique colour. Each desk is rectangular, and its sides are parallel to the office walls. One
day President decided to establish an assembly, of which all his deputies will be members. Unfortunately, he does not remember the exact amount of his deputies, but he remembers that the desk of each his deputy is adjacent to his own desk, that is to say,
the two desks (President's and each deputy's) have a common side of a positive length.
The office-room plan can be viewed as a matrix with n rows and m columns.
Each cell of this matrix is either empty, or contains a part of a desk. An uppercase Latin letter stands for each desk colour. The «period» character («.»)
stands for an empty cell.
Input
The first line contains two separated by a space integer numbers n, m (1 ≤ n, m ≤ 100)
— the length and the width of the office-room, and ccharacter — the President's desk colour. The following n lines
contain m characters each — the office-room description. It is guaranteed that the colour of each desk is unique, and each desk represents
a continuous subrectangle of the given matrix. All colours are marked by uppercase Latin letters.
Output
Print the only number — the amount of President's deputies.
Examples
input
3 4 R G.B. .RR. TTT.
output
2
input
3 3 Z ... .H. ..Z
output
0
给出一个二维数组,每个字母表示一种颜色,问指定的颜色周围有几种不同的颜色,已知相同的颜色一定有公共边
直接暴力遍历就行,注意标记遇到过的颜色
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,vis[105][105],ans=0;
char map[105][105];
int dx[]={0,-1,0,1},dy[]={1,0,-1,0};
int s[105];
void dfs(int x,int y,char ch)
{
vis[x][y]=1;
if(map[x][y]!=ch)
{
if(!s[map[x][y]])
{
++ans;
}
s[map[x][y]]=1;
return;
}
for(int i=0;i<4;++i)
{
int tx=x+dx[i],ty=y+dy[i];
if(tx<0||tx>=n||ty<0||ty>=m||map[tx][ty]=='.'||s[map[tx][ty]])
{
continue;
}
if(!vis[tx][ty])
{
dfs(tx,ty,ch);
}
}
}
void slove(char x)
{
for(int i=0;i<n;++i)
{
for(int j=0;j<m;++j)
{
if(map[i][j]==x)
{
dfs(i,j,x);
return;
}
}
}
}
int main()
{
char x;int bx,by;
scanf("%d%d %c",&n,&m,&x);
for(int i=0;i<n;++i)
{
scanf("%s",map[i]);
}
slove(x);
printf("%d\n",ans);
return 0;
}
相关文章推荐
- 几个南阳oj的STL题
- 常用git命令
- Powerpoint动画教程:现场记录放映感受
- Android 工作学习笔记
- 【C++】堆对象
- 练习、C# 结构体、冒泡排序
- CentOS 7 ifconfig: command not found
- 测试用例生成中启发式算法G、GRE源码
- hbase 分享笔记
- C++中String 操作(一)
- Java文字图像识别
- 摄像机模型与标定——三个坐标系及其之间关系
- Oracle 大批量操作数据
- html——样式、链接和表格
- 安装sklearn机器学习库
- 设置input获得焦点边框样式
- Python 经典书籍
- 深度优先搜索 codevs 1031 质数环
- PowerPoint 2000视图方式讲解
- hbase的计数器