NYOJ ~ 92 ~ 图像有效区域(BFS+预处理)
2017-12-17 17:08
183 查看
思路:BFS。我们可以在这个图外围加一圈非0的数字,然后从这个地图左上角求一个非0的连通块即可。
很巧妙的方法。
很巧妙的方法。
#include<bits/stdc++.h> using namespace std; const int MAXN = 1500; struct node { int x,y; }NOW,NEXT; int W,H,MAP[MAXN][MAXN],dir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}}; bool vis[MAXN][MAXN]; void bfs() { memset(vis,0,sizeof(vis)); NOW.x = 0; NO da8f W.y = 0; queue<node> q; q.push(NOW); while(!q.empty()) { NOW = q.front(); q.pop(); for(int i = 0; i < 4; i++) { int X = NOW.x + dir[i][0], Y = NOW.y + dir[i][1]; if(X >= 0 && Y >= 0 && X <= H + 1 && Y <= W + 1 && vis[X][Y] == false && MAP[X][Y] != 0) { vis[X][Y] = true; MAP[X][Y] = 0; NEXT.x = X; NEXT.y = Y; q.push(NEXT); } } } } int main() { int T; scanf("%d",&T); while(T--) { memset(MAP,1,sizeof(MAP)); scanf("%d%d",&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++) { printf("%d ",MAP[i][j]); } printf("\n"); } } return 0; }
相关文章推荐
- NYOJ 92 图像有用区域(bfs+预处理)
- nyoj 92 图像有效区域面积
- nyoj 92图像的有用区域
- NYOJ92-图像的有用区域
- NYOJ92 图像有用区域 【BFS】
- 【NYOJ-92- 图像有用区域-BFS】
- 图像有用区域(nyoj 92)
- nyoj 92 图像有用区域
- nyoj 92 图像有用区域
- nyoj 92 图像有用区域 【bfs】
- NYOJ 92 图像有用区域 【bfs】
- nyoj 92 图像有用区域
- nyoj 92 图像有用区域
- NYOJ92-图像有用区域
- NYOJ 92图像的有用区域 (技巧性广搜)
- NYOJ 92 图像有用区域
- nyoj--92--图像有用区域(模拟)
- NYOJ 92 图像有用区域 (BFS)
- nyoj 92 图像有用区域(dfs)
- nyoj--92--图像有用区域(模拟)