类似flood fill——President‘s Office

President’s Office

题面翻译

输入格式:

第一行:

房间长n,宽m,总统办公桌代表的字符串

接下来2 ~ n+1行,每行输入m列:

房间内的办公桌的颜色(大写字母)。"."为空单元格。
输出格式:

一行一个变量:

求出在总统桌四周相邻的办公桌数量
样例1 解释:

3 4 R

G . B .

. R R .

T T T .

此办公室的总统桌在二排的第二,三列(连起来的并且颜色相同算一个办公桌,此样例总统桌面积为1*2)

那么四周则有办公桌:

TTT(TTT面积为1*3)

B

因此输出2
Translated by @s5_gan

题目描述

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.

输入格式

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 $ c $ character — 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.

输出格式

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

样例 #1

样例输入 #1

3 4 R
G.B.
.RR.
TTT.

样例输出 #1

2

样例 #2

样例输入 #2

3 3 Z
...
.H.
..Z

样例输出 #2

0

思路

本道题的意思就是给定我们字符,表示总统的桌子,题目要求的是总统桌子四周的桌子的种类个数。因此我们可以用dfs(其实本质上就是类似flood fill模型来做),我们可以对不同类型的桌子进行标记。

拓展:小技巧,对于遍历过的桌子,因为后序就没有必要再用那个桌子,因此我们可以直接将它修改成点。

代码

//flood fill模型

#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>

using namespace std;

const int N = 110;

char str[N][N];
bool st[N];
int n,m;
char s;
int ans;
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};

void dfs(int x,int y){
    
    for(int i=0;i<4;i++){
        int a=dx[i]+x,b=dy[i]+y;
        if(a<1||b<1||a>n||b>m)continue;
        if(str[a][b]=='.')continue;
        if(str[a][b]!=s){
            st[str[a][b]-'A']=true;
            str[a][b]='.';
        }else{
            str[a][b]='.';
            dfs(a,b);
        }
    }
}

int main(){
    cin>>n>>m>>s;
    
    int stx=1,sty=1;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>str[i][j];
            if(str[i][j]==s){
                stx=i,sty=j;
            }
        }
    }
    
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(str[i][j]==s){
                dfs(i,j);
                break;
            }
        }
    }
    
    int ans=0;
    
    for(int i=0;i<N;i++){
        if(st[i])ans++;
    }
    
    cout<<ans;
    
    return 0;
    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

green qwq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值