0x02 递推与递归
2024-08-29 08:19:21
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
int n;
vector<int> chosen;
void calc(int x) {
if (x==n+) {
for (int i=; i<chosen.size(); ++i)
printf("%d ", chosen[i]);
cout<<endl;
return;
}
calc(x+);
chosen.push_back(x);
calc(x+);
chosen.pop_back();
}
int main() {
cin>>n;
calc();
return ;
}
在前面的基础上加入以下代码即可:
if (chosen.size()>m||chosen.size()+(n-x+)<m)
return;
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
int n, m;
vector<int> chosen;
void calc(int x) {
if (chosen.size()>m||chosen.size()+(n-x+)<m)
return;
if (x==n+) {
for (int i=; i<chosen.size(); ++i)
printf("%d ", chosen[i]);
cout<<endl;
return;
}
chosen.push_back(x);
calc(x+);
chosen.pop_back();
calc(x+);
}
int main() {
cin>>n>>m;
calc();
return ;
}
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
int n;
int order[];
int chosen[];
void calc(int k) {
if (k==n+) {
for (int i=; i<=n; ++i)
printf("%d ", order[i]);
puts("");
return;
}
for (int i=; i<=n; ++i) {
if (chosen[i]) continue;
order[k]=i;
chosen[i]=;
calc(k+);
order[k]=;
chosen[i]=;
}
}
int main() {
cin>>n;
calc();
return ;
}
容易发现3个性质:
1.每个位置最多点击一次
2.固定了第一行,满足题意的点击方案只有一种,如第i行已被固定,第i位为1,则第i+1行该位置需要被点击
3.点击先后顺序不影响最终结果
所以用位运算枚举第一行的点击方案,再递推出2~5行的点击,最后判断第5行是否满足题意即可。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int g[][];
char nex[][]={{,},{,-},{-,},{,},{,}}; void turn(int x, int y) {
for (int i=; i<; ++i) {
int tx=x+nex[i][];
int ty=y+nex[i][];
if (tx>&&tx<=&&ty>&&ty<=)
g[tx][ty]^=;
}
} void print() {
/*for (int i=1; i<=5; ++i) {
for (int j=1; j<=5; ++j)
printf("%1d", g[i][j]);
printf("\n");
}
puts("");*/
} int solve() {
int ans=0x3f3f3f3f;
for (int i=; i<; ++i) {
int flag=, tot=;
int tmp[][];
memcpy(tmp, g, sizeof(tmp));
for (int j=; j<; ++j)
if ((i>>j)&) //如果第j位为1,则第一排的第j位需要被按一下
{ turn(, j+); tot++; print();/*printf("a\n");*/}
for (int j=; j<; ++j) {
for (int k=; k<=; ++k)
if (g[j][k]==)
{ turn(j+, k); tot++; print();/*printf("b\n");*/}
}
for (int j=; j<=; ++j)
if (g[][j]==) flag=;
memcpy(g, tmp, sizeof(g));
if (flag) ans=min(ans, tot);
else continue;
}
if (ans>) return -;
return ans;
} int main() {
//freopen("1.txt", "w", stdout);
int T;
scanf("%d", &T);
while (T--) {
for (int i=; i<=; ++i)
for (int j=; j<=; ++j)
scanf("%1d", &g[i][j]);
printf("%d\n", solve());
}
return ;
}
/*
1
11101
11101
11110
11111
11111
*/
最新文章
- Linux 基本命令
- Swift_UI_UILabel
- 关于easyui遇到的问题
- jqgrid显示一行的详情
- PHP会话Session
- JSON、使用JSON进行数据交换的基础和原理
- SAP、BW 权限控制设置
- SQL server 与Oracle开发比较
- linux 进程线程拓展
- JAVAEE——SSH项目实战01:SVN介绍、安装和使用方法
- Mac从零配置Vim
- 算法题丨3Sum
- dom树渲染对性能的影响
- intoj
- Oracle 备份表数据
- 【ASP.NET MVC系列】浅谈ASP.NET 程序发布过程
- SharpSvn 调用在运行时提示加载程序集出错,或有依赖项
- PAT 1101 Quick Sort[一般上]
- Java之IO(一)InputStream和OutputStream
- 我的开源项目——Jerry