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 ...
随机推荐
- angular导出文件保存在本地
$scope.ev_click = function(obj){ var ev = document.createEvent("MouseEvents"); ev.initMous ...
- python不可以打印.doc文件
[背景] 需求: 打印word文件 模块: python-docx [问题] 传递xxx.doc文件给python脚本,执行后,控制台没有内容输出 经查询后了解到,大致理由: doc是早一代的word ...
- go语言入门(二)
Go 语言变量 Go 语言变量名由字母.数字.下划线组成,其中首个字母不能为数字. 声明变量的一般形式是使用 var 关键字: var identifier type 变量声明 第一种,指定变量类型, ...
- google浏览器中,使用clockwork 来调试
参考:https://laravel-china.org/courses/laravel-package/1976/debugging-tool-under-chrome-itsgoingdclock ...
- Linux创建ftp并设置权限以及忘记ftp帐号(密码)修改
忘记ftp密码修改方法: 1.登录服务器 cd /etc/vsftpdcat ftpusers找到对应的ftp用户名 (如果用户名也忘记了 那么 cd /etc 然后cat passwd 查看用户和 ...
- WireShark出现The NPF driver isn't running的问题
昨天开始尝试装上了wireshark网络监视软件,可是今天打开去总是出现“The NPF driver isn't running.You may have trouble capturing or ...
- 【转载】python-协程
转载自:廖雪峰的官方网站 协程,又称微线程,纤程.英文名Coroutine. 协程的概念很早就提出来了,但直到最近几年才在某些语言(如Lua)中得到广泛应用. 子程序,或者称为函数,在所有语言中都是层 ...
- DNS使用TCP还是UDP?
DNS同时占用UDP和TCP端口53是公认的,这种单个应用协议同时使用两种传输协议的情况在TCP/IP栈也算是个另类.下面将介绍DNS分别在什么情况下使用这两种协议. TCP与UDP简介 TCP ...
- 小技巧:tar命令打包目录时,排除文件和目录的命令
今天不巧要用上,百度. tar zcvf fd.tar.gz pardir --exclude=pardir/file1 --exclude=pardir/dir1
- TPC-H数据导入MySQL教程
0. TPC-H是啥 TPC-H是TPC提供的一个benchmark,用来模拟一个现实中的商业应用,可以生成一堆虚构的数据,且自带一些查询,可以导入到各种数据库中来模拟现实需求,检查性能. 具体是怎样 ...