BFS(广搜) 细胞问题
来源:互联网 发布:ios 电量优化 编辑:程序博客网 时间:2024/05/12 20:44
SSL 2324
洛谷 P1451 求细胞数量
题目描述
一矩形阵列由数字0到9组成,数字1到9代表细胞,细胞的定义为沿细胞数字上下左右还是细胞数字则为同一细胞,求给定矩形阵列的细胞个数。如:阵列
0234500067
1034560500
2045600671
0000000089
有4个细胞。
P.S. 有人看不懂,其实就是并在一起的方阵算一个细胞。
题目分析
和找石油差不多的题目,不过用的是广搜。
首先,读入时可以将非零数标记为True。
然后用两重循环,每次找到为True的就进入BFS并增加细胞数量。
BFS在这里的作用,将整个细胞标记为False,然后在接下来的循环中就不会再找这个细胞了。
const dx:array[1..4]of longint=(1,-1,0,0); dy:array[1..4]of longint=(0,0,1,-1);var n,m,x,y:longint; a:array[1..100,1..100]of boolean; f:array[1..10000]of longint; s:array[1..10000,1..2]of longint;procedure init;var i,j:longint; c:char;begin readln(m,n); for i:=1 to m do begin for j:=1 to n do begin read(c); if c<>'0' then a[i,j]:=true else a[i,j]:=false; end; readln; end;end;function ping(x,y:longint):boolean;begin if (not (x in[1..m]))or(not(y in[1..n])) then exit(false); if not a[x,y] then exit(false); exit(true);end;procedure bfs;var h,t,i:longint;begin h:=0;t:=1; s[1,1]:=x;s[1,2]:=y; repeat inc(h); for i:=1 to 4 do if ping(s[h,1]+dx[i],s[h,2]+dy[i]) then begin inc(t); f[t]:=h; s[t,1]:=s[h,1]+dx[i]; s[t,2]:=s[h,2]+dy[i]; a[s[t,1],s[t,2]]:=false; end; until h=t;end;procedure main;var s:longint;begin s:=0; for x:=1 to m do for y:=1 to n do if a[x,y] then begin inc(s);bfs; end; writeln(s);end;begin init; main;end.
0 0
- BFS(广搜) 细胞问题
- 细胞问题(广搜练习题)
- BFS(广搜) 翻币问题
- BFS(广搜) 最小转弯问题
- 【dfs】【bfs】细胞问题
- bfs(广搜)
- 广搜BFS 迷宫问题
- 迷宫问题(广搜 bfs)
- BFS(广搜) 救援
- BFS(广搜) 倒油问题
- Dungeon Master (广搜,bfs)
- hdu1254 推箱子(广搜bfs)
- 广搜(BFS)学习笔记
- POJ3126(素数表+广搜BFS)
- lightoj1046 Rider(广搜BFS)
- BFS(广搜) 骑士旅行
- BFS(广搜训练题目)
- G - BFS广搜
- Swift:Errors thrown from here are not handled because the enclosing catch is not exhaustive
- $.cookie( ) 用法详细解析
- Swift 数组中 Map,FlatMap,Filter,Reduce的使用
- 如何写出兼容性很好的页面
- 从零开始做远控 第六篇 屏幕监控
- BFS(广搜) 细胞问题
- Java 数组基础
- React 生命周期
- 编译时提示`.text' will not fit in region `iram1_0_seg' 解决方式
- 字符串去除重复字母
- [生存志] 第81节 诗云明志向
- android 开发零起步学习笔记(二十一):AsyncTask的基本用法
- [数据结构]第七章-集合与符号表
- 17.单例模式