题目

一 个NxM(N, M <= 1000)的矩阵形成星空,矩阵中的点有两种字符,'#'代表星星,'.'代表空白,星空中的星星最多5000个;给出K(K<=20)个星图,每 个星图都是HxW(H, W <= 100)的矩阵,矩阵中的点有两种字符,'#'代表星星,'.'代表空白,星图中的星星最多20个。问给出的K个星图在给出的星空中能否找到?

字符匹配问题,最简单粗暴的就是直接枚举。但是分析一下复杂度,发现直接暴力枚举复杂度为:(1000x1000x100x100x20),显然不行。
    继续分析一下题中给出的数字,“星空中的星星最多5000个,星图中的星星最多20个”,根据这个条件,考虑存储星空和星图中的那些星星的坐标,将数据进行压缩,然后看星图中的星星坐标(可能经过移动)能否从星空中找到。
    这本是一个很好的开始!但之后,我就开始了傻逼模式:试图将星图分别在x方向和y方向上进行平移,然后得到新的星星坐标,再从星空中星星坐标库中
查找是否有移动后的星星坐标,期间还很自以为机智的使用二分查找加快速度。。。。但是,没有发现这样做的时间复杂度为
(1000x1000x20xlog(5000)) 仍然很大。。
    网上看了别人的做法,发现只需要将星空中的每一个星星位置作为星图中星星的起始点(从上到下,左到右遇到的第一个星星位置),然后根据星图中星星
之间的相对位置,找到将星图起始点对应过去之后,星图中星星在星空中的位置,判断该位置处是否为一颗星星。这样,直到在星空中找到一颗星星作为星图中星星
的起始星星,这样对应之后,星图中的所有星星在星空中的位置都有星星对应,就可以判断星空中存在该星图。这样时间复杂度为(5000x20x20)
    这道题提交了十多遍,我屮艸芔茻。。我真是一个大傻逼啊!
    现在反思一下自己当时的几个失误:
(1)进行第一步简化之后,没有继续分析复杂度,放弃了继续简化的机会;
(2)简化,存储星空中星星的坐标时,就把星空的整体数据给丢掉了(没有想到存储星空中每个点是什么)。这样在之后查找的时候,用二分,而不是直接判断。。

实现

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<unordered_map>
#include<list>
#include<string>
#include<string.h>
#include<set>
#include<queue>
using namespace std;
int cons_star_c[25][25];
int cons_star_r[25][25];
int cons_width[25];
int cons_height[25];
int stars_c[5005];
int stars_r[5005];
int cons_star_count[25];
int stars_total_count;
int stars_width;
int stars_height;
char map[1005][1005]; bool findConstellation(int k) {
for (int i = 0; i < stars_total_count; i++) {
int j = 0;
for (; j < cons_star_count[k]; j++) {
int col = cons_star_c[k][j] - cons_star_c[k][0] + stars_c[i];
int row = cons_star_r[k][j] - cons_star_r[k][0] + stars_r[i];
if (!(col >= 0 && col < stars_width && row >= 0 && row < stars_height)) {
break;
}
else if (map[row][col] == '.') {
break;
}
}
if (j == cons_star_count[k])
return true;
}
return false;
}
int main() {
int K, H, W;
char symbol;
cin >> K;
scanf("%d", &K);
for (int i = 0; i < K; i++) {
cin >> H >> W;
cons_height[i] = H;
cons_width[i] = W;
int star_count = 0;
for (int row = 0; row < H; row++) {
for (int col = 0; col < W; col++) {
cin >>symbol;
if (symbol == '#') {
cons_star_r[i][star_count] = row;
cons_star_c[i][star_count] = col;
star_count++;
}
}
}
cons_star_count[i] = star_count;
} cin >> H >> W;
stars_width = W;
stars_height = H;
stars_total_count = 0;
for (int row = 0; row < H; row++) {
for (int col = 0; col < W; col++) {
cin >>symbol;
map[row][col] = symbol;
if (symbol == '#') {
stars_r[stars_total_count] = row;
stars_c[stars_total_count] = col;
stars_total_count++;
}
}
}
for (int i = 0; i < K; i++) {
if (findConstellation(i))
printf("Yes\n");
else
printf("No\n");
}
return 0;
}

hiho1099_constellation的更多相关文章

随机推荐

  1. 使用jquery再次封装ajax

    $.fn.ajaxSend = function (type, url, postdata, onSuccess) { $.ajax({ async: false, url: url, type: t ...

  2. Unity中HDR外发光的使用

    1.使用标准材质,设置好Emission外发光 2.Camera下打开HDR,加上Bloom特效 最终效果 如果只勾选HDR,只会有高光感,不会有外发光 注意,正向光照下打开HDR不可用抗锯齿,否则切 ...

  3. windows下wordpress环境快速搭建

    所需要软件下载网址:https://bitnami.com/ 安装使用说明网址:http://www.websoft9.com/wp-content/plugins/documente/documen ...

  4. 给用户添加sudo权限

    centos中默认创建的新用户是没有sudo权限的. 在文件/etc/sudoers中添加即可: ## Allow root to run any commands anywhere root ALL ...

  5. div+css基础

    Div+css技术 Div是用于存放文字,图片,元素的容器 Css 是用于指定存放在div中的内容如何显示,包括内容的位置和外观(层叠样式表) Html 文件 <!--必须引入的文件--> ...

  6. 2016年10月12日 星期三 --出埃及记 Exodus 18:23

    2016年10月12日 星期三 --出埃及记 Exodus 18:23 If you do this and God so commands, you will be able to stand th ...

  7. 常用PDF文档开发库

    C++库: 1,PDF类库 PoDoFo   http://podofo.sourceforge.net/  PoDoFo 是一个用来操作 PDF 文件格式的 C++ 类库.它还包含一些小工具用来解析 ...

  8. 个人对js闭包的理解

      闭包算是前端面试的基础题,但我看了很多关于闭包的文章博客,但感觉很多对于闭包的理想还是有分歧的,现在网上对闭包的理解一般是两种: 有些文章认为闭包必须要返回嵌套函数中里面用到外面函数局部变量的方法 ...

  9. XMPP Server

    XMPPFramework,编译失败,@import libxmlSimu后提示:Module 'libxmlSimu' not found XMPP协议实现原理介绍 XMPP协议学习笔记 四.地址格 ...

  10. HDU 4870 Rating 概率DP

    Rating Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...