Description
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 andm 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, andc character — the President's desk colour. The followingn 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 Input
Input
3 4
RG.B
..RR.
TTT.
Output
2
Input
3 3
Z..
..H.
..Z
Output
0
题意:给出一种颜色,问在图中与之有相邻边界的颜色的个数。每一种颜色都是唯一的,并且颜色一样的格子必相邻。
思路:先找到给出颜色的位置,然后判断其四周颜色的种类。
此题在复制的时候忘记修改字母,wa了几次,然后忘记判断一个结束的位置,依然wa,最终还是AC。
#include<iostream>
#include<stdio.h>
#include<memory.h>
using namespace std;
char str[105][105];
int vis[30];
int main()
{
int m,n;
int ta,tb,a,b;
char c;
while(scanf("%d%d %c",&m,&n,&c)!=EOF)
{
memset(vis,0,sizeof(vis));
for(int i=0; i<m; i++)
for(int j=0; j<n; j++)
{
cin>>str[i][j];
}
int flag=0;
for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
if(str[i][j]==c)
{
ta=i;
tb=j;
flag=1;
break;
}
if(flag)
break;
}
// cout<<ta<<" "<<tb<<endl;
a=ta;
b=tb;
for(int i=ta; i<m; i++)
{
if(str[i][tb]==c)
{
a=i;
}
else
break;
}
for(int j=tb; j<n; j++)
{
if(str[ta][j]==c)
{
b=j;
}
else
break;
}
//cout<<a<<" "<<b<<endl;
int sum=0;
for(int i=ta; i<=a; i++)
{
if(tb-1>=0&&str[i][tb-1]>='A'&&str[i][tb-1]<='Z')
{
int ss=str[i][tb-1]-'A';
if(!vis[ss])
{
sum++;
vis[ss]=1;
}
}
if(b+1<n&&str[i][b+1]>='A'&&str[i][b+1]<='Z')
{
int tt=str[i][b+1]-'A';
if(!vis[tt])
{
sum++;
vis[tt]=1;
}
}
}
for(int i=tb; i<=b; i++)
{
if(ta-1>=0&&str[ta-1][i]>='A'&&str[ta-1][i]<='Z')
{
int ss=str[ta-1][i]-'A';
if(!vis[ss])
{
sum++;
vis[ss]=1;
}
}
if(a+1<m&&str[a+1][i]>='A'&&str[a+1][i]<='Z')
{
int tt=str[a+1][i]-'A';
if(!vis[tt])
{
sum++;
vis[tt]=1;
}
}
}
cout<<sum<<endl;
}
return 0;
}
/*
3 4 R
G.B.
.RR.
TTT.
3 3 Z
...
.H.
..Z
*/