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 }

 

转载于:https://www.cnblogs.com/liew/p/4398803.html

乐播投屏是一款简单好用、功能强大的专业投屏软件,支持手机投屏电视、手机投电脑、电脑投电视等多种投屏方式。 多端兼容与跨网投屏:支持手机、平板、电脑等多种设备之间的自由组合投屏,且无需连接 WiFi,通过跨屏技术打破网络限制,扫一扫即可投屏。 广泛的应用支持:支持 10000+APP 投屏,包括综合视频、网盘与浏览器、美韩剧、斗鱼、虎牙等直播平台,还能将央视、湖南卫视等各大卫视的直播内容一键投屏。 高清流畅投屏体验:腾讯独家智能音画调校技术,支持 4K 高清画质、240Hz 超高帧率,低延迟不卡顿,能为用户提供更高清、流畅的视觉享受。 会议办公功能强大:拥有全球唯一的 “超级投屏空间”,扫码即投,无需安装。支持多人共享投屏、远程协作批注,PPT、Excel、视频等文件都能流畅展示,还具备企业级安全加密,保障会议资料不泄露。 多人互动功能:支持多人投屏,邀请好友加入投屏互动,远程也可加入。同时具备一屏多显、语音互动功能,支持多人连麦,实时语音交流。 文件支持全面:支持 PPT、PDF、Word、Excel 等办公文件,以及视频、图片等多种类型文件的投屏,还支持网盘直投,无需下载和转格式。 特色功能丰富:投屏时可同步录制投屏画面,部分版本还支持通过触控屏或电视端外接鼠标反控电脑,以及在投屏过程中用画笔实时标注等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值