Codeforces 6B. President's Office
2014-12-07 15:33
1971 查看
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.
Sample test(s)
input
output
input
output
代码:
#include <stdio.h>
#include <set>
#include <vector>
#include <iostream>
using namespace std;
const int MAXN = 105;
char matrix[MAXN][MAXN];
bool Hash[256];
void Find(int x, int y)
{
if(isupper(matrix[x][y - 1]))
Hash[matrix[x][y - 1]] = true;
if(isupper(matrix[x][y + 1]))
Hash[matrix[x][y + 1]] = true;
if(isupper(matrix[x - 1][y]))
Hash[matrix[x - 1][y]] = true;
if(isupper(matrix[x + 1][y]))
Hash[matrix[x + 1][y]] = true;
}
int main()
{
#ifdef _LOCAL
freopen("F://input.txt", "r", stdin);
#endif
int n, m, posX, posY;
char ch;
scanf("%d%d %c", &n, &m, &ch);
vector<pair<int, int>> president;
Hash[ch] = true;
for(int i = 1; i <= n; ++i)
{
getchar();
for(int j = 1; j <= m; ++j)
{
scanf("%c", &matrix[i][j]);
if(matrix[i][j] == ch)
president.push_back(make_pair(i, j));
}
}
for(int i = 0; i < president.size(); ++i)
Find(president[i].first, president[i].second);
int count = 0;
for(int i = 'A'; i <= 'Z'; ++i)
if(Hash[i]) ++count;
printf("%d\n", count - 1);
return 0;
}
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.
Sample test(s)
input
3 4 R G.B. .RR. TTT.
output
2
input
3 3 Z ... .H. ..Z
output
0
代码:
#include <stdio.h>
#include <set>
#include <vector>
#include <iostream>
using namespace std;
const int MAXN = 105;
char matrix[MAXN][MAXN];
bool Hash[256];
void Find(int x, int y)
{
if(isupper(matrix[x][y - 1]))
Hash[matrix[x][y - 1]] = true;
if(isupper(matrix[x][y + 1]))
Hash[matrix[x][y + 1]] = true;
if(isupper(matrix[x - 1][y]))
Hash[matrix[x - 1][y]] = true;
if(isupper(matrix[x + 1][y]))
Hash[matrix[x + 1][y]] = true;
}
int main()
{
#ifdef _LOCAL
freopen("F://input.txt", "r", stdin);
#endif
int n, m, posX, posY;
char ch;
scanf("%d%d %c", &n, &m, &ch);
vector<pair<int, int>> president;
Hash[ch] = true;
for(int i = 1; i <= n; ++i)
{
getchar();
for(int j = 1; j <= m; ++j)
{
scanf("%c", &matrix[i][j]);
if(matrix[i][j] == ch)
president.push_back(make_pair(i, j));
}
}
for(int i = 0; i < president.size(); ++i)
Find(president[i].first, president[i].second);
int count = 0;
for(int i = 'A'; i <= 'Z'; ++i)
if(Hash[i]) ++count;
printf("%d\n", count - 1);
return 0;
}
相关文章推荐
- CodeForces 6B - President's Office
- CodeForces 6B President's Office
- codeforces 6B President's Office
- CodeForces 6B President's Office
- codeforces 6B. President's Office
- CodeForces 490B Queue
- Codeforces 621E Wet Shark and Blocks 【矩阵加速dp】
- Codeforces 103C Russian Roulette
- Hongcow Builds A Nation ___codeforces 744A
- Codeforces 834D The Bakery(dp+线段树)
- CodeForces - 580B Kefa and Company(尺取法)
- CodeForces 160B Unlucky Ticket
- Codeforces 621C Wet Shark and Flowers
- Codeforces 421D Bug in Code(思维)
- CodeForces 738E Subordinates
- CodeForces 825B:Five-In-a-Row(模拟)
- [bzoj3625][Codeforces 250 E]The Child and Binary Tree(生成函数+多项式运算+FFT)
- CodeForces 348C Subset Sums nsqrtn的姿势。。
- Codeforces 404D Minesweeper 1D
- CodeForces 702A Maximum Increase