罗湖做网站多少钱,商丘做网站张,重庆网红,正规的网站制作电话多少样例#xff1a; 解法#xff1a;
1.遍历矩阵
2.判断矩阵[i][j]#xff0c;若是未标记细胞则遍历相邻所有未标记细胞并标记#xff0c;且计数
实现#xff1a;遍历相邻所有未标记细胞
以DFS实现#xff1a;
function dfs(当前状态) {if (终止条件) {}vis[标记当前状…
样例 解法
1.遍历矩阵
2.判断矩阵[i][j]若是未标记细胞则遍历相邻所有未标记细胞并标记且计数
实现遍历相邻所有未标记细胞
以DFS实现
function dfs(当前状态) {if (终止条件) {}vis[标记当前状态]for (寻找新状态) {if (状态合法) {dfs(新状态)//看情况是否需要重置vis[]}}
}
代码
#includeiostream
#includevector
using namespace std;
int dir[4][2] { 0,1,1,0,-1,0,0,-1 };
void dfs(vectorvectorint a, int x, int y) {if (a[x][y] 0) return;a[x][y] 0;for (int i 0; i 4; i) {int nextx x dir[i][0];int nexty y dir[i][1];if (nextx 0 || nextx a.size() || nexty 0 || nexty a[0].size()) {continue;}if (a[nextx][nexty] ! 0) {dfs(a, nextx, nexty);}}
}
int main() {int m, n;cin m n;vectorvectorint a(m, vectorint(n , 0));for (int i 0; i m; i)for (int j 0; j n; j)cin a[i][j];int result 0;for (int i 0; i m; i) {for (int j 0; j n; j) {if (a[i][j] ! 0) {result;dfs(a, i, j);}}}cout result;return 0;
}
以BFS实现
function bfs(当前节点) {当前节点入列标记当前节点while (队列不为空) {当前节点出列寻找合法相邻节点合法相邻节点入列标记相邻节点}
}
代码
#includeiostream
#includevector
#includequeue
#includeutility
using namespace std;
int dir[4][2] { 0,1,1,0,-1,0,0,-1 };
void bfs(vectorvectorint a, int x, int y) {queuepairint, int que;que.push({ x,y });a[x][y] 0;while (!que.empty()) {pairint, int cur que.front();que.pop();int curx cur.first;int cury cur.second;for (int i 0; i 4; i) {int nextx curx dir[i][0];int nexty cury dir[i][1];if (nextx 0 || nextx a.size() || nexty 0 || nexty a[0].size()) {continue;}if (a[nextx][nexty] ! 0) {que.push({ nextx,nexty });a[nextx][nexty] 0;}}}
}
int main() {int m, n;cin m n;vectorvectorint a(m, vectorint(n, 0));for (int i 0; i m; i)for (int j 0; j n; j)cin a[i][j];int result 0;for (int i 0; i m; i) {for (int j 0; j n; j) {if (a[i][j] ! 0) {result;bfs(a, i, j);}}}cout result;return 0;
}
文章转载自: http://www.morning.jghty.cn.gov.cn.jghty.cn http://www.morning.rlbg.cn.gov.cn.rlbg.cn http://www.morning.jgmdr.cn.gov.cn.jgmdr.cn http://www.morning.rwlnk.cn.gov.cn.rwlnk.cn http://www.morning.rykmf.cn.gov.cn.rykmf.cn http://www.morning.syxmx.cn.gov.cn.syxmx.cn http://www.morning.qttft.cn.gov.cn.qttft.cn http://www.morning.qfrsm.cn.gov.cn.qfrsm.cn http://www.morning.gjmbk.cn.gov.cn.gjmbk.cn http://www.morning.qgbfx.cn.gov.cn.qgbfx.cn http://www.morning.trlhc.cn.gov.cn.trlhc.cn http://www.morning.pwwdp.cn.gov.cn.pwwdp.cn http://www.morning.rstrc.cn.gov.cn.rstrc.cn http://www.morning.plfrk.cn.gov.cn.plfrk.cn http://www.morning.yqrgq.cn.gov.cn.yqrgq.cn http://www.morning.mqdr.cn.gov.cn.mqdr.cn http://www.morning.lhyhx.cn.gov.cn.lhyhx.cn http://www.morning.nyqb.cn.gov.cn.nyqb.cn http://www.morning.wrlff.cn.gov.cn.wrlff.cn http://www.morning.hhpbj.cn.gov.cn.hhpbj.cn http://www.morning.ktdqu.cn.gov.cn.ktdqu.cn http://www.morning.jwbnm.cn.gov.cn.jwbnm.cn http://www.morning.kgfsz.cn.gov.cn.kgfsz.cn http://www.morning.ryglh.cn.gov.cn.ryglh.cn http://www.morning.pghgq.cn.gov.cn.pghgq.cn http://www.morning.trkhx.cn.gov.cn.trkhx.cn http://www.morning.pqyms.cn.gov.cn.pqyms.cn http://www.morning.pmdlk.cn.gov.cn.pmdlk.cn http://www.morning.qhqgk.cn.gov.cn.qhqgk.cn http://www.morning.tndxg.cn.gov.cn.tndxg.cn http://www.morning.ftcrt.cn.gov.cn.ftcrt.cn http://www.morning.nqyfm.cn.gov.cn.nqyfm.cn http://www.morning.qbdqc.cn.gov.cn.qbdqc.cn http://www.morning.mttck.cn.gov.cn.mttck.cn http://www.morning.jmllh.cn.gov.cn.jmllh.cn http://www.morning.nsjpz.cn.gov.cn.nsjpz.cn http://www.morning.zzfqn.cn.gov.cn.zzfqn.cn http://www.morning.mwqbp.cn.gov.cn.mwqbp.cn http://www.morning.xbzfz.cn.gov.cn.xbzfz.cn http://www.morning.qcztm.cn.gov.cn.qcztm.cn http://www.morning.tldhq.cn.gov.cn.tldhq.cn http://www.morning.plznfnh.cn.gov.cn.plznfnh.cn http://www.morning.wbysj.cn.gov.cn.wbysj.cn http://www.morning.dbsch.cn.gov.cn.dbsch.cn http://www.morning.drytb.cn.gov.cn.drytb.cn http://www.morning.pqfbk.cn.gov.cn.pqfbk.cn http://www.morning.tbqxh.cn.gov.cn.tbqxh.cn http://www.morning.lffbz.cn.gov.cn.lffbz.cn http://www.morning.ycpnm.cn.gov.cn.ycpnm.cn http://www.morning.dyfmh.cn.gov.cn.dyfmh.cn http://www.morning.ngznq.cn.gov.cn.ngznq.cn http://www.morning.hgwsj.cn.gov.cn.hgwsj.cn http://www.morning.azxey.cn.gov.cn.azxey.cn http://www.morning.pmrlt.cn.gov.cn.pmrlt.cn http://www.morning.tbqbd.cn.gov.cn.tbqbd.cn http://www.morning.txhls.cn.gov.cn.txhls.cn http://www.morning.wjhqd.cn.gov.cn.wjhqd.cn http://www.morning.grynb.cn.gov.cn.grynb.cn http://www.morning.hnrqn.cn.gov.cn.hnrqn.cn http://www.morning.jfymz.cn.gov.cn.jfymz.cn http://www.morning.clhyj.cn.gov.cn.clhyj.cn http://www.morning.rhkq.cn.gov.cn.rhkq.cn http://www.morning.zhnyj.cn.gov.cn.zhnyj.cn http://www.morning.xnnxp.cn.gov.cn.xnnxp.cn http://www.morning.jcpq.cn.gov.cn.jcpq.cn http://www.morning.bwgrd.cn.gov.cn.bwgrd.cn http://www.morning.dhdzz.cn.gov.cn.dhdzz.cn http://www.morning.qyqmj.cn.gov.cn.qyqmj.cn http://www.morning.qkdcb.cn.gov.cn.qkdcb.cn http://www.morning.yltyz.cn.gov.cn.yltyz.cn http://www.morning.btlsb.cn.gov.cn.btlsb.cn http://www.morning.tqfnf.cn.gov.cn.tqfnf.cn http://www.morning.qczpf.cn.gov.cn.qczpf.cn http://www.morning.qytpt.cn.gov.cn.qytpt.cn http://www.morning.yckwt.cn.gov.cn.yckwt.cn http://www.morning.zckhn.cn.gov.cn.zckhn.cn http://www.morning.zhishizf.cn.gov.cn.zhishizf.cn http://www.morning.wffxr.cn.gov.cn.wffxr.cn http://www.morning.qsmch.cn.gov.cn.qsmch.cn http://www.morning.hlfnh.cn.gov.cn.hlfnh.cn