NEFU要崛起——第5场 B - President's Office

                                                                           B - President's Office
                                 Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
                                              Submit Status Practice CodeForces 6B

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

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

 

 

 

 

 

 

 

题意:找与目标字符相邻的不同字符的个数

题解:开始的时候我用dfs做的 样例过了就是么有做出来啊~ 我去 后来一哥们用这种变态方法直接秒了所有人~

代码:

#include <iostream>
using namespace std;
int n,m,b[256];
char map[150][150];
int main()
{
	char traget;
	int ans=0;
    cin>>n>>m>>traget;
    for(int i=1;i<=n;i++) 
		for(int j=1;j<=m;j++) 
			   cin>>map[i][j];
			   
    for(int i=1;i<=n;i++)  
	{
		for(int j=1;j<=m;j++)
		{
        	if(map[i][j]==traget)
            	b[map[i-1][j]]=b[map[i+1][j]]=b[map[i][j-1]]=b[map[i][j+1]]=1;
		}
	}
    for(int i='A';i<='Z';i++) 
			ans+=b[i];
    cout<<ans-b[traget]<<endl;
    
    return 0;
}


一个叫Pira的代码:

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int mm=111;
char map[mm][mm],ps;
int p[333];
int i,j,n,m,ans;
int main()
{
    while(~scanf("%d%d %c",&n,&m,&ps))
    {
        for(i=0;i<n;++i)
            scanf("%s",map[i]);
        memset(p,0,sizeof(p));
        p[ps]=1;
        for(i=0;i<n;++i)
            for(j=0;j<m;++j)
            {
                if(map[i][j]==ps)
                {
                    if(j)p[map[i][j-1]]=1;
                    if(i)p[map[i-1][j]]=1;
                }
                if(i&&map[i-1][j]==ps)p[map[i][j]]=1;
                if(j&&map[i][j-1]==ps)p[map[i][j]]=1;
            }
        ans=-1;
        for(i='A';i<='Z';++i)ans+=p[i];
        printf("%d\n",ans);
    }
    return 0;
}


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值