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'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 ...
随机推荐
- Git工具的使用教程
Git 是一种版本控制工具,也叫作版本管理软件(分布式管理软件).这里介绍Git的基本使用步骤,关于 Git 更详细的介绍,读者可以参考其官方网站提供的文档. 1 安装Git 在Ubuntu系统中安 ...
- sqlserver datetime的bug
sqlserver datetime 的毫秒的个位似乎存在bug,只有0.3.7这三个值,比如: 2018-01-20 23:59:59:999会变成2018-01-21 00:00:00.000 2 ...
- mybatis中@Param的用法和作用
用注解来简化xml配置的时候,@Param注解的作用是给参数命名,参数命名后就能根据名字得到参数值,正确的将参数传入sql语句中 我们先来看Mapper接口中的@Select方法 package Ma ...
- 一个滑动选中RecyclerView中Item的布局SlidingCheckLayout,手指滑过Item时多项选中。
SlidingCheckLayout是一个滑动选中RecyclerView中Item的布局,手指滑过Item时多项选中. 作者:竹尘居士 github:https://github.com/homgw ...
- Android 初了解
1.1G-4G 1G 大哥大 语音通话 2G 小灵通 采用GSM,美国的一个军方标准,后来被民用了. 可以发短信了,上网的网址不是www,是wap.baidu.com 3G 可以上网了,直接用ww ...
- toString()方法细节
toString(),每一个非基本类型的对象都有一个toString()方法,当编译器需要一个Sting,而你只有一个对象时,该方法会自动调用. class WaterSource { private ...
- Python学习笔记(二):字典
字典由多个键及与其对应的值构成的键值对构成,字典中键唯一,值不唯一. 1)dict 函数: >>>items=[('name','lilei'),('age',12)] >&g ...
- 20_Python字典总结
字典: python内置了字典:dic全称dictionary.其他语言中成为map,使用key-value的存储,键-值.具有极快的查找速度类似与我们查字典,我们要查一个字1.从第一页往后翻,知道翻 ...
- c# 实现mysql事务
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 全部用startssl生成的证书,配置Apache使其支持SSL
Apache的编译安装见这篇: http://www.cnblogs.com/yjken/p/3921840.html 网上查阅了一大批资料,得知自己生成的证书是会被浏览器提示“证书不安全”的,我也就 ...