Codeforces #6B. President's Office
2016-03-29 12:52
375 查看
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 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
output
input
output
给出一个n*m的矩阵 ,描述桌子的布局。总统的桌子和他的副手的桌子相邻,每一个人的桌子有它独有的颜色。问总统有多少个副手。
搜出总统的桌子在矩阵中的边界后判断边界外的其它颜色桌子的数量。
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 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 R G.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; }
相关文章推荐
- 经典算法——求绝对值溢出问题
- CreateCompatibleDC 和 CreateCompatibleBitmap
- iOS开发者程序许可协议之简单介绍
- java接口可以实例化吗?
- 拖延症
- HDU--1166敌兵布阵(线段树)
- HDC,CDC,CClientDC的区别和联系是什么?
- 比较两个mysql数据库表结构的差异
- 索引的原理
- ceph 存储系统
- Nginx+Tomcat+Memcached 集群
- 自定义UICollectionViewFlowLayout
- ios:画直线和虚线
- Intellij IDEA 的使用
- 百度地图开发(一)——地图定位
- 网上出现的js小数计算的除法优化
- css3
- ubuntu下搭建erlang编程环境
- swift-UITabBarController 设置选中和未选中状态文字图片颜色或使用原图
- 机器学习基础(三十) —— 线性回归、正则化(regularized)线性回归、局部加权线性回归(LWLR)