做网站泉州wordpress 前后台都进不去
做网站泉州,wordpress 前后台都进不去,软件开发包含网站开发吗,东莞网站推广裙问题描述 第一种方法
每一行放一个皇后边放皇后边判断是否符合条件递归到第n行#xff0c;则说明当前方案符合条件#xff0c;进行遍历
代码实现
#include cstring
#include iostream
#include algorithmusing namespace std;const int N 10;int…问题描述 第一种方法
每一行放一个皇后边放皇后边判断是否符合条件递归到第n行则说明当前方案符合条件进行遍历
代码实现
#include cstring
#include iostream
#include algorithmusing namespace std;const int N 10;int n;
char path[N][N]; // 二维棋盘
bool col[N], dg[N * 2], udg[N * 2]; // col表示列、dg表示对角线、udg表示反对角线void dfs(int u)
{if(u n) // 当前行数等于n皇后放置完毕{for(int i 0; i n; i){for(int j 0; j n; j) cout path[i][j];puts();}puts();return;}for(int i 0; i n; i) // 判断当前行在哪一列放皇后{if(!col[i] !dg[u i] !udg[u - i n]) // 满足条件{path[u][i] Q;col[i] dg[u i] udg[u - i n] true; dfs(u 1);col[i] dg[u i] udg[u - i n] false; // 回溯path[u][i] .;}}
}int main()
{cin n;for(int i 0;i n; i) // 初始化for(int j 0; j n; j) path[i][j] .;dfs(0);return 0;
}
第二种方法
遍历每一个位置分两种情况
放皇后不放皇后
每次遍历记录当前棋盘上放的皇后的数量当数量等于n时当前皇后的放置位置符合条件
代码实现
#include iostream
#include cstring
#include algorithmusing namespace std;const int N 10;int n;
char path[N][N];
bool row[N], col[N], dg[N * 2], udg[N * 2]; // row表示行、col表示列、dg表示对角线、udg表示反对角线void dfs(int x, int y, int s) // x表示x轴位置y表示y轴位置s表示当前棋盘皇后数量
{if(y n) // 当y轴超出棋盘范围x轴加1y轴为0{y 0;x;}if(x n) // 当前位置x轴超出棋盘范围如果皇后数量为n当前棋盘满足条件{if(s n){for(int i 0; i n; i){for(int j 0; j n; j) cout path[i][j];puts();}puts();}return;}// 不放皇后dfs(x, y 1, s);// 放皇后if(!row[x] !col[y] !dg[x y] !udg[x - y n]){path[x][y] Q;row[x] col[y] dg[x y] udg[x - y n] true;dfs(x, y 1, s 1); // y轴加1row[x] col[y] dg[x y] udg[x - y n] false; // 回溯path[x][y] .;}
}int main()
{cin n;// 初始化for(int i 0;i n; i)for(int j 0; j n; j) path[i][j] .;dfs(0, 0, 0); // 从0 0出发皇后数量为0return 0;
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/89400.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!