题目链接:添加链接描述
思路
先从炮弹点bfs搜一遍,如果相同气泡数量小于3 直接输出 0; 如果大于那么就还需要判断与第一行不相连接的气泡有多少个 (这里是不相互连接,没有说种类,所以只看连不连接就行,在这可把我wa蒙圈了) 最后,第一次搜的和这些不相连接的加起来就是答案; (还有就是搜索的时候是6个方向,因为奇偶行气泡的数目是不一样的) 奇数:上,下,左,右,左上,左下 偶数:上,下,左,右,右上,右下
代码:
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std
;
const int d
[2][6][2]={{1,0,0,1,-1,0,0,-1,1,1,-1,1},{1,0,0,1,-1,0,0,-1,1,-1,-1,-1}};
char mp
[110][110];
int len
[110];
bool vis
[110][110];
int n
,m
,cnt
;
struct node
{
int x
;
int y
;
}tmp
;
void bfs(int x
,int y
,char ch
)
{
tmp
.x
=x
;
tmp
.y
=y
;
vis
[x
][y
]=1;
queue
<node
>q
;
q
.push(tmp
);
while(q
.size())
{
node cur
=q
.front();
q
.pop();
for(int i
=0;i
<6;i
++)
{
int xx
=cur
.x
+d
[cur
.x
%2][i
][0];
int yy
=cur
.y
+d
[cur
.x
%2][i
][1];
if(xx
<=0||yy
<=0||xx
>n
||yy
>len
[xx
]||vis
[xx
][yy
]) continue;
if(mp
[xx
][yy
]==ch
)
{
vis
[xx
][yy
]=1;
tmp
.x
=xx
;
tmp
.y
=yy
;
q
.push(tmp
);
cnt
++;
}
}
}
}
void bfs2(int x
,int y
)
{
tmp
.x
=x
;
tmp
.y
=y
;
vis
[x
][y
]=1;
queue
<node
>q
;
q
.push(tmp
);
while(q
.size())
{
node cur
=q
.front();
q
.pop();
for(int i
=0;i
<6;i
++)
{
int xx
=cur
.x
+d
[cur
.x
%2][i
][0];
int yy
=cur
.y
+d
[cur
.x
%2][i
][1];
if(xx
<=0||yy
<=0||xx
>n
||yy
>len
[xx
]||vis
[xx
][yy
]||mp
[xx
][yy
]=='E') continue;
vis
[xx
][yy
]=1;
tmp
.x
=xx
;
tmp
.y
=yy
;
q
.push(tmp
);
}
}
}
int main()
{
int h
,w
;
while(~scanf("%d%d%d%d",&n
,&m
,&h
,&w
))
{
int ans
=0;cnt
=1;
memset(vis
,0,sizeof(vis
));
for(int i
=1;i
<=n
;i
++)
{
scanf("%s",mp
[i
]+1);
if(i
&1) len
[i
]=m
;
else len
[i
]=m
-1;
}
bfs(h
,w
,mp
[h
][w
]);
if(cnt
<3)
{
printf("%d\n",ans
);
continue;
}
ans
+=cnt
;
for(int i
=1;i
<=m
;i
++)
{
if(mp
[1][i
]!='E'&&!vis
[1][i
])
{
bfs2(1,i
);
}
}
for(int i
=1;i
<=n
;i
++)
{
for(int j
=1;j
<=len
[i
];j
++)
{
if(mp
[i
][j
]!='E'&&!vis
[i
][j
]) ans
++;
}
}
printf("%d\n",ans
);
}
return 0;
}
转载请注明原文地址:https://blackberry.8miu.com/read-43237.html