您的位置:首页 > 其它

hdoj 1547 Bubble Shooter(dfs+dfs)

2012-03-13 14:29 260 查看
【题目大意】:给出一个泡泡龙的局面,'a'~'z'分别代表颜色,‘E'代表此处为空。给定一个设计点(x,y)。问最多会消去多少个泡泡

【解题思路】:搜索题,不过要分两次,第一次搜索,判联通,也就是找出同色相连的是否有>=3个。第二步,搜连带掉落的泡泡,也就是那些在消去联通块之后可能会随之掉落的泡泡。个E和奇偶行的处理很重要。

我写搜索的能力一直很弱,处理奇偶行的不同情况烦到差点没崩溃,wa了几次。宿舍又有个舍友一直在播放一首忧伤的情歌~结果搞到我自己被感染了,还是不够淡定。

等到现在,参考了一份代码重新写过之后,才a的。不过这份代码对奇偶行的处理显然要比我原来写的要精简和聪明的多。

【代码】:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <cctype>
#include <map>
#include <iomanip>

using namespace std;

#define eps 1e-8
#define pi acos(-1.0)
#define inf 1<<30
#define pb push_back
#define lc(x) (x << 1)
#define rc(x) (x << 1 | 1)
#define lowbit(x) (x & (-x))
#define ll long long

int tx[6]={0,1,1,0,-1,-1};
int ty[6]={2,1,-1,-2,-1,1};
char ch[200][200];
int m,n,ans,ans1;
int x,y;
string s;
char tmp;

void solve_dfs(int x,int y,char c){
ch[x][y]=0;
ans++;
int nx,ny;
for (int i=0; i<6; i++){
nx=x+tx[i];
ny=y+ty[i];
if (nx<1 || nx>n || ny<1 || ny>m) continue;
if (c=='0' && ch[nx][ny]) solve_dfs(nx,ny,c);  //判掉落
if (ch[nx][ny]==c) solve_dfs(nx,ny,c);    //判联通
}
return ;
}

void init(){
ans=0;
memset(ch,0,sizeof(ch));
m=m*2-1;
for (int i=1;i<=n;i++){
cin >> s;
if (i%2) for (int j=0; j<s.size(); j++)
ch[i][j*2+1]=s[j];
else
for (int j=0; j<s.size(); j++)
ch[i][j*2+2]=s[j];
for (int j=1; j<=m; j++) if (ch[i][j]=='E') ch[i][j]=0;
}
if (x%2) y=y*2-1; else y=y*2;
tmp=ch[x][y];
return;
}

int main() {
while (~scanf("%d%d%d%d",&n,&m,&x,&y)){
init();
solve_dfs(x,y,tmp);
ans1=ans;
if (ans1<3) ans1=0;
else{
for (int i=1; i<=m; i++)
if (ch[1][i]) solve_dfs(1,i,'0');
for (int i=1; i<=n; i++)
for (int j=1; j<=m; j++)
if (ch[i][j]) ans1++;
}
printf("%d\n",ans1);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: