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 ...
随机推荐
- EasyUi组合条件分页查询
1.引入css与js文件 <link rel="stylesheet" type="text/css" href="themes/default ...
- i春秋第二届春秋欢乐赛RSA256writeup
i春秋第二届春秋欢乐赛writeup 下载之后进行解压 发现四个文件 0x01看到题目是RSA的 又看到public.key 所以直接用kali linux的openssl 0x02可以看到e就是E ...
- ThinkPHP的运行流程-1
我在index\Lib\Action\目录下新建了一个ShowAction.class.php文件.ps:该目录是控制器的目录. 然后这个文件中继承了action这个类.代码如下: 1 2 3 4 5 ...
- Linux-Load Average解析(转)
load Average 1.1:什么是Load?什么是Load Average? Load 就是对计算机干活多少的度量(WikiPedia:the system Load is a measur ...
- kvm安装准备
到实际情况下,做虚拟化是直接做在真机上. 但实验时,可以在虚拟机上进行.(因为做实验的时候没办法连接到桥接模式的网络,所以使用了NAT方式来连接网络) 在vmware安装centos 64bit fo ...
- RestTemplate OR Spring Cloud Feign 上传文件
SpringBoot,通过RestTemplate 或者 Spring Cloud Feign,上传文件(支持多文件上传),服务端接口是MultipartFile接收. 将文件的字节流,放入ByteA ...
- 守护进程daemon函数
#include <unistd.h> int daemon(int nochdir,int noclose) 在创建精灵进程的时候,往往需要将精灵进程的工作目录修改为"/&q ...
- Django之Cookie、Session和自定义分页
cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不 ...
- CVE-2012-0158个人分析
CVE-2012-0158是一个比较有名的老漏洞了,这次从论坛上找到一个poc文件,利用这个poc来分析CVE-2012-0158漏洞的形成. http://bbs.pediy.com/showthr ...
- vue 子父组件之间的通信,及在调用组件的地方
这里是用了 element ui 你们也可以看一下管方的文档 http://element.eleme.io/#/zh-CN/component/installation 组件html <div ...