关注
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<vector>
#include<queue>
using namespace std;
typedef long long LL;
struct node{
int c;
int tn;
int tm;
int num;
node(int a,int b,int dd,int d)
{
c = a;
tn = b;
tm = dd;
num = d;
}
node()
{
}
};
struct node2{
int tn;
int tm;
int c;
node2(int a,int b,int d)
{
tn = a;
tm = b;
c = d;
}
node2()
{
}
};
bool cmp(node a,node b)
{
if(a.num!=b.num)
{
return a.num>b.num;
}
else if(a.c!=b.c)
{
return a.c<b.c;
}
else if(a.tn!=b.tn)
{
return a.tn<b.tn;
}
else
{
return a.tm<b.tm;
}
}
int t,n,m,dx[]={0,1,-1,0},dy[]={1,0,0,-1};
string s[100];
int main()
{
cin>>t;
while(t--)
{
cin>>n>>m;
for(int i=0;i<=n;i++)
for(int j=0;j<=m;j++)
s[i][j]='#';
for(int i=0;i<n;i++)
cin>>s[i];
while(1)
{
vector<node>arr;
arr.clear();
int book[100][100] = {0};
for(int i=0;i<=50;i++)
for(int j=0;j<=50;j++)
book[i][j]=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(!book[i][j]&&s[i][j]!='#')
{
int tc = 1;
char c = s[i][j];
book[i][j] = 1;
queue<node2>q;
q.push(node2(i,j,s[i][j]));
while(!q.empty())
{
int sz = q.size();
for(int i=1;i<=sz;i++)
{
node2 temp = q.front();
q.pop();
int x = temp.tn;
int y = temp.tm;
for(int k = 0;k<4;k++)
{
int tempx = x+dx[k];
int tempy = y+dy[k];
if(tempx>=0&&tempx<n&&tempy>=0&&tempy<m&&!book[tempx][tempy]&&s[tempx][tempy]==c)
{
book[tempx][tempy] = 1;
tc++;
q.push(node2(tempx,tempy,c));
}
}
}
}
if(tc>1)
{
arr.push_back(node(s[i][j],i,j,tc));
}
}
}
if(arr.size()==0)
break;
else
{
int ss;
sort(arr.begin(),arr.end(),cmp);
int xx = arr[0].tn;
int yy = arr[0].tm;
int cc = arr[0].c;
queue<node2>q;
q.push(node2(xx,yy,cc));
while(!q.empty())
{
int sz = q.size();
for(int i=1;i<=sz;i++)
{
node2 temp = q.front();
q.pop();
int x = temp.tn;
int y = temp.tm;
int c = temp.c;
for(int k = 0;k<4;k++)
{
int tempx = x+dx[k];
int tempy = y+dy[k];
if(tempx>=0&&tempx<n&&tempy>=0&&tempy<m&&s[tempx][tempy]==cc)
{
s[tempx][tempy]='#';
q.push(node2(tempx,tempy,c));
}
}
}
}
for(int i=0;i<m;i++)
{
int k = 0;
for(int j=n-1;j>=0;j--)
{
if(s[j][i]=='#')
{
k++;
}
else
s[j+k][i]=s[j][i];
if(k!=0)
s[j][i]='#';
}
}
for(int i=0;i<m;i++)
{
int k=0;
for(int j=0;j<n;j++)
if(s[j][i]=='#')
k++;
if(k==n)
{
for(int j=0;j<n;j++)
s[j][i]=s[j][i+1],s[j][i+1]='#';
}
}
}
}
int shu = 0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(s[i][j]=='#')
shu++;
}
cout<<n*m-shu<<endl;
}
return 0;
} 第三题
查看原帖
点赞 1
相关推荐
02-10 10:11
门头沟学院 前端工程师
钱嘛数字而已: 点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 牛客新年AI问运 #
6242次浏览 110人参与
# 工作中的卑微时刻 #
33391次浏览 199人参与
# 牛客AI体验站 #
16200次浏览 286人参与
# 多益网络工作体验 #
63119次浏览 306人参与
# 有必要和同事成为好朋友吗? #
952次浏览 20人参与
# 正在实习的碎碎念 #
1644773次浏览 13716人参与
# 面试中的破防瞬间 #
1190077次浏览 11026人参与
# 工作一周年分享 #
52295次浏览 274人参与
# 滴!实习打卡 #
786446次浏览 6841人参与
# 秋招吐槽大会 #
304219次浏览 1523人参与
# 机械人的薪资开到多少,才适合去? #
164997次浏览 571人参与
# 你最满意的offer薪资是哪家公司? #
71361次浏览 353人参与
# 大学最后一个寒假,我想…… #
89247次浏览 809人参与
# 你怎么看待AI面试 #
145958次浏览 786人参与
# 哪些公司真双非友好? #
62856次浏览 268人参与
# OC/开奖 #
411213次浏览 2282人参与
# 为了实习逃课值吗? #
65725次浏览 526人参与
# 如果可以选,你最想从事什么工作 #
721859次浏览 4870人参与
# 重来一次,你会对开始求职的自己说 #
32781次浏览 388人参与
# 如何提高实习转正率? #
86485次浏览 504人参与