codevs 1293 送给圣诞夜的极光
2016-02-03 12:34
232 查看
裸bfs。多加一些位置转移即可。
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
char s[105];
int map[105][105],n,m,cnt=0,bx,by;
int dx[]={0,1,0,-1,0,-1,1,1,-1,2,0,-2,0},dy[]={0,0,1,0,-1,-1,-1,1,1,0,2,0,-2};
bool vis[105][105];
struct code
{
int x,y;
};
queue <code> q;
bool judge(int x,int y)
{
if ((x>=1) && (x<=n) && (y>=1) && (y<=m) && (vis[x][y]==false) && (map[x][y]==1))
return true;
return false;
}
void bfs(int bx,int by)
{
code begin;
begin.x=bx;begin.y=by;
while (!q.empty())
q.pop();
vis[bx][by]=true;
q.push(begin);
while (!q.empty())
{
code head=q.front();
q.pop();
for (int i=1;i<=12;i++)
{
int tx=head.x+dx[i],ty=head.y+dy[i];
if (judge(tx,ty)==true)
{
code now;
now.x=tx;now.y=ty;
vis[tx][ty]=true;
q.push(now);
}
}
}
}
int main()
{
memset(vis,false,sizeof(vis));
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
{
scanf("%s",s);
for (int j=0;j<m;j++)
{
if (s[j]=='#') map[i][j+1]=1;
else map[i][j+1]=0;
}
}
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
{
if ((map[i][j]==1) && (vis[i][j]==false))
{
cnt++;
bfs(i,j);
}
}
printf("%d\n",cnt);
return 0;
}
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
char s[105];
int map[105][105],n,m,cnt=0,bx,by;
int dx[]={0,1,0,-1,0,-1,1,1,-1,2,0,-2,0},dy[]={0,0,1,0,-1,-1,-1,1,1,0,2,0,-2};
bool vis[105][105];
struct code
{
int x,y;
};
queue <code> q;
bool judge(int x,int y)
{
if ((x>=1) && (x<=n) && (y>=1) && (y<=m) && (vis[x][y]==false) && (map[x][y]==1))
return true;
return false;
}
void bfs(int bx,int by)
{
code begin;
begin.x=bx;begin.y=by;
while (!q.empty())
q.pop();
vis[bx][by]=true;
q.push(begin);
while (!q.empty())
{
code head=q.front();
q.pop();
for (int i=1;i<=12;i++)
{
int tx=head.x+dx[i],ty=head.y+dy[i];
if (judge(tx,ty)==true)
{
code now;
now.x=tx;now.y=ty;
vis[tx][ty]=true;
q.push(now);
}
}
}
}
int main()
{
memset(vis,false,sizeof(vis));
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
{
scanf("%s",s);
for (int j=0;j<m;j++)
{
if (s[j]=='#') map[i][j+1]=1;
else map[i][j+1]=0;
}
}
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
{
if ((map[i][j]==1) && (vis[i][j]==false))
{
cnt++;
bfs(i,j);
}
}
printf("%d\n",cnt);
return 0;
}
相关文章推荐
- C#知识点扫盲——GC(Garbage Collector)
- iOS开发-黑魔法 method swizzle(一)
- Android 动画 ValueAnimator(一)
- yii2.0 基于RESTful的接口的几种请求方式, 举例说明
- poj1011 dfs 剪枝
- git 提交ignore files
- 同一包下面类方法的调用
- 状态压缩DP解POJ3254-Corn Fields
- Python实现浏览器自动化操作
- 三星N900(note3)刷机包 颓废N0.8.1 修复已知BUG 集成谷歌服务
- 关于jsonp的理解
- atitit。自己定义uml MOF EMF体系eclipse emf 教程o7t
- YisouSpider你想搞死我的服务器吗?
- [LeetCode]217. Contains Duplicate
- Ninja介绍
- 测试-Unity组件的调用顺序
- Remove duplicates from sorted listed 2
- 类与类之间的关系
- socket connect的问题
- 选择安卓系统中的图片资源