CodeForces 6B-President‘s Office(简单的DFS)

news/2025/2/27 11:24:34

题目描述:

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 n n rows and m m 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.

输入:

The first line contains two separated by a space integer numbers n n n , m m m ( 1<=n,m<=100 1<=n,m<=100 1<=n,m<=100 ) — the length and the width of the office-room, and c c c character — the President's desk colour. The following n n n lines contain m m 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. 

输出: 

Print the only number — the amount of President's deputies. 

样例输入:(有两组)

3 4 R

G.B.

.RR.

TTT.  

3 3 Z

...

.H.

..Z

样例输出: 

2

题目大意 and 解题思路:

第一行:房间长n,宽m,总统办公桌代表的字符。

接下来2 ~ n+1行,每行输入m列:房间内的办公桌的颜色(大写字母)。"."为空单元格。

求出在总统桌四周相邻的办公桌数量。

对于样例1:总统桌子为R,而周围与其相连的有上方的B,和下方的三个T,所以数量为2,即样例1的输出为2。

这道题其实就是DFS,首先找到总统桌子对应字符在地图中的位置,然后四个方向遍历搜索,对于与其相连的桌子,用一个标记数组进行标记,在这里,相同的字母只算一个数量(例如:样例中的三个T其实算一个),所以只需要定义一个一维标记数组,将每种桌子的字符转换成对应的ASCII码进行标记就可以了,最后得到多少种不同的标记,结果就是多少!

AC Code: 

#include<bits/stdc++.h>
using namespace std;
const int N=101;
char s[N][N];//存入地图的数组 
char p;//总统的桌子 
int n,m,ans;
int vis[N];//标记其他的桌子,因为桌子均为26个英文字母,相同的字母算同一类,这里定义为一维数组就可以了 
int dx[]={0,0,1,-1};//这两个是方向数组 
int dy[]={1,-1,0,0};
void dfs(int x,int y) {
	for(int k=0;k<4;k++) {//四个方向进行搜索 
		int tx=x+dx[k];
		int ty=y+dy[k];
		if(tx<0||tx>=n||ty<0||ty>=m)//越界判断 
			continue;
		if(s[tx][ty]!='.') {//如果不是单元格,即为桌子 
			if(s[tx][ty]!=p) {//如果不是总统桌子 
				vis[s[tx][ty]-'A'+1]=1;//对此桌子进行标记,下标为字母对应的ASCII码 
				s[tx][ty]='.';//访问过此桌子,将其修改为单元格 
			}else {//如果是总统桌子 
				s[tx][ty]='.';//将其修改为单元格 
				dfs(tx,ty);//从总统桌子的某个点继续搜索 
			}
		}
	}
}
int main() {
	memset(vis,0,sizeof(vis));//标记数组清零 
	scanf("%d %d %c",&n,&m,&p);
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			scanf(" %c",&s[i][j]);//%前面的空格确保完整输入 
	for(int i=0;i<n;i++) {
		for(int j=0;j<m;j++) {
			if(s[i][j]==p) {//从总统桌子的这个点开始搜索 
				dfs(i,j);
			}
		}
	}
	for(int i=1;i<=26;i++)
		if(vis[i])//同一个字母形成一类桌子,数量加1 
			ans++;
	printf("%d\n",ans);
	return 0;
}


http://www.niftyadmin.cn/n/712813.html

相关文章

job是什么 oracle12c_安装完oracle12c,oraclejobschedulerorcl服务打不开, 请问怎么查看它是否禁用,怎么把它打开...

展开全部racle在处理一般事务32313133353236313431303231363533e4b893e5b19e31333366303665时并不需要全部启动其后台的所有服务由于oracle服务所占用系统资源比较大&#xff0c;一般情况下启动监听服务oraclesidtnslistener和数据库服务oracleservicesid就可以满足数据处理的大…

java web开发(十一)过滤器

为什么80%的码农都做不了架构师&#xff1f;>>> 一 生命周期 方法声明功能描述init(FilterConfig filterConfig)用于初始化过滤器&#xff0c;整个生命周期中只被调用一次doFilter(ServletRequest req, ServletResponse resp)拦截处理方法destroy()卸载Filter之前调…

《机器学习实战》读书笔记5:朴素贝叶斯分类器的原理

贝叶斯定理 我们知道&#xff1a;P(A∩B)P(A|B)P(B)P(B|A)P(A)所以有&#xff1a;P(A|B)P(B|A)P(A)P(B)这就是贝叶斯定理。贝叶斯分类器的原理 假如我们要为一个疾病诊断系统构建一个贝叶斯分类器。首先&#xff0c;我们有如下训练集&#xff1a; 职业症状疾病矿工咳嗽肺炎矿…

个人版整理APP测试流程

2016.1.5 我的笔记 一 、APP测试基本流程 1.1 测试周期 测试周期可按项目的开发周期来确定测试时间&#xff0c;一般测试时间为两三周&#xff08;即15个工作日&#xff09;&#xff0c;根据项目情况以及版本质量可适当缩短或延长测试时间。正式测试前先向主管确认项目排期。 1…

ipsec协议_IPSec在防火墙USG5500上的运用

IPSec(Internet Protocol Security)是一整套的解决方案&#xff0c;一个协议包。IPsec主要由以下协议组成&#xff1a;一、认证头(AH)&#xff0c;为IP数据报提供无连接数据完整性、消息认证以及防重放攻击保护&#xff1b;二、封装安全载荷(ESP)&#xff0c;提供机密性、数据源…

荔枝集团战队斩获 2023 Amazon DeepRacer自动驾驶赛车企业总决赛冠军

6月27日&#xff0c;2023 Amazon DeepRacer自动驾驶赛车企业总决赛在上海决出了最终结果&#xff0c;荔枝集团“状元红”战队与Cisco、德勤管理咨询、北京辛诺创新、神州泰岳、敦煌网等12支队伍的竞逐中&#xff0c;在两轮比赛中成绩遥遥领先&#xff0c;最终斩获桂冠。而今年年…

微服务配置中心是干啥的_微服务之配置中心ConfigKeeper

在微服务架构中&#xff0c;配置中心是必不可少的基础服务。ConfigKeeper已开源&#xff0c;本文将深度分析配置中心的核心内容&#xff0c;错过「Spring Cloud中国社区北京沙龙-2018.10.28 」的同学将从本篇文章中收获现场的分享内容。背景微服务容器架构后&#xff0c;为了方…

Myeclipse10.0版下载

链接&#xff1a;http://pan.baidu.com/s/1kVl1kSf 密码&#xff1a;p6yr 主界面框图 转载于:https://www.cnblogs.com/veis/p/6938457.html