有一个n行m列的棋盘,初始化全部为白色,根据输入,控制每个格子的颜色或者搜索展示第一个遇到的白色单元格,如果没找到,则输出-1;如果找到,则输出找到的白色单元格坐标x,y。
注意:查找的单元格不包含自身,且单元格左上角从(1,1)开始
控制动作为:
c 将单元格改为黑色
l 从当前单元格向左搜索
r 从当前单元格向右搜索
u 从当前单元格向上搜索
d 从当前单元格向下搜索
例子:
5 5 20
l 3 5
r 4 3
u 4 5
d 2 4
int boardControl() {
int n, m, k;
cin >> n;
cin >> m;
cin >> k;
vector<vector<char>> board = vector(n + 1, vector<char>(m + 1, 'W'));
char ch;
int x, y;
for (int i = 0; i < k; i++) {
cin >> ch;
cin >> x;
cin >> y;
if (x < 1 || x > n || y < 1 || y > m) {
cout << -1 << endl;
continue;
}
int flag = false;
if (ch == 'c') {
board[x][y] = 'B';//涂黑
}
else if (ch == 'l') {
for (int j = y - 1; j > 0; j--) {
if (board[x][j] == 'W') {
cout << x << " " << j << endl;
flag = true;
break;
}
}
if (flag == false) {
cout << -1 << endl;
}
}
else if (ch == 'r') {
for (int j = y + 1; j < m + 1; j++) {
if (board[x][j] == 'W') {
cout << x << " " << j << endl;
flag = true;
break;
}
}
if (flag == false) {
cout << -1 << endl;
}
}
else if (ch == 'u') {
for (int j = x - 1; j > 0; j--) {
if (board[j][y] == 'W') {
cout << j << " " << y << endl;
flag = true;
break;
}
}
if (flag == false) {
cout << -1 << endl;
}
}
else if (ch == 'd') {
for (int j = x + 1; j < n + 1; j++) {
if (board[j][y] == 'W') {
cout << j << " " << y << endl;
flag = true;
break;
}
}
if (flag == false) {
cout << -1 << endl;
}
}
else {
cout << "非法输入" << endl;
}
}
return 0;
}