hihocoder1310 岛屿
hihocoder1310 岛屿
题意:
中文题意
思路:
dfs,面积和数量都很好求,问题在岛屿形状上,感觉让人比较麻烦,用vector保存各个点,只要两个岛之间每个点距离一样就好了,这里的形状的定义比较狭隘,就是平移可得的意思,如果换成可以旋转等变换得到,会比较麻烦感觉。
ac代码:
C++
// daoyu1310.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<cstdio>
#include<iostream>
#include<string>
#include<set>
#include<vector>
using namespace std;
int n, m; //1 <= n, m <= 50
char map[51][51]; //store map
int dir[4][2] = { 1,0,-1,0,0,1,0,-1 };
struct island {
int area;
vector<pair<int, int> > shape;
island(int x, int y, int s)
{
shape.push_back(make_pair(x, y));
area = s;
}
};
bool issame(island* a, island* b)
{
if (a->area != b->area) return false;
for (int k = 1; k < a->shape.size(); k++)
{
if (a->shape[k] != b->shape[k]) return false;
}
return true;
}
int count(int i, int j, island* is)
{
if (map[i][j] == '#')
{
map[i][j] = '*'; //represent visited
is->shape.push_back(make_pair(i - is->shape[0].first, j - is->shape[0].second));
}
else
{
return 0;
}
int s = 1;
int nexti, nextj;
for (int k = 0; k < 4; k++)
{
nexti = i + dir[k][0];
nextj = j + dir[k][1];
if (nexti >= 0 && nexti < n && nextj >= 0 && nextj < m && map[nexti][nextj] == '#')
s += count(nexti, nextj, is);
}
return s;
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> map[i][j];
vector<island*> res;
set<int> area;
int countshape = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
if (map[i][j] == '#')
{
island* il = new island(i, j, 1);
il->area = count(i, j, il);
area.insert(il->area);
countshape++;
for (int k = 0; k < res.size(); k++)
{
if (issame(il, res[k]))
{
countshape--;
break;
}
}
res.push_back(il);
}
}
//for (int i = 0; i < res.size(); i++)
//{
// cout << res[i]->area << endl;
// for (int j = 0; j < res[i]->shape.size(); j++)
// {
// cout << res[i]->shape[j].first << " " << res[i]->shape[j].second<< endl;
// }
// cout << endl;
//
//}
//if (issame(res[0],res[2])) cout << "hello" << endl;
cout << res.size() << " " << area.size() << " " << countshape << endl;
return 0;
}
hihocoder1310 岛屿的更多相关文章
- hihoCoder1310 岛屿 (dfs)
思路:首先dfs求得所有联通块,再搜索的同时把每个联通块的坐标都保存下来,然后把每个联通块处理一下–首先得到某个联通块的最小横坐标和纵坐标,然后让每个坐标去减去这个横坐标和纵坐标.相当于使得所有联通块 ...
- [LeetCode] Island Perimeter 岛屿周长
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- [LeetCode] Number of Islands II 岛屿的数量之二
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- [LeetCode] Number of Islands 岛屿的数量
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- hiho #1310 : 岛屿 (dfs,hash)
题目2 : 岛屿 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给你一张某一海域卫星照片,你需要统计: 1. 照片中海岛的数目 2. 照片中面积不同的海岛数目 3. 照 ...
- NYOJ--1237最大岛屿
最大岛屿 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 神秘的海洋,惊险的探险之路,打捞海底宝藏,激烈的海战,海盗劫富等等.加勒比海盗,你知道吧?杰克船长驾驶着自己的的 ...
- 岛屿(洛谷 U5399)
题目背景 放假了,Lkw和mm到岛上旅游.阳光明媚,风景秀丽.正当Lkw和mm享受眼前这旖旎的风光时,突降大雨,小岛上开始积水,没过几分钟,水便快要触及膝盖.Lkw和mm意识到了事态的严重性,赶紧向高 ...
- 【bzoj1791】岛屿
[bzoj1791]岛屿 题意 求基环树的直径. \(n\leq 100000\) 分析 这道题的题解貌似很少啊. 所以自己也写一份吧. 首先找出基环树的环. 那么树的直径有两种情况: ①以环为根的某 ...
- 项目源码--Android迷幻岛屿综合游戏
下载源码 技术要点: 1.游戏开发综合技术 2.多线程机制实现游戏逻辑 3.自定义控件,系统控件等综合图层的使用 4.图层素材动画的综合技术 5.游戏算法的实现 6. OpenGL ES的综合使用 7 ...
随机推荐
- Java简单爬虫(一)
简单的说,爬虫的意思就是根据url访问请求,然后对返回的数据进行提取,获取对自己有用的信息.然后我们可以将这些有用的信息保存到数据库或者保存到文件中.如果我们手工一个一个访问提取非常慢,所以我们需要编 ...
- perl6正则 1: ~~ , //, m//, rx//
~~ perl6 中, 要匹配一个正则, 使用 ~~ 智能匹配符. > so 'abcde' ~~ /a.c/ True > so 'abcde' ~~ /a.d/ False > ...
- ubuntu之一些安装配置的坑
前言 本博客记录自己使用ubuntu的一些错误和坑. ubuntu不支持yum下载安装机制 命令 sudo apt install yum 是可以安装yum的,但安装好后执行: $ yum insta ...
- slave->pxc后GTID不一致
以下两个参数在两个节点是对得上的. | wsrep_last_applied | 3363764 | | wsrep_last_committed | 3363764 但show master sta ...
- redis源码分析——aofrewrite
随着redis的运行,aof会不断膨胀(对于一个key会有多条aof日志),导致通过aof恢复数据时,耗费大量不必要的时间.redis提供的解决方案是aof rewrite.根据db的内容,对于每个k ...
- VMware-workstation-6.5.2-156735.exe
480HD-KPZ2X-TA56C-4YTQQ VMware 12 专业版永久许可证密钥 5A02H-AU243-TZJ49-GTC7K-3C61N
- centos6下mysql的主从复制的配置
2015年9月17日 23:00:36 update 想要好好了解mysql复制,还是去看看<高性能MySQL>(第三版)好了,上面说的比较详细. =========== 在本地用virt ...
- 《HBase权威指南》学习笔记
第一章 简介 背景: GFS:集群存储海量数据,数据在节点间冗余复制,即使一台存储服务器发生故障,也不会影响可用性. GFS的缺点:适合存储少许非常大的文件,而不适合存储大量小文件,因为文件的元数据 ...
- lr_get_transaction_duration 函数介绍
lr_get_transaction_duration 用于获取事务所消耗的时间. 实例: Action() { double trans_time; //定义变量 web_url("www ...
- 在linux下安装sbt
1.到官方网站下载deb包,下载地址:https://dl.bintray.com/sbt/debian/sbt-1.0.3.deb 2.点击下载的deb包进行安装 3.安装完成后,在terminal ...