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 }