QQ游戏连连看
总提交:98 测试通过:42
描述
众所周知,lfq198502非常喜欢玩连连看游戏。连连看游戏 ,只要将相同花色的两张牌用三根以内的直线连在一起就可以消除, 规则简单容易上手 。
操作:第一次使用鼠标点击棋盘中的棋子,该棋子此时为“被选中”,以特殊方式显示;再次以鼠标点击其他棋子,若该棋子与被选中的棋子图案相同,且把第一个棋子到第二个棋子连起来,中间的直线不超过 3 根,则消掉这一对棋子,否则第一颗棋子恢复成未被选中状态,而第二颗棋子变成被选中状态。
我们可以把连连看的界面想象成一个10×10的方格,每个方格内都放有一种类型的图案或者为空,简单起见,我们用不同的字符代表不同的图案,空格表示没有图案。你的任务是,给出游戏的初始界面,和游戏过程中lfq198502鼠标点击的坐标记录,判断他在这些操作后是否将图案全部消完。
输入
输入包含多组测试情况,每组情况数据包含两部分。
第一部分:10行字符串,每行包含10个字符(空格表示该方格为空,其他字符表示方格中的有用该字符表示的图案),表示游戏的初始界面。
第二部分:首先是一个整数n,表示lfq198502的鼠标点击次数;接下来的n行,每行两个整数x,y(1≤x≤10, 1≤y≤10),表示鼠标点击位置所在的行和列。
输出
每组测试情况输出一行。如果lfq198502能够将图案全部消除,输出“Yes,all patterns are eliminated!”;否则,输出“No,m pattern(s) are left!”。
样例输入
abccba x
bax
ab 8
8
@ (
( @
18
1 3
1 4
1 2
1 5
1 1
1 6
1 10
2 3
2 2
3 2
3 3
2 1
3 7
4 10
8 1
10 7
10 3
8 10
ab 8
ba
8
@
@
8
1 1
2 2
1 2
2 1
1 10
4 10
8 1
10 7
样例输出
No,4 pattern(s) are left!
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
#include<algorithm>
#include<cstdlib>
template<typename Type>
class Coordinate_2D{
public:
Coordinate_2D(void){}
Coordinate_2D(const Type& _x, const Type& _y):x(_x), y(_y){}
void set(const Type& _x, const Type& _y){ x = _x; y = _y; }
Coordinate_2D add(const Coordinate_2D& b)const { return Coordinate_2D(x + b.x, y + b.y); }
Coordinate_2D subtract(const Coordinate_2D& b)const { return Coordinate_2D(x - b.x, y - b.y); }
Coordinate_2D clockwiseRotation90Angle(const Coordinate_2D& b)const { return Coordinate_2D(y - b.y, b.x - x); }
Coordinate_2D turnHorizontal(const Type& _y)const{ return Coordinate_2D(x, 2 * _y - y); }
Coordinate_2D turnVertical(const Type& _x)const{ return Coordinate_2D(2 * _x - x, y); }
double distanceTo(const Coordinate_2D& b)const { return sqrt(pow(x - b.x, 2) + pow(y - b.y, 2)); }
void print(void)const { cout << "(" << x << "," << y << ")"; }
bool operator ==(const Coordinate_2D& b)const { return x == b.x && y == b.y; }
bool operator !=(const Coordinate_2D& b)const { return x != b.x || y != b.y; }
public:
Type x;
Type y;
};
const size_t MAX_SIZE_X = 10; //地图大小
const size_t MAX_SIZE_Y = 10; //地图大小
const short int MAX_DIRECTION = 4; //遍历方向个数
const short int DIRECTION_X[MAX_DIRECTION] = { 0, 0, 1, -1 };
const short int DIRECTION_Y[MAX_DIRECTION] = { -1, 1, 0, 0 };
char map[MAX_SIZE_X][MAX_SIZE_Y];
size_t size_x = MAX_SIZE_X, size_y = MAX_SIZE_Y;
bool success;
Coordinate_2D<size_t> aim;
void dfs(const size_t x, const size_t y, unsigned int turnTimes, short int turnCode){
if (success) return;
if (turnTimes > 3) return;
if (x == aim.x && y == aim.y){ success = true; return; }
size_t current_x, current_y;
for (short int direction(0); direction != MAX_DIRECTION; ++direction){
current_x = x + DIRECTION_X[direction];
current_y = y + DIRECTION_Y[direction];
if (current_x < size_x && current_y < size_y && map[current_x][current_y] == ' '){
map[current_x][current_y] = '!';
dfs(current_x, current_y, (turnCode == direction ? turnTimes : turnTimes + 1), direction);
map[current_x][current_y] = ' ';
}
}
}
bool removeSuccessfully(const Coordinate_2D<size_t>& start, const Coordinate_2D<size_t>& end){
aim.set(end.x - 1, end.y - 1);
success = false;
map[aim.x][aim.y] = ' ';
dfs(start.x - 1, start.y - 1, 0, -1);
if (success){
map[start.x - 1][start.y - 1] = ' ';
}
else{
map[aim.x][aim.y] = map[start.x - 1][start.y - 1];
}
return success;
}
void startGame(void){
bool selected = false;
Coordinate_2D<size_t> lastArea;
Coordinate_2D<size_t> currentArea;
unsigned int operationTimes;
cin >> operationTimes;
while (operationTimes--){
cin >> currentArea.x >> currentArea.y;
if (selected){
if (currentArea != lastArea){
if (map[currentArea.x - 1][currentArea.y - 1] == map[lastArea.x - 1][lastArea.y - 1]
&& removeSuccessfully(lastArea, currentArea)){
selected = false;
}
else{
lastArea = currentArea;
}
}
}
else{
lastArea = currentArea;
selected = true;
}
}
}
void printResult(void){
unsigned int patterns = 0;
for (size_t i = 0; i != size_x; ++i){
for (size_t j = 0; j != size_y; ++j){
if (map[i][j] != ' '){ ++patterns; }
}
}
printf(patterns == 0 ? "Yes,all patterns are eliminated!\n" : "No,%u pattern(s) are left!\n", patterns);
}
void initMap(void){
for (size_t i = 0; i != size_x; ++i){
getchar();
for (size_t j = 0; j != size_y; ++j){
map[i][j] = getchar();
}
}
}
int main(void){
unsigned int playTimes;
cin >> playTimes;
while (playTimes--){
initMap();
startGame();
printResult();
}
return EXIT_SUCCESS;
}