hihoCoder1319 岛屿周长 (bfs)
思路:从给定坐标开始bfs,将所有联通点标记,然后把每个联通点的四个方向都判断一下,如果这个方向相邻的是一个非联通点说明需要把这条边实在最外围,即周长的一部分。
AC代码
#include <stdio.h>
#include<string.h>
#include <queue>
using namespace std;
const int maxn = 100+5;
int a[maxn][maxn];
bool con[maxn][maxn], vis[maxn][maxn];
int n, m;
struct Pos{
int x, y;
Pos(int x, int y):x(x), y(y){
}
};
const int dx[] = {0,0,1,-1};
const int dy[] = {1,-1,0,0};
bool isVis(int x, int y) {
if(x < 0 || y < 0 || x >= n || y >= m) return false;
return true;
}
void bfs(int x, int y) {
memset(vis, 0, sizeof(vis));
memset(con, 0, sizeof(con));
queue<Pos>Q;
vis[x][y] = true;
con[x][y] = true;
Q.push(Pos(x, y));
while(!Q.empty()) {
Pos p = Q.front();
Q.pop();
int x = p.x, y = p.y;
for(int i = 0; i < 4; i++) {
int px = x + dx[i];
int py = y + dy[i];
if(!isVis(px, py) || vis[px][py]) continue;
if(a[x][y] != a[px][py]) {
vis[px][py] = 1;
continue;
}
vis[px][py] = 1;
con[px][py] = 1;
Q.push(Pos(px, py));
}
}
}
int solve(int x, int y) {
bfs(x, y);
int ans = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(con[i][j]) {
for(int k = 0; k < 4; k++) {
int x = i + dx[k];
int y = j + dy[k];
if(!isVis(x, y) || !con[x][y]) {
ans++;
}
}
}
}
}
return ans;
}
int main() {
int x, y;
scanf("%d%d%d%d", &n, &m, &x, &y);
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
scanf("%d", &a[i][j]);
}
}
printf("%d\n", solve(x, y));
return 0;
}
如有不当之处欢迎指出!
hihoCoder1319 岛屿周长 (bfs)的更多相关文章
- [LeetCode] Island Perimeter 岛屿周长
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- 中矿新生赛 H 璐神看岛屿【BFS/DFS求联通块/连通块区域在边界则此连通块无效】
时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 32768K,其他语言65536K64bit IO Format: %lld 题目描述 璐神现在有张n*m大小的地图,地图上标明了陆地(用 ...
- 463. Island Perimeter岛屿周长
[抄题]: You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 re ...
- 【leetcode】solution in java——Easy2
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6410409.html 6:Reverse String Write a function that takes ...
- 利用广度优先搜索(BFS)与深度优先搜索(DFS)实现岛屿个数的问题(java)
需要说明一点,要成功运行本贴代码,需要重新复制我第一篇随笔<简单的循环队列>代码(版本有更新). 进入今天的主题. 今天这篇文章主要探讨广度优先搜索(BFS)结合队列和深度优先搜索(DFS ...
- [Swift]LeetCode463. 岛屿的周长 | Island Perimeter
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- 力扣(LeetCode)463. 岛屿的周长
给定一个包含 0 和 1 的二维网格地图,其中 1 表示陆地 0 表示水域. 网格中的格子水平和垂直方向相连(对角线方向不相连).整个网格被水完全包围,但其中恰好有一个岛屿(或者说,一个或多个表示陆地 ...
- LeetCode 463. Island Perimeter岛屿的周长 (C++)
题目: You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 repr ...
- Leetcode题目200.岛屿数量(BFS+DFS+并查集-中等)
题目描述: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 ...
随机推荐
- Inner Join and Left Join 与条件的结合
在使用关系数据库时,表连接和对结果集的筛选是必不可少的查询技能,对于他们的用法你都搞清楚了么?请让我们一起来过一遍. 表创建与初始化: Inner Join 结果集: 对于Inner Join, 条件 ...
- Android原生代码与html5交互
一.首先是网页端,这个就是一些简单的标签语言和JS函数: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN&q ...
- tomcat无法打开8080页面
tomcat已启动 app已经正常执行 但不能打开8080管理页面 可能是在webapps目录下没有ROOT目录
- 【转】GLONASS全球卫星导航系统
GLONASS是“GLOBAL NAVIGATION SATELLITE SYSTE(全球卫星导航系统)”的缩写,作用类似于美国的GPS.欧洲的伽利略卫星定位系统.最早开发于苏联时期,后由俄罗斯继续该 ...
- 浅谈ES6
ECMAScript6.0(简称ES6)是javaScript语言的下一代标准,已经在2015年6月正式发布了.它的目标,使得javaScript语言可以用来编写复杂的大型应用程序,成为企业级开发语言 ...
- postgres允许别人访问连接配置
- isFile() exists() isDirectory()的区别
isFile()public boolean isFile()测试此抽象路径名表示的文件是否是一个标准文.如果该文件不是一个目录,并且满足其他与系统有关的标准,那么该文件是标准文件.由Java应用程序 ...
- 01_Linux软件源配置
一.国内软件源 阿里源:https://mirrors.aliyun.com/ 清华大学:https://mirrors.tuna.tsinghua.edu.cn/ 中科大 : https://mir ...
- 读Ghost博客源码与自定义Ghost博客主题
我使用的Ghost博客一直使用者默认的Casper主题.我向来没怎么打理过自己博客,一方面认为自己不够专业,很难写出质量比较高的文字:另一方面认为博客太耗时间,很容易影响正常的工作内容.最近公司即将搬 ...
- WebService 的工作原理
Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的 ...