函数calc计算围棋中位置(x,y)处连成一片的棋子个数。所谓连成一片,即沿着棋盘横竖线往任意方向遍历,遍历过程允许转弯,不允许走斜线,中间未出现对方棋子或空子。
enum color {
NONE, WHITE, BLACK, // 棋子颜色,NONE表示未落子
};
struct weiqi {
enum color board[19][19]; // 棋盘上每个位置的落子
};
int calc(struct weiqi *wq, int x, int y){}
第1-19行数据是棋盘上棋子的颜色数据。0表示未落子,1表示白子,2表示黑子。 第1行最左边位置的坐标是(0,0),第1行第2列的坐标是(1,0),第2行第1列的坐标是(0,1),依此类推。 第20行数据是起始坐标(x,y)
与坐标(X,Y)连成一片的棋子数目
0000000000000000000 0000011000000000000 0000001111000000000 0000001021000000000 0000001010100000000 0000000000000000000 0000000000000000000 0000000000000000000 0000000000000000000 0000000000000000000 0000000000000000000 0000000000000000000 0000000000000000000 0000000000000000000 0000000000000000000 0000000000000000000 0000000000000000000 0000000000000000000 0000000000000000000 5,1
9
#include
#include
enum color {
NONE, WHITE, BLACK, //棋子颜色,NONE表示未落子
};
struct weiqi {
enum color board[19][19]; //棋盘上每个位置的落子
};
int calc(struct weiqi* wq, int x, int y)
{
static int dir[][2] = { {1,0},{0,1},{0,-1},{-1,0} };
static color current = wq->board[y][x];
if (x >= 19
|| x < 0
|| y >= 19
|| y < 0
|| wq->board[y][x] != current
|| wq->board[y][x] == NONE
)
return 0;
int counter = 1;
wq->board[y][x] = NONE;
for (int i = 0; i< 4; i++)
counter+=calc(wq, x + dir[i][0], y + dir[i][1]);
return counter;
}
int input(struct weiqi* wq, int* x, int* y)
{
int row, col;
int ret;
char buf[80];
for (row = 0; row < 19; ++row) {
if (fgets(buf, sizeof(buf), stdin) == NULL)
return -1;
if (strlen(buf) < 19)
return -1;
for (col = 0; col < 19; ++col) {
switch (buf[col]) {
case '0':
wq->board[row][col] = NONE;
break;
case '1':
wq->board[row][col] = WHITE;
break;
case '2':
wq->board[row][col] = BLACK;
break;
default:
return -1;
}
}
}
ret = scanf( "%d,%d", x, y);
if (ret != 2)
return -1;
for (row = 0; row < 19; ++row) {
for (col = 0; col < 19; ++col) {
fprintf(stderr, "%d ", wq->board[row][col]);
}
fprintf(stderr, "\n");
}
fprintf(stderr, "x = %d, y = %d\n", *x, *y);
return 0;
}
int main()
{
struct weiqi wq;
int x = 0, y = 0;
int cnt;
memset(&wq, 0, sizeof(wq));
if (input(&wq, &x, &y) < 0) {
fprintf(stderr, "error!\n");
return 1;
}
cnt = calc(&wq, x, y);
printf("%d\n", cnt);
return 0;
}
请实现list_sort,使用冒泡法将head指向的链表按val值大小排成升序
struct node {
int val;
struct node *next;
};
void list_sort(struct node *head)
{
}
static void list_sort(struct node* head)
{
for (node* i = head; i; i=i->next) {
for (node* j = i->next; j; j = j->next) {
if (i->val > j->val)
std::swap(i->val, j->val);
}
}
}
|
字符串,如:abc
可能的出栈顺序,每行一种顺序
abc
abc acb bac bca cba
#include
#include
#include
#include
using namespace std;
int main() {
string str;
cin >> str;
vector> result; //(栈中字符,出栈字符)
result.push_back({ string(),string() });
for (const char& ch : str) {
int m_size = result.size();
for (int i = 0; i < m_size; i++) {
result[i].first.push_back(ch);
pair temp = result[i];
for (int j = 0; j < result[i].first.size(); j++) {
temp.second.push_back(temp.first.back());
temp.first.pop_back();
result.push_back(temp);
}
}
}
set output;
for (auto& stk : result)
output.insert(stk.second + string(stk.first.rbegin(), stk.first.rend()));
for (const string& str : output)
cout << str << endl;
return 0;
}