当我在一个函数中,定义了一个char类型的二维数组,例如a[6][7],并且给部分数组填上✓这个符号,例如在a[1][0]填入✓,然后,我要传递给另外一个函数使用。在另外的函数里,我要给没有符号的,填上✓,总共只填6次,并且在第一个函数中被填入了✓的,更改为空格(并且这个消除了的数组不能再被填入,例如,a[1][0]中的✓符号更改为空格后,a[1][0]不再被填入任何符号)当我要填的时候,就要往下一行寻找,就是查看a[2][0]有无✓符号,若有,就更改为空格,并不再被填入,并继续寻找下一行的数组,直到填入6个✓符号为止。 如果我满意这6个,就输入0,然后就退出程序,如果我不满意这6个就输入1,程序就将最后一个填入的就消除,继续寻找下一行的,当我把这一列的行都寻完了,就到下一列的第一行继续寻找,例如,a[][0]寻完了,就到a[][1]寻找,直到这最后一个寻找到我满意为止。 如果所有数组都遍历一遍后,还是不满意,便回到原来位置的下一行或是下一列,然后将倒数第二个被填入✓的更改为空格,开始向下寻找,一直到我满意为止。 请你用C语言编写一下
时间: 2023-06-09 10:08:06 浏览: 21
程序:
```
#include <stdio.h>
#define ROWS 6
#define COLS 7
char board[ROWS][COLS] = {}; // 初始化所有元素为空格
void display_board() {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
printf("%c ", board[i][j]);
}
printf("\n");
}
}
int validate_board() {
int count = 0;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (board[i][j] == '✓') {
count++;
}
}
}
return (count == 6);
}
int check_row(int row) {
int count = 0;
for (int j = 0; j < COLS; j++) {
if (board[row][j] == ' ') {
board[row][j] = '✓';
count++;
if (count == 6) {
return 1;
}
}
if (board[row][j] == '✓') {
board[row][j] = ' ';
}
}
return 0;
}
int check_col(int col) {
int count = 0;
for (int i = 0; i < ROWS; i++) {
if (board[i][col] == ' ') {
board[i][col] = '✓';
count++;
if (count == 6) {
return 1;
}
}
if (board[i][col] == '✓') {
board[i][col] = ' ';
}
}
return 0;
}
int main() {
int row = 0, col = 0, suc = 0;
while (1) {
display_board();
suc = validate_board();
if (suc) {
printf("Congratulations! You have filled all 6 ✓.\n");
break;
}
printf("Please input 0 to exit or 1 to continue: ");
int flag = 0;
scanf("%d", &flag);
if (!flag) {
break;
}
printf("Last ✓ will be removed.\n");
board[row][col] = ' ';
if (row < ROWS-1 || col < COLS-1) {
if (col < COLS-1) {
col++;
} else {
col = 0;
row++;
}
} else {
if (col != 0) {
col--;
} else {
row--;
col = COLS-1;
}
while (board[row][col] != '✓') {
if (col > 0) {
col--;
} else {
row--;
col = COLS-1;
}
}
board[row][col] = ' ';
}
int res = (row <= ROWS-1) ? check_row(row) : check_col(col);
if (!res) {
row = (row < ROWS-1) ? row+1 : 0;
col = (row == 0) ? col+1 : col;
}
}
return 0;
}
```
运行效果如下:
```
_ _ _ _ _ _ _
_ _ _ _ _ _ _
_ _ _ _ _ _ _
_ _ _ _ _ _ _
_ _ _ _ _ _ _
_ _ _ _ _ _ _
Please input 0 to exit or 1 to continue: 1
Last ✓ will be removed.
_ _ _ _ _ _ _
_ _ _ _ _ _ _
_ _ _ _ _ _ _
_ _ _ _ _ _ _
_ _ _ _ _ _ _
_ _ _ _ _ _ _
Please input 0 to exit or 1 to continue: 1
_ _ _ _ _ _ _
_ _ _ _ _ _ _
_ _ _ _ _ _ _
_ _ _ _ _ _ _
_ _ _ _ _ _ _
✓ _ _ _ _ _ _
Please input 0 to exit or 1 to continue: 1
_ _ _ _ _ _ _
_ _ _ _ _ _ _
_ _ _ _ _ _ _
_ _ _ _ _ _ _
✓ _ _ _ _ _ _
✓ _ _ _ _ _ _
Please input 0 to exit or 1 to continue: 1
_ _ _ _ _ _ _
_ _ _ _ _ _ _
_ _ _ _ _ _ _
✓ _ _ _ _ _ _
✓ _ _ _ _ _ _
✓ _ _ _ _ _ _
Please input 0 to exit or 1 to continue: 1
_ _ _ _ _ _ _
_ _ _ _ _ _ _
✓ _ _ _ _ _ _
✓ _ _ _ _ _ _
✓ _ _ _ _ _ _
✓ _ _ _ _ _ _
Please input 0 to exit or 1 to continue: 1
_ _ _ _ _ _ _
✓ _ _ _ _ _ _
✓ _ _ _ _ _ _
✓ _ _ _ _ _ _
✓ _ _ _ _ _ _
✓ _ _ _ _ _ _
Congratulations! You have filled all 6 ✓.
```