# 《啊哈！算法》－第 3 章：枚举，很暴力！

_我已经从中二毕业了

## 炸弹人

``````char a[NUM][NUM] = {
{'#','#','#','#','#','#','#','#','#','#','#','#','#'},
{'#','G','G','.','G','G','G','#','G','G','G','.','#'},
{'#','#','#','.','#','G','#','G','#','G','#','G','#'},
{'#','.','.','.','.','.','.','.','#','.','.','G','#'},
{'#','G','#','.','#','#','#','.','#','G','#','G','#'},
{'#','G','G','.','G','G','G','.','#','.','G','G','#'},
{'#','G','#','.','#','G','#','.','#','.','#','.','#'},
{'#','#','G','.','.','.','G','.','.','.','.','.','#'},
{'#','G','#','.','#','G','#','#','#','.','#','G','#'},
{'#','.','.','.','G','#','G','G','G','.','G','G','#'},
{'#','G','#','.','#','G','#','G','#','.','#','G','#'},
{'#','G','G','.','G','G','G','#','G','.','G','G','#'},
{'#','#','#','#','#','#','#','#','#','#','#','#','#'}
};``````

``````#include <stdio.h>
#define NUM 13

char a[NUM][NUM] = {...};

int main(void) {
int i, j, x, y, q, p;
int num = 0;
int max = 0;

for (i = 1; i < NUM - 1; i++) {
for (j = 1; j < NUM - 1; j++) {
num = 0;

if (
a[i][j] == '.' &&
(a[i - 1][j] == '.' ||
a[i][j + 1] == '.' ||
a[i - 1][j] == '.' ||
a[i][j - 1] == '.')
) {
// 杀上边的怪
x = i;
y = j;

while (a[x][y] != '#') {
if (a[x][y] == 'G') {
num += 1;
}
x -= 1;
}

// 杀右边的怪
x = i;
y = j;

while (a[x][y] != '#') {
if (a[x][y] == 'G') {
num += 1;
}

y += 1;
}

// 杀下边的怪
x = i;
y = j;

while (a[x][y] != '#') {
if (a[x][y] == 'G') {
num += 1;
}

x += 1;
}

// 杀左边的怪
x = i;
y = j;

while (a[x][y] != '#') {
if (a[x][y] == 'G') {
num += 1;
}

y -= 1;
}

if (num >= max) {
max = num;
q = i;
p = j;

printf("%d, (%d, %d)\n", num, q, p);
}
}
}
}

printf("(%d, %d)\n", q, p);

return 0;
}``````

## 全排列

``````#include <stdio.h>

void fn(len) {
int a[10], i, j, k, l;
int start = 1;
int end = len + 1;
int flag = 1;

for (i = 1; i <= 10; i++) {
a[i] = 0;
}

for (i = 1; i < len; i++) {
start *= 10;
end *= 10;
}

for (i = start; i < end; i++) {
j = i;
flag = 1;

for (k = 1; k <= 10; k++) {
a[k] = 0;
}

while (j != 0) {
l = j % 10;

if (a[l] == 0 && l <= len) {
a[l] = 1;
j /= 10;
} else {
flag = 0;
break;
}
}

if (flag == 1) {
printf("%d\n", i);
}
}
}

int main(void) {
int n;

scanf("%d", &n);

fn(n);

return 0;
}``````

7.9k 声望
235 粉丝
0 条评论