Skip to content

Commit 7773bd9

Browse files
添加 0051.N皇后.md C语言版本
1 parent 7913258 commit 7773bd9

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

problems/0051.N皇后.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,5 +521,130 @@ func solveNQueens(_ n: Int) -> [[String]] {
521521
}
522522
```
523523

524+
### C
525+
```c
526+
char ***ans;
527+
char **path;
528+
int ansTop, pathTop;
529+
//将path中元素复制到ans中
530+
void copyPath(int n) {
531+
char **tempPath = (char**)malloc(sizeof(char*) * pathTop);
532+
int i;
533+
for(i = 0; i < pathTop; ++i) {
534+
tempPath[i] = (char*)malloc(sizeof(char) * n + 1);
535+
int j;
536+
for(j = 0; j < n; ++j)
537+
tempPath[i][j] = path[i][j];
538+
tempPath[i][j] = '\0';
539+
540+
}
541+
ans[ansTop++] = tempPath;
542+
}
543+
544+
//判断当前位置是否有效(是否不被其它皇后影响)
545+
int isValid(int x, int y, int n) {
546+
int i, j;
547+
//检查同一行以及同一列是否有效
548+
for(i = 0; i < n; ++i) {
549+
if(path[y][i] == 'Q' || path[i][x] == 'Q')
550+
return 0;
551+
}
552+
//下面两个for循环检查斜角45度是否有效
553+
i = y - 1;
554+
j = x - 1;
555+
while(i >= 0 && j >= 0) {
556+
if(path[i][j] == 'Q')
557+
return 0;
558+
--i, --j;
559+
}
560+
561+
i = y + 1;
562+
j = x + 1;
563+
while(i < n && j < n) {
564+
if(path[i][j] == 'Q')
565+
return 0;
566+
++i, ++j;
567+
}
568+
569+
//下面两个for循环检查135度是否有效
570+
i = y - 1;
571+
j = x + 1;
572+
while(i >= 0 && j < n) {
573+
if(path[i][j] == 'Q')
574+
return 0;
575+
--i, ++j;
576+
}
577+
578+
i = y + 1;
579+
j = x -1;
580+
while(j >= 0 && i < n) {
581+
if(path[i][j] == 'Q')
582+
return 0;
583+
++i, --j;
584+
}
585+
return 1;
586+
}
587+
588+
void backTracking(int n, int depth) {
589+
//若path中有四个元素,将其拷贝到ans中。从当前层返回
590+
if(pathTop == n) {
591+
copyPath(n);
592+
return;
593+
}
594+
595+
//遍历横向棋盘
596+
int i;
597+
for(i = 0; i < n; ++i) {
598+
//若当前位置有效
599+
if(isValid(i, depth, n)) {
600+
//在当前位置放置皇后
601+
path[depth][i] = 'Q';
602+
//path中元素数量+1
603+
++pathTop;
604+
605+
backTracking(n, depth + 1);
606+
//进行回溯
607+
path[depth][i] = '.';
608+
//path中元素数量-1
609+
--pathTop;
610+
}
611+
}
612+
}
613+
614+
//初始化存储char*数组path,将path中所有元素设为'.'
615+
void initPath(int n) {
616+
int i, j;
617+
for(i = 0; i < n; i++) {
618+
//为path中每个char*开辟空间
619+
path[i] = (char*)malloc(sizeof(char) * n + 1);
620+
//将path中所有字符设为'.'
621+
for(j = 0; j < n; j++)
622+
path[i][j] = '.';
623+
//在每个字符串结尾加入'\0'
624+
path[i][j] = '\0';
625+
}
626+
}
627+
628+
char *** solveNQueens(int n, int* returnSize, int** returnColumnSizes){
629+
//初始化辅助变量
630+
ans = (char***)malloc(sizeof(char**) * 400);
631+
path = (char**)malloc(sizeof(char*) * n);
632+
ansTop = pathTop = 0;
633+
634+
//初始化path数组
635+
initPath(n);
636+
backTracking(n, 0);
637+
638+
//设置返回数组大小
639+
*returnSize = ansTop;
640+
int i;
641+
*returnColumnSizes = (int*)malloc(sizeof(int) * ansTop);
642+
for(i = 0; i < ansTop; ++i) {
643+
(*returnColumnSizes)[i] = n;
644+
}
645+
return ans;
646+
}
647+
```
648+
524649
-----------------------
525650
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
 (0)