PAT甲级—暴力搜索
1091 Acute Stroke (30point(s))
基础的搜索,但是直接用递归会导致段错误,改用队列之后就不会了,这说明递归调用在空间利用率上还是很吃亏的。


#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <map>
#include <queue>
#include <vector>
#include <set>
#define ll long long
#define inf 0x3f3f3f
#define pii pair<int, int>
#define pb push_back
using namespace std;
const int maxn = 1e5+100;
int n, m, l, t;
int cnt, res, mp[70][200][1500];
bool vis[70][200][1500];
void dfs(int x, int y, int z){
if(x<1||x>l||y<1||y>n||z<1||z>m||mp[x][y][z]!=1||vis[x][y][z]) return;
vis[x][y][z] = 1, cnt++;
dfs(x+1, y, z), dfs(x, y+1, z), dfs(x, y, z+1);
dfs(x-1, y, z), dfs(x, y-1, z), dfs(x, y, z-1);
return;
}
int main(){
scanf("%d%d%d%d", &n, &m, &l, &t);
for(int i = 1; i <= l; i++)
for(int j = 1; j <= n; j++)
for(int k = 1; k <= m; k++)
scanf("%d", &mp[i][j][k]);
for(int i = 1; i <= l; i++){
for(int j = 1; j <= n; j++){
for(int k = 1; k <= m; k++){
if(mp[i][j][k]==1&&!vis[i][j][k]) {
dfs(i, j, k);
if(cnt>=t) res += cnt;
cnt = 0;
}
}
}
}
printf("%d", res);
}
Segmentation Fault(DFS)


#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <map>
#include <queue>
#include <vector>
#include <set>
#define ll long long
#define inf 0x3f3f3f
#define pii pair<int, int>
#define pb push_back
using namespace std;
const int maxn = 1e5+100;
struct node{
int x, y, z;
};
int n, m, l, t;
int cnt, res, mp[70][200][1500];
bool vis[70][200][1500];
void bfs(int x, int y, int z){
queue<node> que;
que.push({x, y, z});
int cnt = 0;
while(!que.empty()){
node tmp = que.front(); que.pop();
int x = tmp.x, y = tmp.y, z = tmp.z;
if(x<1||x>l||y<1||y>n||z<1||z>m||mp[x][y][z]!=1||vis[x][y][z]) continue;
vis[x][y][z] = 1, cnt++;
que.push({x+1, y, z}), que.push({x, y+1, z}), que.push({x, y, z+1});
que.push({x-1, y, z}), que.push({x, y-1, z}), que.push({x, y, z-1});
}
if(cnt>=t) res += cnt;
}
int main(){
scanf("%d%d%d%d", &n, &m, &l, &t);
for(int i = 1; i <= l; i++)
for(int j = 1; j <= n; j++)
for(int k = 1; k <= m; k++)
scanf("%d", &mp[i][j][k]);
for(int i = 1; i <= l; i++)
for(int j = 1; j <= n; j++)
for(int k = 1; k <= m; k++)
if(mp[i][j][k]==1&&!vis[i][j][k])
bfs(i, j, k);
printf("%d", res);
}
Accepted(BFS)
Reference:
https://github.com/LNoving/PAT
1103 Integer Factorization (30分)
这题就是DFS+剪枝,剪枝不够的话有几个测试样例会超时,主要体现在:
- 需要事先把各个数的p次方求出来并保存在数组中,避免后面在DFS中反复计算
- 各项的幂数相同,系数必然会形成排列,可以规定序列为降序,避免出现像3 2 4 1和2 4 3 1这样重复搜索的情况。进一步我们可以限制选取当前项系数的上下界,上限可以设置为上一项的系数值,下限可以通过判断最后能不能用完num来判断
怎么也没想到这题调试了一下午,最开始没用vector而是用的string,可能因为不那么熟悉string,出现了各种小问题比如初始化之类的,光在这个上面就百度了很久,惭愧。后来发现别人用的vector,就赶紧用这个改了一遍就好了,后面就是超时再优化的问题了


#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <map>
#include <queue>
#include <vector>
#include <set>
#define ll long long
#define inf 0x3f3f3f
#define pii pair<int, int>
#define pb push_back
using namespace std;
// const int maxn = 1e5+100;
int n, k, p;
int cnt, sum, fac[400];
//string s("", 400), res("", 400);
vector<int> now, res;
int qpow(int a, int n){
int res = 1;
while(n){
if(n&1) res *= a;
a *= a;
n >>= 1;
}
return res;
}
void init(){
for(int i = 0; i <= sqrt(n); i++)
fac[i] = qpow(i, p);
}
void dfs(int num, int id){
if(id>k) return;
else if(num==0&&id==k) {
if(cnt>sum) res = now, sum = cnt;
else if(cnt==sum) res = max(res, now);
return;
}
int r = id > 0 ? now[id-1] : pow(num, 1.0/p);
for(int i = r; i >= 1; i--){
if((k-id+1)*fac[i] < num) break;
else if(num-fac[i]<0) continue;
now.pb(i), cnt += i;
dfs(num-fac[i], id+1);
now.pop_back(), cnt -=i;
}
}
int main(){
scanf("%d%d%d", &n, &k, &p);
init(), dfs(n, 0);
if(!res.empty()){
printf("%d = %d^%d", n, res[0], p);
for(int i = 1; i <= k-1; i++) printf(" + %d^%d", res[i], p);
}
else printf("Impossible");
}
PAT甲级—暴力搜索的更多相关文章
- PAT甲级考前整理(2019年3月备考)之一
转载请注明出处:https://www.cnblogs.com/jlyg/p/7525244.html 终于在考前,刷完PAT甲级131道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种 ...
- 2019秋季PAT甲级_备考总结
2019 秋季 PAT 甲级 备考总结 在 2019/9/8 的 PAT 甲级考试中拿到了满分,考试题目的C++题解记录在这里,此处对备考过程和考试情况做一个总结.如果我的方法能帮助到碰巧点进来的有缘 ...
- PAT甲级1098. Insertion or Heap Sort
PAT甲级1098. Insertion or Heap Sort 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.在每次迭代中,插入排序从输入数据中删除一个元素 ...
- PAT甲级1033. To Fill or Not to Fill
PAT甲级1033. To Fill or Not to Fill 题意: 有了高速公路,从杭州到任何其他城市开车很容易.但由于一辆汽车的坦克容量有限,我们不得不在不时地找到加油站.不同的加油站可能会 ...
- PAT甲级1026. Table Tennis
PAT甲级1026. Table Tennis 题意: 乒乓球俱乐部有N张桌子供公众使用.表的编号从1到N.对于任何一对玩家,如果有一些表在到达时打开,它们将被分配给具有最小数字的可用表.如果所有的表 ...
- PAT甲级1010. Radix
PAT甲级1010. Radix (25) 题意: 给定一对正整数,例如6和110,这个等式6 = 110可以是真的吗?答案是"是",如果6是十进制数,110是二进制数. 现在对于 ...
- PAT甲级考前整理(2019年3月备考)之三,持续更新中.....
PAT甲级考前整理一:https://www.cnblogs.com/jlyg/p/7525244.html,主要讲了131题的易错题及坑点 PAT甲级考前整理二:https://www.cnblog ...
- PAT甲级考前整理(2019年3月备考)之二,持续更新中.....
PAT甲级考前整理之一网址:https://www.cnblogs.com/jlyg/p/7525244.html,主要总结了前面131题的类型以及易错题及坑点. PAT甲级考前整理三网址:https ...
- PAT甲级专题|树的遍历
PAT甲级专题-树的遍历 涉及知识点:树.建树.深度优先搜索.广度优先搜索.递归 甲级PTA 1004 输出每一层的结点,邻接表vector建树后.用dfs.bfs都可以边搜边存当前层的数据, #in ...
随机推荐
- 项目API接口鉴权流程总结
权益需求对接中,公司跟第三方公司合作,有时我们可能作为甲方,提供接口给对方,有时我们也作为乙方,调对方接口,这就需要API使用签名方法(Sign)对接口进行鉴权.每一次请求都需要在请求中包含签名信息, ...
- AndroidStuidio安装
前言 端午小长假,安卓入门走起 正文 下载AndroidStudio 这里给出google的官网 https://developer.android.com/studio 注意,因404原因,如果你无 ...
- 温故而知新--day2
温故而知新--day2 类 类与对象 类是一个抽象的概念,是指对现实生活中一类具有共同特征的事物的抽象.其实列化后称为对象.类里面由类属性组成,类属性可以分为数据属性和函数属性(函数属性又称为类方法) ...
- 【RAC】oracle11g r2 rac环境删除节点步骤
1.移除数据库实例 如果节点运行了service首先需要删除service使用dbca图形化界面删除节点依次选择 Real Application Clusters -- > Instance ...
- Xctf攻防世界—crypto—Normal_RSA
下载压缩包后打开,看到两个文件flag.enc和pubkey.pem,根据文件名我们知道应该是密文及公钥 这里我们使用一款工具进行解密 工具链接:https://github.com/3summer/ ...
- MongoDB数据库,一些的筛选过滤查询操作和db.updae()更新数据库记录遇到的坑。
缘由:使用MongoDB时遇到一些需要查询/更新操作指定某些字段的业务场景 查询和更新指定字段就需要进行简单的筛选和过滤,也能在大数据量时减少查询消耗时间 1. 查询数据库某些指定字段,同时默认返回_ ...
- USB限流IC,输入5V,输出5V,最大3A限流
USB限流芯片,5V输入,输出5V电压,限流值可以通过外围电阻进行调节,PWCHIP产品中可在限流范围0.4A-4.8A,并具有过压关闭保护功能. 过压关闭保护: 如芯片:PW1555,USB我们一半 ...
- Matlab GUI学习总结
从简单的例子说起吧. 创建Matlab GUI界面通常有两种方式: 1,使用 .m 文件直接动态添加控件 2. 使用 GUIDE 快速的生成GUI界面显然第二种可视化编辑方法算更适合 ...
- 【分享】每个 Web 开发者在 2021 年必须拥有 15 个 VSCode 扩展
为什么VSCode如此受欢迎 Visual Studio Code在开发人员中迅速流行起来,它是最流行的开发环境,可定制性是其流行的原因之一. 因此,如果你正在使用VSCode,这里有一个扩展列表,你 ...
- 回归测试_百度百科 https://baike.baidu.com/item/%E5%9B%9E%E5%BD%92%E6%B5%8B%E8%AF%95
回归测试_百度百科https://baike.baidu.com/item/%E5%9B%9E%E5%BD%92%E6%B5%8B%E8%AF%95