题目1 : Constellations
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
Recently Little Hi started to like astronomy and downloaded thepictures of K constellations. He wonders how many of them he can spot in thenight?
输入
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 widthof the picture.
Line 2~H+1 of each picture: An H*W matrix of characters representing thepicture 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 LittleHi looks to.
Line 2~N of sky: An N*M matrix of characters representing the sky Little Hilooks to. Each line contains M characters. '#' for a star and '.' for emptyarea. There are no more than 5000 stars in the sky.
输出
For each constellation in the Input output "Yes" or"No" indicating whether Little Hi can spot the constellation in thesky.
All pictures of constellations and the sky Little Hi looks to are in the samedirection so there is no need to rotate the pictures.
提示
A constellation can be spoted if and only if all stars in theconstellation can be matched in the sky. It is allowed that two spotedconstellations share common stars.
样例输入
3
5 5
#....
.....
...#.
.....
.#...
5 5
....#
.....
.....
#....
....#
5 6
.....#
......
#.....
......
....#.
10 10
.......#..
..........
..#.......
..........
......#...
..........
..........
..#.......
......#...
..........
样例输出
No
Yes
Yes
本人菜鸟一枚,水平有限,花了3个小时写的代码,当做是练习,长时间不写代码会生疏。
以下我的C++代码实现,其中难免有不合理的地方,希望大家提出意见。
文件1:constellations.h
#ifndef _CONSTELLATIONS_H_
#define _CONSTELLATIONS_H_
class STAR_MAP_CL
{
public:
STAR_MAP_CL();
STAR_MAP_CL(int _iH, int _iW);
~STAR_MAP_CL();
void InputData();
void Initial(int _iH, int _iW);
int iW;
int iH;
char *pData;
char **ppPtr;
};
class CONSTELLATION_CL : public STAR_MAP_CL
{
public:
CONSTELLATION_CL();
CONSTELLATION_CL(int _iH, int _iW);
~CONSTELLATION_CL();
};
class SKY_CL : public STAR_MAP_CL
{
public:
SKY_CL();
SKY_CL(int _iH, int _iW);
~SKY_CL();
bool IsFind(CONSTELLATION_CL clConstellation);
};
#endif
文件2:constellations_main.cpp
#include <iostream>
#include "constellations.h"
using namespace std;
STAR_MAP_CL::STAR_MAP_CL():iH(0),iW(0),pData(NULL),ppPtr(NULL)
{
return;
}
STAR_MAP_CL::STAR_MAP_CL(int _iH, int _iW)
{
Initial(_iH,_iW);
return;
}
STAR_MAP_CL::~STAR_MAP_CL()
{
int i;
if (NULL != pData)
{
delete pData;
}
if (NULL != ppPtr)
{
delete ppPtr;
}
return;
}
void STAR_MAP_CL::Initial(int _iH, int _iW)
{
int i;
iH = _iH;
iW = _iW;
pData = new char[iH*iW];
ppPtr = new char*[iH];
for (i=0; i<iH; i++)
{
ppPtr[i]=pData+i*iW;
}
return;
}
void STAR_MAP_CL::InputData()
{
int i;
for (i=0; i<iH; i++)
{
cin>>ppPtr[i];
}
return;
}
CONSTELLATION_CL::CONSTELLATION_CL()
{
}
CONSTELLATION_CL::CONSTELLATION_CL(int _iH, int _iW):STAR_MAP_CL(_iH,_iW)
{
}
CONSTELLATION_CL::~CONSTELLATION_CL()
{
}
SKY_CL::SKY_CL()
{
}
SKY_CL::SKY_CL(int _iH, int _iW):STAR_MAP_CL(_iH,_iW)
{
}
SKY_CL::~SKY_CL()
{
}
bool SKY_CL::IsFind(CONSTELLATION_CL clConstellation)
{
int i,j,k,m,n;
bool bFind;
char** ppSub;
if (iH<clConstellation.iH || iW<clConstellation.iW)
{
return false;
}
bFind = true;
ppSub = new char*[clConstellation.iH];
for (i=0; i<=iH-clConstellation.iH; i++)
{
for (j=0; j<=iW-clConstellation.iW; j++)
{
for (k=0; k<clConstellation.iH; k++)
{
ppSub[k]=ppPtr[i+k]+j;
}
for (n=0; n<clConstellation.iH; n++)
{
for (m=0; m<clConstellation.iW; m++)
{
bFind &= (clConstellation.ppPtr[n][m] == ppSub[n][m]);
}
}
if (true == bFind)
{
delete []ppSub;
return true;
}
else
{
bFind = true;
}
}
}
delete []ppSub;
return false;
}
int main()
{
int iN;
int _iH,_iW;
int i;
cin>>iN;
CONSTELLATION_CL *pclConstellations;
pclConstellations = new CONSTELLATION_CL[iN];
for (i=0; i<iN; i++)
{
cin>>_iH>>_iW;
pclConstellations[i].Initial(_iW,_iH);
pclConstellations[i].InputData();
}
cin>>_iH>>_iW;
SKY_CL clSky(_iH,_iW);
clSky.InputData();
for (i=0; i<iN; i++)
{
if (true == clSky.IsFind(pclConstellations[i]))
{
cout<<"yes\n";
}
else
{
cout<<"no\n";
}
}
return 0;
}
运行结果:
hc@ubuntu:~/Workspace/ms_proj/src$ ./run_constellation
2
2 2
*#
#*
3 3
**#
#**
*#*
5 5
#****
****#
**#**
*#***
***#*
yes
no