CF6B President's Office (#搜索 -1.13)

题意翻译

输入格式:

第一行:

房间长n,宽m,总统办公桌代表的字符串

接下来2 ~ n+1行,每行输入m列:

房间内的办公桌的颜色(大写字母)。"."为空单元格。 输出格式:

一行一个变量:

求出在总统桌四周相邻的办公桌数量 样例1 解释:

3 4 R

G . B .

. R R .

T T T .

此办公室的总统桌在二排的第二,三列(连起来的并且颜色相同算一个办公桌,此样例总统桌面积为1*2)

那么四周则有办公桌:

TTT(TTT面积为1*3)

B

因此输出2 

输入输出格式

输入格式:

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.

输出格式:

Print the only number — the amount of President's deputies.

输入输出样例

输入样例#1

3 4 R
G.B.
.RR.
TTT.

输出样例#1

2

输入样例#2

3 3 Z
...
.H.
..Z

输出样例#2

0

思路

暴力dfs,以总统桌子为中心,向四周寻找桌子。

oi真的是个玄学的东西,从1开始搜WA第15个点,改成0后就AC了。

 

#include <stdio.h>
#include <iostream>
using namespace std;
int tox[5]={1,0,-1,0};
int toy[5]={0,1,0,-1};
char a[104][104];//地图 
int n,m,s;
char p;//总统的桌子 
bool color[27];//标记其他的桌子 
inline void dfs(int x,int y)
{
	register int i,j;
	for(i=0;i<4;i++)
	{
		int x1=x+tox[i];
		int y1=y+toy[i];
		if(x1<0 || x1>=n || y1<0 ||y1>=m)//是否越界 
		{
			continue;
		}
		if(a[x1][y1]!='.')//如果是桌子 
		{
			if(a[x1][y1]!=p)//如果不是总统桌子的其他部分 
			{
				color[a[x1][y1]-'A']=1;//标记有其他桌子 
				a[x1][y1]='.';//滚吧桌子。 
			}
			else//如果是总统桌子的其他部分 
			{
				a[x1][y1]='.';//滚吧桌子。 
				dfs(x1,y1);//从那里继续搜 
			}
		}
	}
	//return;//mmp这可是dfs的模版为什么加上后就RE第3个点 
}
int main()
{
	ios::sync_with_stdio(false);
	int i,j;
	cin>>n>>m>>p;
	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		{
			cin>>a[i][j];
		}
	}
	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		{
			if(a[i][j]==p)//如果是总统的桌子 
			{
				dfs(i,j);//从那里开始搜索 
				break;//搜完就可以退出了,不然会搜一模一样的(本题中应该不会影响答案结果,但极其耗时间) 
			}
		}
	}
	for(i=0;i<26;i++)//统计在总统旁的桌子 
	{
		if(color[i])
		{
			s++; 
		}
	}
	cout<<s<<endl;
	return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值