• President's Office


    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 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.

    Output

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

    Example

    Input
    3 4 R
    G.B.
    .RR.
    TTT.
    Output
    2
    Input
    3 3 Z
    ...
    .H.
    ..Z
    Output
    0

    题意:找给出颜色周围有多少种不同颜色

     1 #include <iostream>
     2 #include <stdio.h>
     3 using namespace std;
     4 int n, m;
     5 char p;
     6 char Map[120][120];
     7 int col[100];
     8 int ans = 0;
     9 int dir[4][2] = {0,1,1,0,-1,0,0,-1};
    10 
    11 void dfs(int x, int y){
    12     for(int i = 0; i < 4; i ++){
    13         int xx = x + dir[i][0];
    14         int yy = y + dir[i][1];
    15         if(xx >= 0 && xx < n && yy >= 0 && yy < m){
    16             if(col[Map[xx][yy]] == 0 && Map[xx][yy] != '.'){
    17                 ans++;
    18                 col[Map[xx][yy]] = 1;
    19             }
    20         }
    21     }
    22 }
    23 
    24 
    25 int main(){
    26     cin >> n >> m;
    27     getchar();
    28     p = getchar();
    29     col[p] = 1;
    30     for(int i = 0; i < n; i++){
    31         for(int j = 0; j < m; j++){
    32             cin >> Map[i][j];
    33         }
    34     }
    35     for(int i = 0; i < n; i++){
    36         for(int j = 0;j < m; j++){
    37             if(Map[i][j] == p){
    38                 dfs(i,j);
    39             }
    40         }
    41     }
    42     cout << ans << endl;
    43 }
  • 相关阅读:
    MySQL的语句执行顺序
    mysql 基本使用
    spring 事务详解
    java 设计模式
    hibernate 简单查询
    qwq(一些有趣的数学题)
    关于libra9z
    HDU6756 Finding a MEX
    CF1386C Joker
    CF1340F Nastya and CBS
  • 原文地址:https://www.cnblogs.com/jxust-jiege666/p/6723895.html
Copyright © 2020-2023  润新知