P1036 [NOIP 2002 普及组] 选数
对每个数进行一次选或不选,选完后记录数字且回溯。填满 个数字后计算数字和是否是质数即可。
#include <bits/stdc++.h>
using namespace std;
int n, k, a[20 + 9], ans[20 + 9], cnt = 0;
bool vis[20 + 9];
bool p(int n)//判断质数函数
{
if(n <= 1)
{
return false;
}
for (int i = 2; i * i <= n; i ++)
{
if (n % i == 0)//n是否能整除i
{
return false;
}
}
return true;
}
void C(int row, int m)
{
if (row > k)//选到了第k个数的下一个位置
{
int sum = 0;
for (int i = 1; i <= k; i ++)
{
sum += ans[i];//累加ans中数字和
}
if (p(sum))//sum是一个质数
{
cnt ++;
}
return;
}
for (int i = m + 1; i <= n; i ++)//不枚举重复的情况
{
if (vis[i] == false)//当前数字i没用过
{
ans[row] = a[i];//赋值
vis[i] = true;//标记用过
C(row + 1, i);
vis[i] = false;//回溯
}
}
}
int main()
{
cin >> n >> k;
for (int i = 1; i <= n; i ++)
{
cin >> a[i];
}
C(1, 0);//开始搜索
cout << cnt;
return 0;
}P1479 宿舍里的故事之五子棋
对每个格子进行放或不放棋子的操作,放完后记录棋子且回溯。放完 个棋子后进5子连线的判断即可。
#include <bits/stdc++.h>
using namespace std;
int n, vis[5 + 9][5 + 9], ans = 0;
bool d[13 + 9];
void DFS(int x, int y, int row)
{
if (x > 5 || row == n)//已经放到了n个或已经放完了整个棋盘
{
if (row == n)//当前放完了n个
{
int k = 0;
if (vis[1][1] == true && vis[1][2] == true && vis[1][3] == true && vis[1][4] == true && vis[1][5] == true)//判断5子连珠
{
k ++;
}
if (vis[2][1] == true && vis[2][2] == true && vis[2][3] == true && vis[2][4] == true && vis[2][5] == true)//判断5子连珠
{
k ++;
}
if (vis[3][1] == true && vis[3][2] == true && vis[3][3] == true && vis[3][4] == true && vis[3][5] == true)//判断5子连珠
{
k ++;
}
if (vis[4][1] == true && vis[4][2] == true && vis[4][3] == true && vis[4][4] == true && vis[4][5] == true)//判断5子连珠
{
k ++;
}
if (vis[5][1] == true && vis[5][2] == true && vis[5][3] == true && vis[5][4] == true && vis[5][5] == true)//判断5子连珠
{
k ++;
}
if (vis[1][1] == true && vis[2][1] == true && vis[3][1] == true && vis[4][1] == true && vis[5][1] == true)//判断5子连珠
{
k ++;
}
if (vis[1][2] == true && vis[2][2] == true && vis[3][2] == true && vis[4][2] == true && vis[5][2] == true)//判断5子连珠
{
k ++;
}
if (vis[1][3] == true && vis[2][3] == true && vis[3][3] == true && vis[4][3] == true && vis[5][3] == true)//判断5子连珠
{
k ++;
}
if (vis[1][4] == true && vis[2][4] == true && vis[3][4] == true && vis[4][4] == true && vis[5][4] == true)//判断5子连珠
{
k ++;
}
if (vis[1][5] == true && vis[2][5] == true && vis[3][5] == true && vis[4][5] == true && vis[5][5] == true)//判断5子连珠
{
k ++;
}
if (vis[1][1] == true && vis[2][2] == true && vis[3][3] == true && vis[4][4] == true && vis[5][5] == true)//判断5子连珠
{
k ++;
}
if (vis[1][5] == true && vis[2][4] == true && vis[3][3] == true && vis[4][2] == true && vis[5][1] == true)//判断5子连珠
{
k ++;
}
d[k] = true;//不可重复加上同样的连珠数
}
return;
}
vis[x][y] = false;//不放在(x, y)
if (y == 5)//当前行搜完,搜索下一行
{
DFS(x + 1, 1, row);
}
else//搜下一个位置
{
DFS(x, y + 1, row);
}
vis[x][y] = true;//放
if (y == 5)//当前行搜完,搜索下一行
{
DFS(x + 1, 1, row + 1);
}
else//搜下一个位置
{
DFS(x, y + 1, row + 1);
}
vis[x][y] = false;//回溯
}
int main()
{
cin >> n;
DFS(1, 1, 0);
for (int i = 1; i <= 12; i ++)
{
if (d[i])//有出现这个k
{
ans += i;//加上答案
}
}
cout << ans;
return 0;
}P1123 取数游戏
对每个格子进行选或不选的操作,如进入相邻圈则一定不选。选完整个二维数组累加数字和取最大值即可。
#include <bits/stdc++.h>
using namespace std;
int n, m, vis[13 + 9][13 + 9], ans = 0;//同一个点可以是在多个已选数字的相邻范围之内的,用int类型的vis
int a[13 + 9][13 + 9], MAX;
void DFS(int x, int y, int row)
{
if (x == n + 1)//放完整个数组
{
MAX = max(MAX, row);//记录最大值MAX
return;
}
if (y == m)不放
{
DFS(x + 1, 1, row);//搜索下一行,和不变
}
else
{
DFS(x, y + 1, row);//搜索下一个数字,和不变
}
if (vis[x][y] == 0)//当前的(x, y)可以放数字
{
vis[x][y] ++;//累加相邻圈
vis[x - 1][y] ++;//累加相邻圈
vis[x + 1][y] ++;//累加相邻圈
vis[x][y - 1] ++;//累加相邻圈
vis[x][y + 1] ++;//累加相邻圈
vis[x - 1][y - 1] ++;//累加相邻圈
vis[x + 1][y - 1] ++;//累加相邻圈
vis[x - 1][y + 1] ++;//累加相邻圈
vis[x + 1][y + 1] ++;//累加相邻圈
if (y == m)//放
{
DFS(x + 1, 1, row + a[x][y]);//搜索下一行,和加上a[x][y]
}
else
{
DFS(x, y + 1, row + a[x][y]);搜索下一个数字,和加上a[x][y]
}
vis[x][y] --;回溯
vis[x - 1][y] --;回溯
vis[x + 1][y] --;回溯
vis[x][y - 1] --;回溯
vis[x][y + 1] --;回溯
vis[x - 1][y - 1] --;回溯
vis[x + 1][y - 1] --;回溯
vis[x - 1][y + 1] --;回溯
vis[x + 1][y + 1] --;回溯
}
}
int main()
{
int t;
cin >> t;
while (t --)
{
cin >> n >> m;
for (int i = 1; i <= n; i ++)
{
for (int j = 1; j <= m; j ++)
{
cin >> a[i][j];
}
}
MAX = -1e9;//重置MAX
DFS(1, 1, 0);//开始搜索
cout << MAX << "\n";
}
return 0;
}