单页面视频网站,我想投资谁有项目,北京国税局网站做票种核定,建设电子商务网站需要什么设备题目描述
在2行5列的格子中填入1到10的数字。 要求#xff1a;相邻的格子中的数#xff0c;右边的大于左边的#xff0c;下边的大于上边的。 如下图所示的2种#xff0c;就是合格的填法。 请你计算一共有多少种可能的方案。 输出
请输出该整数#xff0c;不要输出任何多…题目描述
在2行5列的格子中填入1到10的数字。 要求相邻的格子中的数右边的大于左边的下边的大于上边的。 如下图所示的2种就是合格的填法。 请你计算一共有多少种可能的方案。 输出
请输出该整数不要输出任何多余的内容
法一 代码如下
#include iostream
using namespace std;
bool vis[12];
int a[12][12], ans 0;bool check(int x, int y) {if (x 0) {if (a[x][y] a[x][y - 1] || y 0) {return true;}}if (a[x][y] a[x][y - 1] a[x][y] a[x - 1][y] || y 0 a[x][y] a[x - 1][y])return true;return false;
}void dfs(int x, int y) {if (x 2)ans;for (int i 1; i 10; i) {a[x][y] i;if (!vis[i]) {if (check(x, y)) {vis[i] true;if (y 4) {dfs(x 1, 0);} else {dfs(x, y 1);}vis[i] false;}}}
}int main() {dfs(0, 0);cout ans endl;return 0;
}法二 代码如下
#include iostream
#include algorithm
using namespace std;int a[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int ans;bool check() {if (a[1] a[0] a[2] a[1] a[3] a[2] a[4] a[3] a[6] a[5] a[7] a[6] a[8] a[7] a[9] a[8] a[5] a[0] a[6] a[1] a[7] a[2] a[8] a[3] a[9] a[4]) {return true;}return false;
}int main() {do {if (check())ans;} while (next_permutation(a, a 10));cout ans endl;return 0;
}next_permutation()的错误使用导致错误 样例代码如下
#include iostream
#include algorithm
using namespace std;int a[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int ans;bool check() {//数组下标从0开始这里却从1开始导致错误if (a[2] a[1] a[3] a[2] a[4] a[3] a[5] a[4] a[7] a[6] a[8] a[7] a[9] a[8] a[10] a[9] a[6] a[1] a[7] a[2] a[8] a[3] a[9] a[10] a[10] a[5]) {return true;}return false;
}int main() {do {if (check())ans;} while (next_permutation(a, a 10));cout ans endl;return 0;
}在这里可以刷一下同样可以用next_permutation()函数AC的题目 [蓝桥杯2016初赛]方格填数
[蓝桥杯2017初赛]纸牌三角形