hihoCoder #1099 Constellations

Description

Recently Little Hi started to like astronomy and downloaded the pictures of K constellations. He wonders how many of them he can spot in the night?

Input

Line 1: K(1 <= K <= 20), the number of constellations. K constellation pictures follow. The format of each picture is: Line 1 of each picture: H and W(5 <= H, W, <= 100), the height and width of the picture. Line 2~H+1 of each picture: An H*W matrix of characters representing the picture of a constellation. Each line contains W characters. ‘#‘ for a star and ‘.‘ for empty area. There are no more than 20 stars in each constellation. After K constellations is the sky Little Hi looks to in the night. Line 1 of sky: N and M(100 <= N, M <= 1000), the size of the sky Little Hi looks to. Line 2~N of sky: An N*M matrix of characters representing the sky Little Hi looks to. Each line contains M characters. ‘#‘ for a star and ‘.‘ for empty area. There are no more than 5000 stars in the sky.

Output

For each constellation in the Input output "Yes" or "No" indicating whether Little Hi can spot the constellation in the sky. All pictures of constellations and the sky Little Hi looks to are in the same direction so there is no need to rotate the pictures.

Hint

A constellation can be spoted if and only if all stars in the constellation can be matched in the sky. It is allowed that two spoted constellations share common stars.

Sample Input

3
5 5
#....
.....
...#.
.....
.#...
5 5
....#
.....
.....
#....
....#
5 6
.....#
......
#.....
......
....#.
10 10
.......#..
..........
..#.......
..........
......#...
..........
..........
..#.......
......#...
..........

Sample Output

No
Yes
Yes

Solution

枚举比较 为减少比较次数 存储星座数据时 只需存储每个星座中星星的相对位置。TALK IS CHEAP......

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4
 5 struct point
 6 {
 7     int dx;
 8     int dy;
 9 };
10
11 struct constellation
12 {
13     int S;
14     point path[20];
15 };
16
17 constellation arr[20];
18 int K;
19 int N,M;
20 char sky[1000][1000];
21
22 void solve()
23 {
24     for (int k = 0; k < K; ++k) {
25         bool f = false;
26         for (int i = 0; i < N && !f; ++i) {
27             for (int j = 0; j < M && !f; ++j) {
28                 bool flag = true;
29                 if (sky[i][j] == ‘#‘) {
30                     for (int s = 1; s < arr[k].S ; ++s) {
31                         int _i = i+arr[k].path[s].dx;
32                         int _j = j+arr[k].path[s].dy;
33                         if (_i >= N ||  _j >= M || sky[_i][_j] != ‘#‘) {
34                             flag = false;
35                             break;
36                         }
37                     }
38                     if (flag) f = true;
39                 }
40
41
42             }
43         }
44         if (f) {
45             cout << "Yes" << endl;
46         }
47         else {
48             cout << "No" << endl;
49         }
50     }
51
52
53 }
54
55 int main()
56 {
57     scanf ("%d", &K);
58     int w,h;
59     for (int k = 0; k < K; ++k) {
60         scanf("%d %d", &h, &w);
61         char c;
62         int sx,sy;
63         arr[k].S = 0;
64         for (int i = 0; i < h; ++i) {
65             for (int j = 0; j < w; ++j) {
66                 scanf(" %c", &c);
67                 if (c == ‘#‘) {
68                     if (arr[k].S == 0) {
69                         sx = i;
70                         sy = j;
71                         arr[k].S = 1;
72                     }
73                     else {
74                         arr[k].path[arr[k].S++] = (point){i-sx, j-sy};
75                     }
76                 }
77             }
78         }
79     }
80
81     scanf("%d %d", &N, &M);
82
83     for (int i = 0; i < N; ++i) {
84         for (int j = 0; j < M; ++j) {
85             scanf (" %c", sky[i] + j);
86         }
87     }
88     solve();
89 }
时间: 2024-10-31 15:35:48

hihoCoder #1099 Constellations的相关文章

[hihoCoder#1381]Little Y&#39;s Tree

[hihoCoder#1381]Little Y's Tree 试题描述 小Y有一棵n个节点的树,每条边都有正的边权. 小J有q个询问,每次小J会删掉这个树中的k条边,这棵树被分成k+1个连通块.小J想知道每个连通块中最远点对距离的和. 这里的询问是互相独立的,即每次都是在小Y的原树上进行操作. 输入 第一行一个整数n,接下来n-1行每行三个整数u,v,w,其中第i行表示第i条边边权为wi,连接了ui,vi两点. 接下来一行一个整数q,表示有q组询问. 对于每组询问,第一行一个正整数k,接下来一

hihoCoder 1175:拓扑排序二

题目链接: http://hihocoder.com/problemset/problem/1175 题目难度:一星级(简单题) 今天闲来无事,决定刷一道水题.结果发现这道水题居然把我卡了将近一个钟头. 最后终于调通了.总结起来,原因只有一个:不够仔细. 思路不用细说了,就是拓扑排序的简单应用.然而,一些不起眼的细节才是让你掉坑里的真正原因. 猜猜哪儿可能出bug? // A simple problem, but you can't be too careful with it. #inclu

【HDOJ】1099 Lottery

题意超难懂,实则一道概率论的题目.求P(n).P(n) = n*(1+1/2+1/3+1/4+...+1/n).结果如果可以除尽则表示为整数,否则表示为假分数. 1 #include <cstdio> 2 #include <cstring> 3 4 #define MAXN 25 5 6 __int64 buf[MAXN]; 7 8 __int64 gcd(__int64 a, __int64 b) { 9 if (b == 0) return a; 10 else return

hihocoder [Offer收割]编程练习赛18 C 最美和弦(dp)

题目链接:http://hihocoder.com/problemset/problem/1532 题解:一道基础的dp,设dp[i][j][k][l]表示处理到第几个数,当前是哪个和弦错了几次初始x值是多少.这里还要再辅助一个val[k]表示处理到当前情况只错了k次的最小值是多少因为改变的不止是和弦还有初始值,可以看一下代码理解一下. #include <iostream> #include <cstring> #include <cstdio> #include &

1099:零起点学算法06——再来一题除法算术题

1099: 零起点学算法06--再来一题除法算术题 Time Limit: 1 Sec  Memory Limit: 128 MB   64bit IO Format: %lldSubmitted: 4811  Accepted: 1917[Submit][Status][Web Board] Description 再来一题除法算术题 Input 没有输入 Output 输出8除以5,保留1位小数 Sample Output 1.6 Source 零起点学算法 1 # include <std

hihocoder #1190 : 连通性&#183;四 点双联通分量

http://hihocoder.com/problemset/problem/1190?sid=1051696 先抄袭一下 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho从约翰家回到学校时,网络所的老师又找到了小Hi和小Ho. 老师告诉小Hi和小Ho:之前的分组出了点问题,当服务器(上次是连接)发生宕机的时候,在同一组的服务器有可能连接不上,所以他们希望重新进行一次分组.这一次老师希望对连接进行分组,并把一个组内的所有连接关联的服务器也视为这个组内

Hihocoder 太阁最新面经算法竞赛18

Hihocoder 太阁最新面经算法竞赛18 source: https://hihocoder.com/contest/hihointerview27/problems 题目1 : Big Plus 描述 Given an NxN 01 matrix, find the biggest plus (+) consisting of 1s in the matrix. size 1 plus size 2 plus size 3 plus size 4 plus 1 1 1 1 111 1 1

hihoCoder 1393 网络流三&#183;二分图多重匹配(Dinic求二分图最大多重匹配)

#1393 : 网络流三·二分图多重匹配 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 学校的秋季运动会即将开始,为了决定参赛人员,各个班又开始忙碌起来. 小Hi和小Ho作为班上的班干部,统计分配比赛选手的重任也自然交到了他们手上. 已知小Hi和小Ho所在的班级一共有N名学生(包含小Hi和小Ho),编号依次为1..N. 运动会一共有M项不同的比赛,编号为1..M.第i项比赛每个班需要派出m[i]名选手参加. 根据小Hi和小Ho的统计,编号为i的学生表示最多同时参加

hihoCoder 1430 : A Boring Problem(一琐繁题)

hihoCoder #1430 : A Boring Problem(一琐繁题) 时间限制:1000ms 单点时限:1000ms 内存限制:256MB Description - 题目描述 As a student of the school of electronics engineering and computer science in Peking University, Kyle took the course named Advanced Algebra in his freshma