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 岛屿的更多相关文章

  1. hihoCoder1310 岛屿 (dfs)

    思路:首先dfs求得所有联通块,再搜索的同时把每个联通块的坐标都保存下来,然后把每个联通块处理一下–首先得到某个联通块的最小横坐标和纵坐标,然后让每个坐标去减去这个横坐标和纵坐标.相当于使得所有联通块 ...

  2. [LeetCode] Island Perimeter 岛屿周长

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  3. [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 ...

  4. [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 ...

  5. hiho #1310 : 岛屿 (dfs,hash)

    题目2 : 岛屿 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给你一张某一海域卫星照片,你需要统计: 1. 照片中海岛的数目 2. 照片中面积不同的海岛数目 3. 照 ...

  6. NYOJ--1237最大岛屿

    最大岛屿 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 神秘的海洋,惊险的探险之路,打捞海底宝藏,激烈的海战,海盗劫富等等.加勒比海盗,你知道吧?杰克船长驾驶着自己的的 ...

  7. 岛屿(洛谷 U5399)

    题目背景 放假了,Lkw和mm到岛上旅游.阳光明媚,风景秀丽.正当Lkw和mm享受眼前这旖旎的风光时,突降大雨,小岛上开始积水,没过几分钟,水便快要触及膝盖.Lkw和mm意识到了事态的严重性,赶紧向高 ...

  8. 【bzoj1791】岛屿

    [bzoj1791]岛屿 题意 求基环树的直径. \(n\leq 100000\) 分析 这道题的题解貌似很少啊. 所以自己也写一份吧. 首先找出基环树的环. 那么树的直径有两种情况: ①以环为根的某 ...

  9. 项目源码--Android迷幻岛屿综合游戏

    下载源码 技术要点: 1.游戏开发综合技术 2.多线程机制实现游戏逻辑 3.自定义控件,系统控件等综合图层的使用 4.图层素材动画的综合技术 5.游戏算法的实现 6. OpenGL ES的综合使用 7 ...

随机推荐

  1. Linux下libevent安装与示例

    http://www.cnblogs.com/kunhu/p/3632225.html

  2. MySQL源码分析(一)

    近段时间简单看了下Mysql源码相关内容,主要从一个select查询出发,查看了一下整个代码结构.分析总结如下: https://mubu.com/doc/explore/13965

  3. Linux下多路径multipath配置【转】

    一.multipath在redhat 6.2中的基本配置: 1. 通过命令:lsmod |grep dm_multipath  检查是否正常安装成功.如果没有输出说明没有安装那么通过yum功能安装一下 ...

  4. 190.Reverse Bits---位运算

    题目链接:https://leetcode.com/problems/reverse-bits/description/ 题目大意:将数值的二进制反转. 法一(借鉴):由于是无符号32位整型,当二进制 ...

  5. Class.this 和 this 的有什么不同

    转载:http://www.cnblogs.com/liwei45212/archive/2013/04/17/3026364.html 在阅读Java代码的時候 我们时会看到Class.this的使 ...

  6. Tutorial 2: Requests and Responses

    转载自:http://www.django-rest-framework.org/tutorial/2-requests-and-responses/ Tutorial 2: Requests and ...

  7. jstat分析JVM内存

    zabbix: Jstat:gcutil:Old space utilization(%) S0  — Heap上的 Survivor space 0 区已使用空间的百分比S1  — Heap上的 S ...

  8. jquery 获取某a标签的href地址 实现页面加载时跳转

    jQuery(document).ready(function(){if(jQuery("#zzjg a").length>0){var hrefValue = jQuery ...

  9. 谷歌翻译python接口

    项目地址:  https://github.com/ssut/py-googletrans 安装: sudo pip install googletrans 使用: #!/usr/bin/python ...

  10. 第三方登陆微博、qq、微信

    源文:http://blog.csdn.net/tivonalh/article/details/60954373 假设是已经申请完成各平台开发者账号. 先来简单的,微博和QQ 微博: 引入微博JS ...