Codeforces #6B. President's Office (DFS)

来源:互联网 发布:网络用语jqk什么意思 编辑:程序博客网 时间:2024/06/06 01:02
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 nm (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.

Examples
input
3 4 RG.B..RR.TTT.
output
2
input
3 3 Z....H...Z
output
0

题目大意

给出一个n*m的矩阵 ,描述桌子的布局。总统的桌子和他的副手的桌子相邻,每一个人的桌子有它独有的颜色。问总统有多少个副手。

解题思路

搜出总统的桌子在矩阵中的边界后判断边界外的其它颜色桌子的数量。

#include <cstdio>#include <map>#include <cstring>using namespace std;int n, m;int x, y;int ans = 0;int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};map<char, bool> map1;char c;char s[110][110];bool vis[110][110] = {0};void dfs(int x, int y){    if (vis[x][y]) return ;    vis[x][y] = true;    for (int i = 0; i < 4; i++){        if (x + dx[i] >= 0 && x + dx[i] < n && y + dy[i] >= 0 && y + dy[i] < m && s[x + dx[i]][y + dy[i]] != '.'){            if (s[x + dx[i]][y + dy[i]] != c)                map1[s[x + dx[i]][y + dy[i]]] = true;            else                dfs(x + dx[i], y + dy[i]);        }    }}int main(){    memset(s, 0, sizeof(s));    scanf("%d%d%*c%c", &n, &m, &c);    for (int i = 0; i < n; i++){        getchar();        for (int j = 0; j < m; j++){            scanf("%c", &s[i][j]);            if (s[i][j] == c){                x = i;                y = j;            }        }    }    dfs(x, y);    printf("%d\n", map1.size());    return 0;}


0 0
原创粉丝点击