您的位置:首页 > 其它

nyoj92图像有用区域——BFS

2018-03-27 21:32 363 查看
图像有用区域

为什么不能AC…

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <queue>
#define maxn 1001
using namespace std;
int W,H;
int map[960][1440];
int Next[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
struct Node{
int x,y;
Node(){}
Node(int x,int y):x(x),y(y){}
};
int ans = 0;
void bfs(){
int nx,ny;
queue <Node>que;
que.push(Node(1,1));
map[1][1] = 0;
while(!que.empty()){
Node head = que.front();
que.pop();
for(int k=0;k<4;k++){
nx = head.x + Next[k][0];
ny = head.y + Next[k][1];
if(nx>=1 && ny>=1 && nx<=H && ny<=W){
if(map[nx][ny]!=0){
map[nx][ny] = 0;
que.push(Node(nx,ny));
}
}
}

}
}
int main(){
//    freopen("/Users/zhaohaibo/Desktop/a.txt","r",stdin);
int ncase;
cin>>ncase;
while(ncase--){
memset(map,-1,sizeof(map));
cin>>W>>H;
for(int i=1;i<=H;i++)
for(int j=1;j<=W;j++)
scanf("%d",&map[i][j]);
bfs();
for(int i=1;i<=H;i++)
for(int j=1;j<=W;j++)
if(j==W)
printf("%d\n",map[i][j]);
else
printf("%d ",map[i][j]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: