NYOJ 92 图像有用区域 bfs
来源:互联网 发布:淄博最专业的seo公司 编辑:程序博客网 时间:2024/03/28 19:24
一开始思路异常清晰有木有(〃'▽'〃)结果就是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[n][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[n][i]==1)printf("0 ");else printf("%d ",Map[n][i]);}printf("\n");}}return 0;}
阅读全文
1 0
- NYOJ-92 图像有用区域 BFS
- nyoj 92 图像有用区域 【bfs】
- NYOJ 92--图像有用区域【BFS && 水题】
- NYOJ 92 图像有用区域 【bfs】
- nyoj--92 图像有用区域(bfs)
- NYOJ 92 图像有用区域 (BFS)
- nyoj 92 图像有用区域 bfs
- NYOJ-92 图像有用区域(bfs)
- 【NYOJ-92- 图像有用区域-BFS】
- NYOJ 92 图像有用区域 bfs
- NYOJ 92 图像有用区域(BFS)
- nyoj 92 图像有用区域 【BFS】 【好题 】
- NYOJ 92 图像有用区域 (经典的 bfs)
- nyoj 图像有用区域(BFS)
- nyoj ACM:图像有用区域 (bfs)
- NYOJ 92 图像有用区域
- NYOJ 92 图像有用区域
- nyoj 92 图像有用区域
- 关于sublime中文问题
- DPDK学习笔记1--基础概念篇(UIO,大页内存,CPU亲和性,NUMA机制等)
- linux 命令中grep 命令得个人总结(一)
- 【Hibernate】Session清空缓存与清理缓存
- POJ 2387(最短路dijkstra)
- NYOJ 92 图像有用区域 bfs
- 练习二(第二周)
- python virtualenv
- Linux命令
- JS中自定义实现trim()方法
- [BZOJ]4240: 有趣的家庭菜园 贪心+树状数组
- 练习一 温度转换
- Windows环境下(64bit,内存4G),安装虚拟机(VM12.5),Ubuntu(17.04),anaconda(Python3.6)和TensorFlow(1.3)
- IDEA 创建Spring + SpringMVC + MyBatis整合maven项目