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'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 ...
随机推荐
- python3 第七章 - 循环语句
为了让计算机能计算成千上万次的重复运算,我们就需要循环语句. Python中的循环语句有 while for 循环语句的执行过程,如下图: while 循环 Python中while语句的一般形式: ...
- java.lang.ClassNotFoundException: com.radiadesign.catalina.session.RedisSessionHandlerValve
org.apache.tomcat.util.digester.Digester.startElement Begin event threw exception java.lang.ClassNot ...
- Universe Design Tool Using JDBC connect Sybase/Oracle Get Error
一.针对Sybase 1 使用SAP Universe 设计工具连接Sybase数据库报错,报错如下: “CS: Java Class not found in classpath : com.syb ...
- js promise看这篇就够了
一.背景 大家都知道nodejs很快,为什么会这么快呢,原因就是node采用异步回调的方式来处理需要等待的事件,使得代码会继续往下执行不用在某个地方等待着.但是也有一个不好的地方,当我们有很多回调的时 ...
- spring的jar各包作用
http://yjwen337.blog.163.com/blog/static/3625847820106132949858/[转]spring.jar是包含有完整发布的单个jar 包,spring ...
- HTML学习——标签
1.</hr>效果: 2.<q>简短文本引用, <blockquote>长文本引用, 表象:为文本添加一个双引号,实:一个语义,引用别人的话语. 3.&nb ...
- git 对 Microsoft Word 进行版本控制
昨天中国高校发生了一件骇人听闻的事情,听说不少高校的校园网用户连接校园网被勒索病毒给黑了,重要文件全部被加密,必须要支付赎金才能解密,具体新闻可以参见:http://www.sohu.com/a/14 ...
- Zabbix3.4部署
Zabbix简介 zabbix(音同 zæbix)是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活 ...
- html5的video标签自动播放
概念澄清 这里的"自动播放",是指用户的视觉效果,并不一定是元素自身的自动播放. 查看相关文档后,有以下两种最简方案. 配置属性 发现有video标签有一个自动播放的属性autop ...
- iOS-常用三方工具
#菜单 pod 'LGSideMenuController' # 刷新 pod 'MJRefresh' # 网络请求 pod 'AFNetworking', '~> 3.0' # 图片缓存 po ...