具体题目后边补充(网易雷火)
#include<iostream>
#include<vector>
using namespace std;
int defaultId = 1;
typedef struct Win_property
{
int X_pos;
int Y_pos;
int wide;
int hight;
}Win_property;
typedef struct Window
{
int w_id;
Win_property property;
struct Window* w_next;
}Win, *pWin;
pWin BuyNode()
{
pWin res = (pWin)malloc(sizeof(Win));
if (NULL == res)
{
exit(1);
}
res->w_id = 0;
res->w_next = NULL;
res->property.X_pos = 0;
res->property.Y_pos = 0;
res->property.wide = 0;
res->property.hight = 0;
return res;
}
void Init(pWin *first)
{
*first = BuyNode();
}
void Insert(pWin pNode, int x, int y, int w, int h) //头插
{
pWin s = BuyNode();
s->w_next = pNode->w_next;
pNode->w_next = s;
s->w_id = defaultId;
defaultId++;
s->property.X_pos = x;
s->property.Y_pos = y;
s->property.wide = w;
s->property.hight = h;
}
int GetWindow(pWin qNode, int posx, int posy)
{
while (qNode != NULL)
{
if (posx >= qNode->property.X_pos && posx <= qNode->property.X_pos + qNode->property.wide
&& posy >= qNode->property.Y_pos && posy <= qNode->property.Y_pos + qNode->property.hight)
{
return qNode->w_id;
}
qNode = qNode->w_next;
}
return -1;
}
int GetWindowId(pWin pNode, int posx, int posy)
{
pWin tmp = pNode;
if (pNode == NULL && tmp->w_next == NULL)
{
return -1;
}
int index = GetWindow(tmp, posx, posy);
if (pNode->w_next->w_id != index) //返回的窗口ID不是眼前的第一个,则进行调整
{
while (tmp->w_next->w_id != index && tmp->w_next->w_next != NULL)
{
tmp = tmp->w_next;
}
if (tmp->w_next->w_id == index)
{
pWin s = tmp->w_next;
tmp->w_next = s->w_next;
s->w_next = pNode->w_next;
pNode->w_next = s;
}
}
return index;
}
/*
2 4
100 100 100 100
10 10 150 150
105 105
180 180
105 105
1 1
*/
int main()
{
int x, y; //左上角的坐标
int w, h; //窗口的长和宽
int n; //窗口数
int m; //鼠标点击事件个数
int getx, gety; //鼠标点击事件的坐标位置
pWin firstview = NULL; //头指针
Init(&firstview);
cin>>n>>m;
vector<int> re(m, 0);
while (n > 0)
{
cin >> x >> y >> w >> h;
Insert(firstview, x, y, w, h);
--n;
}
for (int i = 0; i < m; ++i)
{
cin >> getx >> gety;
re[i] = GetWindowId(firstview, getx, gety);
}
for (int i = 0; i < m; i++)
{
cout << re[i]<< endl;
}
return 0;
}
需要有一个现成的数据结构来做支撑(我是自己定义的结构和方法),如果能够找到合适的数据结构,则该题极其简单。该数据结构应该符合的要求如下:
-
需要一个能够保存key-value的数据结构,该结构最好脱离传统下标的束缚,因而数组之类的结构不可取
-
考虑到窗口调整的效率,点击窗口移动到当前视角的时间复杂度要低,同样,避免使用数组,链表是一种不错的选择