您的位置:首页 > 其它

hdu 杭电 1241 Oil Deposits

2012-08-31 19:45 239 查看
题意:找图中没有连在一起的'@'的个数。

解法:广搜

ac代码:

View Code

#include<iostream>
#include<queue>
using namespace std;

const int M=100+10;
char map[M][M];//地图
bool use[M][M];//用作标记
int vec[8][2]={-1, 0,
-1,-1,
0,-1,
1,-1,
1, 0,
1, 1,
0, 1,
-1,1};//方向向量:左,左上,上,右上,右,右下,下,左下

struct que
{
int i,j;
};

int main()
{
int m,n;
int i,j,k;
int count;
int d,c;
queue<que>q;
que temp;
while(scanf("%d%d",&m,&n)!=EOF&&m)
{
getchar();
count=0;
memset(use,0,sizeof(use));
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='@')
use[i][j]=1;
}
getchar();
}

for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
if(use[i][j])
{
use[i][j]=0;
temp.i=i;temp.j=j;
q.push(temp);
while(!q.empty())
{
for(k=0;k<8;k++)
{
d=q.front().i+vec[k][0];
c=q.front().j+vec[k][1];
if((d>=1&&d<=m)&&(c>=1&&c<=n)&&use[d][c]!=0)
{
temp.i=d;temp.j=c;
q.push(temp);
use[d][c]=0;
}
}
q.pop();
}
count++;
}
}
}
printf("%d\n",count);
}
return 0;
}

峰注:不明白请留言
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: