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.
ExamplesinputCopy
3 4 R
G.B.
.RR.
TTT.
outputCopy
2
inputCopy
3 3 Z
…
.H.
…Z
outputCopy
0
题意:
寻找总统的议员数,总统的议员只会出现在总统桌子旁边。
这道题我原本想的是,在输入的时候找到总统桌子然后算出总统周围的坐标(上下左右),结果嘛,在第四个例子那里始终过不去
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
set<char>s;
char a[105][105];
int main()
{
int n,m;char c;
cin>>n>>m>>c;
int r=0,l=200,u=200,d=0;
for(int i=1;i<=n;i++)
{
for(int t=1;t<=m;t++)
{
cin>>a[i][t];
if(a[i][t]==c)
{
l=min(t,l);
r=max(t,r);
u=min(i,u);
d=max(i,d);
}
}
}
char q='.';
s.insert(q);s.insert(c);
int shang=max(u-1,1),xia=min(d+1,n),zuo=max(l-1,1),you=min(r+1,m);
for(int i=shang;i<=xia;i++)
{
for(int t=zuo;t<=you;t++)
{
if(i==shang&&t==zuo)continue;
if(i==xia&&t==zuo)continue;
if(i==shang&&t==you)continue;
if(i==xia&&t==you)continue;
s.insert(a[i][t]);cout<<a[i][t]<<" ";
}
}
cout<<s.size()-2<<endl;
return 0;
}
上面是错误代码!!!!
卡了一会儿,我自己随便弄了一个例子
2 3 r
r r d
l t e
这个结果应该是3对吧 而上面的代码输出确实 1,OK 原因找到了仍然是边界上的问题(一直在考虑这个东西结果还是死在这里了)
所以换了一下思路,还是进行暴力的去找总统桌子的位置,然后在那一个的上下左右去找(原本思路是由整体来,现在是一个一个来),然后ac了。。。ac了。。。(一种植物)
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
set<char>s;
char a[105][105];
int main()
{
int n,m;char c;
cin>>n>>m>>c;
int r=1,l=101,u=101,d=1;
for(int i=1;i<=n;i++)
{
for(int t=1;t<=m;t++)
{
cin>>a[i][t];
}
}
char q='.';
s.insert(q);s.insert(c);
for(int i=1;i<=n;i++)
{
for(int t=1;t<=m;t++)
{
if(a[i][t]==c)
{
if(t+1<=m)s.insert(a[i][t+1]);
if(i+1<=n)s.insert(a[i+1][t]);
if(t-1>=1)s.insert(a[i][t-1]);
if(i-1>=1)s.insert(a[i-1][t]);
}
}
}
cout<<s.size()-2<<endl;
return 0;
}
总结不能死在一颗树上