NYOJ 92 图像有用区域 bfs
2017-09-10 14:58
330 查看
一开始思路异常清晰有木有(〃'▽'〃)结果就是wa;
有点类似于求连通块,只是是求外面的连通块并进行标记,被标记的输出0,未被标记的输出原来的值;
一个坑在于如果是这样的:
外面的连通块被分成了几份,这样方法就失效了,我们在原来的图外加上一圈1就可以避免;
完整代码:
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
int w,h;
int Map[1445][965];
int vis[1445][965];
struct node{
int x,y;
};
int check(int x,int y){
if(x<0||x>w+1||y<0||y>h+1)return 0;
if(vis[x][y]==1||Map[x][y]==0)return 0;
return 1;
}
void dfs(){
queue<node>s;
node now,next;
now.x=0,now.y=0;
vis[0][0]=1;
s.push(now);
while(!s.empty()){
now=s.front();
s.pop();
for(int i=-1;i<=1;i++){
for(int n=-1;n<=1;n++)if((i==0&&n!=0)||(i!=0&&n==0)){
next.x=now.x+i,next.y=now.y+n;
if(check(next.x,next.y)==0)continue;
vis[next.x][next.y]=1;
s.push(next);
}
}
}
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&w,&h);
for(int i=1;i<=h;i++){
for(int n=1;n<=w;n++)scanf("%d",&Map
[i]);
}
for(int i=0;i<=h+1;i++)Map[0][i]=1,Map[w+1][i]=1;
for(int i=0;i<=w+1;i++)Map[i][0]=1,Map[i][h+1]=1;
memset(vis,0,sizeof(vis));
dfs();
for(int i=1;i<=h;i++){
for(int n=1;n<=w;n++){
if(vis
[i]==1)printf("0 ");
else printf("%d ",Map
[i]);
}
printf("\n");
}
}
return 0;
}
相关文章推荐
- NYOJ 92 图像有用区域(BFS)
- nyoj 92 图像有用区域 【BFS】 【好题 】
- NYOJ 92 图像有用区域 【bfs】
- nyoj92图像有用区域【bfs】
- NYOJ 92 图像有用区域(BFS)
- nyoj 92 图像有用区域 【bfs】
- NYOJ 92--图像有用区域【BFS && 水题】
- NYOJ 92 图像有用区域 (BFS)
- nyoj 92 图像有用区域 bfs
- NYOJ 92 图像有用区域 (经典的 bfs)
- nyoj92图像有用区域——BFS
- NYOJ-92 图像有用区域(bfs)
- nyoj--92 图像有用区域(bfs)
- 【NYOJ-92- 图像有用区域-BFS】
- NYOJ92 图像有用区域 【BFS】
- nyoj 图像有用区域(BFS)
- NYOJ 92 图像有用区域
- NYOJ92图像有用区域
- nyoj 92 图像有用区域
- nyoj 图像有用区域(BFS)