题目

一 个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. JAVA基础知识之JVM-——自定义类加载器

    JVM中除了根加载器之外其他加载器都是ClassLoader的子类实例, 可以通过扩展ClassLoader的子类,通过重写方法来实现自定义的类加载器. ClassLoader中有两个关键的方法如下, ...

  2. centOS安装Mysql指南

    centOS安装Mysql指南 说明:使用操作系统centOS6.4 32位系统:mysql:mysql-5.7.10-linux-glibc2.5-i686.tar.gz; 一.准备 下载mysql ...

  3. ios沙盒路径

    http://www.cnblogs.com/ios-wmm/p/3299695.html iOS沙盒路径的查看和使用 NSString *path = NSHomeDirectory();//主目录 ...

  4. xtrabackup 安装、备份、还原及错误处理 教程

    xtrabackup 是MYSQL的一个备份软件 Xtrabackup是一个对InnoDB做数据备份的工具,支持在线热备份(备份时不影响数据读写),是商业备份工具InnoDB Hotbackup的一个 ...

  5. 【转载】COM 连接点

    原文:COM 连接点 CLR 完全介绍 COM 连接点 Thottam R. Sriram 来自:http://msdn.microsoft.com/zh-cn/magazine/cc163361.a ...

  6. left join 等连接查询遇到同名字段覆盖问题

    可以在查询时给字段赋别名,但是需要注意以下:*的位置要在最前面,放在其他地方都会出错.这种写法同名覆盖的字段还在,然后在*的后面加上别名字段,已经可以满足所有需求了 SELECT *,r.id as ...

  7. asynchronous vs non-blocking

    http://stackoverflow.com/questions/2625493/asynchronous-vs-non-blocking In many circumstances they a ...

  8. GCC编译器代码优化

    代码优化是指编译器通过分析源代码,找出其中尚未达到最优的部分,然后对其重新进行组合,目的是改善程序的执行性能.GCC提供的代码优化功能非常强大,它通过编译选项-On来控制优化代码的生成,其中n是一个代 ...

  9. Quick Trick About Using Dbms_Metadata With Forms_DDL In Oracle Forms

    Example is given below to fetch any Oracle objects DDL script using DBMS_Metadata.Get_DDL command in ...

  10. XML详解:第二部分 XML Schema

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...