NOJ [1301] Gopher Hole
来源:互联网 发布:程序员新人第一天 编辑:程序博客网 时间:2024/06/02 18:50
一开始我没想到,如果原来有两个洞无法合并,加入第三个洞如果可以合并,那么三个洞就会变1个洞,所以原来写的WA了,后来参照了下题解的方法,才AC了,果然太弱了啊
- Death-Moon loves telling stories.
Some days ago, he told us a funny story.Long long ago, there is a hamster who is so naughty. Now, he comes to a place likes a N * N square. so, he is so excited to drill holes underground.
- 输入
- Input until EOF.
Fisrt input two integers N (3 <= N < 100) and M (0 < M <2000). N is the side of N * N ground, M is the number of operations.
Then follow M lines.
Each line is a command:
Out x y : Hamster will get out at (x, y) from underground. So, place (x, y) will be a hole.
P : Calculate out the number of the holes and output (Two holes are connected when they share an edge).
If two holes are connected, it means it is one hole. - 输出
- For each 'P' command, output the number of the holes. Maybe hamster will get out at the same place more than once.
#include<stdio.h>#include<string.h>#define maxn 10010bool is_break[maxn];int father[maxn];int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};int is_not=0;int find(int x){ if(x==father[x]) return father[x]; if(x!=father[x]) father[x]=find(father[x]); return father[x];}void Union(int x,int y){ int xx=find(x); int yy=find(y); if(xx!=yy) { father[yy]=xx; is_not--; }}int main(){ int n,m,i,j; while(~scanf("%d%d",&n,&m)) { for(i=0;i<=n*n+10;i++) father[i]=i; memset(is_break,false,sizeof(is_break)); char com[4]; int x,y,xx,yy,ans=0,dit; while(m--) { scanf("%s",com); if(com[0]=='O') { scanf("%d%d",&x,&y); dit=x*n+y; if(is_break[dit]) continue; is_break[dit]=1; for(j=0;j<4;j++) { xx=x+dir[j][0]; yy=y+dir[j][1]; if(xx>=0 && yy>=0 && xx<n && yy<n) { if(is_break[xx*n+yy]==0) continue; else break; } } if(j==4) { ans++; continue; } is_not=1; for(j=0;j<4;j++) { xx=x+dir[j][0]; yy=y+dir[j][1]; if(xx<0 || yy<0 || xx>=n || yy>=n || is_break[xx*n+yy]==0) continue; else Union(dit,xx*n+yy); } ans+=is_not; } else if(com[0]=='P') printf("%d\n",ans ); } } return 0;}
0 0
- NOJ [1301] Gopher Hole
- NOJ [1301] Gopher Hole
- NOJ 1301 Gopher Hole 并查集好题 n*n矩阵上打洞 不相连洞的个数
- NOJ [1187] Hole Breaker
- NOJ [1320] Hole of Black
- Hole
- Noj
- Gopher II
- gopher meetup
- Another hole...
- Watering Hole
- 2536 Gopher II
- Dog and Gopher
- pku2536 Gopher II
- Gopher 应用实现步骤
- POJ-2536 Gopher II
- poj 2536 Gopher II
- poj_2536 Gopher II
- hdu 2896 病毒侵袭
- Struts2 StackContext ValueStack ActionContext
- android程序混淆
- Mina源码阅读笔记(一)-整体解读
- UITableView自定义Cell
- NOJ [1301] Gopher Hole
- WaitForSingleObject
- pca
- 开发TI DSP需要知道的
- 修改android的system.img文件方法
- Mina源码阅读笔记(三)-Mina的连接IoAccpetor
- 对rspec的“才知道”
- 情感金钱
- 广州有资源、有人才,为何缺少移动互联网巨头?