hiho1099_constellation
题目
一 个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的更多相关文章
随机推荐
- noi 8465 马走日
8465:马走日 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 1024kB 描述 马在中国象棋以日字形规则移动. 请编写一段程序,给定n*m大小的棋盘,以及马的初始位置(x,y ...
- selenium帮助手册以及 webdriver的各种driver
帮助手册 http://selenium-python.readthedocs.io/locating-elements.html 转载于:http://blog.csdn.net/five3/art ...
- lua中特殊用法
th> a=torch.zeros(,) [.0001s] th> a [torch.DoubleTensor of size 1x5] [.0001s] th> a[{,floor ...
- ubuntu截屏
按下键盘上的 PrintScreen 键保存全屏截图 按下 Alt+PrintScreen 快捷键截取当前窗口 按下 Shift+PrintScreen 快捷键截取任意矩形内容
- 2016 Al-Baath University Training Camp Contest-1 C
Description Rami went back from school and he had an easy homework about bitwise operations (and,or, ...
- php修改和增加xml结点属性
<?xml version="1.0" encoding="UTF-8" ?> <clientSet> <server url=& ...
- Linux 性能监控、测试、优化工具
Linux 平台上的性能工具有很多,眼花缭乱,长期的摸索和经验发现最好用的还是那些久经考验的.简单的小工具.系统性能专家 Brendan D. Gregg 在最近的 LinuxCon NA 2014 ...
- EF中限制字段显示长度
在EF中有些添加的字段 文本显示超多文字,想截取显示又没有截取功能. 怎么办? 我们可以在EF中类的属性中设置 你想限制这个用户名只能有10个字符长度 public String UserName { ...
- 谢启鸿谈"如何学好高等代数"
高等代数和数学分析.空间解析几何一起,并称为数学系本科生的三大基础课.所谓基础课,顾名思义,就是本科四年学习的所有数学课程,都是以上述三门课作为基础的.因此对一年级新生而言,学好这三门基础课,其重要性 ...
- C#部署安装,将用户安装路径记录下写入注册表,并启动
安装部署程序,将安装目录写入注册表 (1)在“安装部署项目”上点击“注册表编辑器” (2)在HKey_LOCAL_MACHINE_SoftWare 下新建键 Manufacturer 代表软件的制造商 ...