題目
小M最近迷上了一款叫做《俄羅斯方塊》的遊戲。與傳統的俄羅斯方塊不同,這個遊戲是在一個由六邊形格子組成的棋盤上進行消除,棋盤的形狀如下圖所示。
玩家需要將遊戲提供的方塊放置在棋盤上,方塊是由互相連接的六邊形格子組成的,例如下圖便展示了部分方塊的形狀。
遊戲開始時棋盤爲空,每個回合遊戲提供一個方塊,玩家將其放在一個合法位置(方塊不能旋轉)。合法是指這個方塊可以放在棋盤上,組成其的每一個六邊形格子不超出棋盤,且棋盤上對應的位置爲空。放置後,棋盤上對應的格子被佔據。每當一行或一列(列有2個方向)全部被佔據時,這一行或這一列便被消除,同時玩家得分,得分爲消除的格子數量。需要注意的是存在當一個方塊放置後,同時使得多個行列被消除的情況。
下圖展示了一個得分的例子,將方塊放在紅框位置,則同時有兩行和一列被消除,得18分。
爲了便於描述,我們將棋盤格子的座標記爲下圖所示:
有了上述座標系,我們便可以將一個包含m個格子的方塊用一個整數m和m對座標來進行描述。假設其中一個格子的座標爲(x,y),那麼其他格子均是相對這個格子的偏移量。如果我們給這m個座標同時加上一個相同的偏移量(dx,dy),那麼它們依然表示的是同一個方塊。
例如下圖的方塊:
便可以使用 4 0 0 1 0 -1 -1 0 -1 或者 4 0 -3 1 -3 -1 -4 0 -4 來描述。
現在問題來了,棋盤上當前已經有若干方塊(即已經有部分格子被佔據),再額外給你一個方塊,最多能夠消除多少格子呢?
注意:本題只允許C++、Java語言提交,其他語言提交得分無效
輸入描述:
每個輸入數據包含多個測試點。
第一行爲測試點的個數T(T<=100)。
每個測試點的第一行爲M(0<=M<=61),表示棋盤上已經有多少個格子被佔據。
接下來M行,每行是兩個整數x y,表示被佔據格子的座標。
下一行是一個方塊描述。
輸入數據保證座標合法,方塊是合法方塊,且初始局面不存在能夠消除的情況。每個方塊的格子數不超過10。
輸出描述:
對於每個測試點,輸出一行,表示通過將輸入方塊放到棋盤上,最多能夠消除多少格子。如果方塊無法放置在棋盤上,輸出-1。
示例1
輸入
1
29
0 4
0 3
1 3
0 2
1 2
0 1
1 1
-2 0
0 0
1 0
2 0
4 0
-3 -1
-2 -1
-1 -1
0 -1
1 -1
-4 -2
-3 -2
-2 -2
-1 -2
0 -2
-4 -3
-3 -3
-2 -3
-1 -3
-4 -4
-3 -4
-2 -4
4 0 0 1 0 -1 -1 0 -1
輸出
18
思路
(1)六邊形的方程表達式,
x - 4 <= y <= x + 4, -4 <= x <= 4, -4 <= y <= 4
爲了將座標映射成非負整數,所有點(x, y)加上偏移OFFSET = 4;
用9*9的數組A表示該棋盤,其中-1表示無效(在六邊形以外的座標),0表示空,1表示已放置方塊;
(2)遍歷棋盤數組A,座標爲(x, y),包含m個格子的方塊爲K,方塊第i個方塊座標爲(kix, kiy),在棋盤A中找到令A(x + kix, x + kiy), 0<=i < m均爲0的位置(x0, y0),表示方塊K可以放置在該位置;
(3)將A中放置方塊K的座標值設爲1;
(4)分行列遍歷數組A,如果當前行或列(不包含無效的座標,值爲-1)以填滿,則將該行或列的座標放入std::set中(去重),記錄可以消除的方塊數NUM;
(5)將(3)中置爲1的座標值還原爲0;
(6)重複(2)~(6)找到最大的NUM,即爲所求;
代碼
代碼已通過樣例,不知道是否可以AC,僅供參考,若有問題,還望指出~
#include <algorithm>
#include <iostream>
#include <vector>
#include <set>
const int OFFSET = 4; //座標偏移,將座標全部映射至0~8之間
bool isValid(int x, int y){
if (x < 0 || x > 8 || y < 0 || y > 8){
return false;
}
if (y > x + 4 || y < x - 4){
return false;
}
return true;
}
inline bool isEmpty(const std::vector<std::vector<int>>& map, int x, int y){
return map[x][y] == 0;
}
struct Point{
Point(int x, int y) : x(x), y(y){}
//爲set做排序用, 不可缺少
bool operator<(const Point& point) const {
if (x != point.x){
return x < point.x;
}
return y < point.y;
}
int x, y;
};
int canVanishNum(const std::vector<std::vector<int>>& map){
std::set<Point> result;
//行
for (int i = 0; i <= 8; ++i){
bool isAllOccupied = true;
for (int k = 0; k <= 8; ++k){
if (map[i][k] == -1){
continue;
}
if (map[i][k] == 0){
isAllOccupied = false;
break;
}
}
if (isAllOccupied == false){
continue;
}
for (int k = 0; k <= 8; ++k){
if (map[i][k] == -1){
continue;
}
result.insert(Point(i, k));
}
}
//列
for (int i = 0; i <= 8; ++i){
bool isAllOccupied = true;
for (int k = 0; k <= 8; ++k){
if (map[k][i] == -1){
continue;
}
if (map[k][i] == 0){
isAllOccupied = false;
break;
}
}
if (isAllOccupied == false){
continue;
}
for (int k = 0; k <= 8; ++k){
if (map[k][i] == -1){
continue;
}
result.insert(Point(k, i));
}
}
return result.size();
}
void setMapPointNum(std::vector<std::vector<int>>& map, const std::vector<Point>& coords,
int x, int y, int num){
for (int i = 0; i < coords.size(); ++i){
int newX = x + coords[i].x;
int newY = y + coords[i].y;
map[newX][newY] = num;
}
}
int calcMax(std::vector<std::vector<int>>& map, std::vector<Point>& coords){
int rows = map.size();
int cols = map.front().size();
int x = coords.front().x;
int y = coords.front().y;
int result = -1;
//如果無法放置,則會返回-1
for (int i = 0; i < rows; ++i){
for (int k = 0; k < cols; ++k){
int newX = i + x;
int newY = k + y;
if (isValid(newX, newY) && isEmpty(map, newX, newY)){
bool mark = true;
for (int j = 1; j < coords.size(); ++j){
int tmpX = i + coords[j].x;
int tmpY = k + coords[j].y;
if (!(isValid(tmpX, tmpY) && isEmpty(map, tmpX, tmpY))){
mark = false;
break;
}
}
if (mark == false){
continue;
}
setMapPointNum(map, coords, i, k, 1);
result = std::max(result, canVanishNum(map));
setMapPointNum(map, coords, i, k, 0);
}
}
}
return result;
}
int main()
{
//-1表示不可用點,0表示空點,1表示已佔用點
std::vector<std::vector<int>> map(9, std::vector<int>(9, 0));
for (int i = 0; i <= 8; ++i){
for (int k = 0; k <= 8; ++k){
if (isValid(i, k) == false){
map[i][k] = -1;
}
}
}
//輸入數據
int loopTime = 0;
std::cin >> loopTime;
while (loopTime--){
int pointNum = 0;
std::cin >> pointNum;
int x = 0, y = 0;
for (int i = 0; i < pointNum; ++i){
std::cin >> x >> y;
x += OFFSET;
y += OFFSET;
map[x][y] = 1;
}
std::vector<Point> coords;
int coordNum = 0;
std::cin >> coordNum;
for (int i = 0; i < coordNum; ++i){
std::cin >> x >> y;
coords.push_back(Point(x, y));
}
std::cout << calcMax(map, coords) << std::endl;
}
system("pause");
return 0;
}