## 经典回溯算法问题：九宫格

```7 6 1 9 3 4 8 2 5
3 5 4 6 2 8 1 9 7
9 2 8 1 5 7 6 3 4
2 1 9 5 4 6 3 7 8
4 8 3 2 7 9 5 1 6
5 7 6 3 8 1 9 4 2
1 9 5 7 6 2 4 8 3
8 3 2 4 9 5 7 6 1
6 4 7 8 1 3 2 5 9
```

### 【输入样例】

```0 6 1 0 3 0 0 2 0
0 5 0 0 0 8 1 0 7
0 0 0 0 0 7 0 3 4
0 0 9 0 0 6 0 7 8
0 0 3 2 0 9 5 0 0
5 7 0 3 0 0 9 0 0
1 9 0 7 0 0 0 0 0
8 0 2 4 0 0 0 6 0
0 4 0 0 1 0 2 5 0
```

### 【输出样例】

```7 6 1 9 3 4 8 2 5
3 5 4 6 2 8 1 9 7
9 2 8 1 5 7 6 3 4
2 1 9 5 4 6 3 7 8
4 8 3 2 7 9 5 1 6
5 7 6 3 8 1 9 4 2
1 9 5 7 6 2 4 8 3
8 3 2 4 9 5 7 6 1
6 4 7 8 1 3 2 5 9
```

```#include<stdio.h>

bool canFill(int a[9][9], int k, int n){
int i, j, row = k / 9, col = k % 9;
for(i = 0; i < 9; ++i){
if(a[i][col] == n){
return false;
}
}
for(j = 0; j < 9; ++j){
if(a[row][j] == n){
return false;
}
}
for(i = row - row % 3; i < row - row % 3 + 3; ++i){
for(j = col - col % 3; j < col - col % 3 + 3; ++j){
if(a[i][j] == n){
return false;
}
}
}
return true;
}

void fill(int a[9][9], int k = 0){
int i, j;
if(k == 81){
for(i = 0; i < 9; ++i){
for(j = 0; j < 9; ++j){
printf("%d ", a[i][j]);
}
printf("\n");
}
return;
}
int row = k / 9, col = k % 9;
if(a[row][col] == 0){
for(i = 1; i <= 9; ++i){
if(canFill(a, k, i)){
a[row][col] = i;
fill(a, k + 1);
a[row][col] = 0;
}
}
}else{
fill(a, k + 1);
}
}

int main(){
int i, j, a[9][9];
for(i = 0; i < 9; ++i){
for(j = 0; j < 9; ++j){
scanf("%d", &a[i][j]);
}
}
fill(a);
return 0;
}
```

2013年5月4日3点温习重写代码如下：

```#include<stdio.h>

int square[9][9];

int isEnabled(int k, int num){
int row = k / 9;
int col = k % 9;
for(int i = 0; i < 9; ++i){
if(square[row][i] == num || square[i][col] == num){
return false;
}
}
int m = row - row % 3;
int n = col - col % 3;
for(int i = m; i < m + 3; ++i){
for(int j = n; j < n + 3; ++j){
if(square[i][j] == num){
return false;
}
}
}
return true;
}

void fill(int k = 0){
int row = k / 9;
int col = k % 9;
if(k >= 81){
for(int i = 0; i < 9; ++i){
for(int j = 0; j < 9; ++j){
printf("%d ", square[i][j]);
}
printf("\n");
}
return;
}
if(square[row][col] > 0){
fill(k + 1);
return;
}
for(int i = 1; i <= 9; ++i){
if(isEnabled(k, i)){
square[row][col] = i;
fill(k + 1);
square[row][col] = 0;
}
}
}

int main(){
for(int i = 0; i < 9; ++i){
for(int j = 0; j < 9; ++j){
scanf("%d", &square[i][j]);
}
}
fill();
return 0;
}
```

(阅读全文…)

## 经典算法：整数划分问题

```6
5+1
4+2, 4+1+1
3+3, 3+2+1, 3+1+1+1
2+2+2, 2+2+1+1, 2+1+1+1+1
1+1+1+1+1+1
```

(阅读全文…)

## PHP中获取中英文混合字符串长度

```\$str = 'Hello world!';
echo strlen(\$str);	// 输出12
```

```\$str = '你好，世界！';
echo strlen(\$str);	// GBK或GB2312下输出12，UTF-8下输出18
```

(阅读全文…)

## [转载]你真的已经搞懂JavaScript了吗？

```if (!("a" in window)) {
var a = 1;
}

```var a = 1,
b = function a(x) {
x && a(--x);
};

```function a(x) {
return x * 2;
}
var a;

```function b(x, y, a) {
arguments[2] = 10;
```function a() {