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 ...
随机推荐
- WPF APP 启动时增加特殊逻辑
public partial class App : Application { public App() { this.Startup += (o1, e1)=>{ string comman ...
- 最全的HashMap源码解析!
HashMap源码解析 HashMap采用键值对形式的存储结构,每个key对应唯一的value,查询和修改的速度很快,能到到O(1)的平均复杂度.他是非线程安全的,且不能保证元素的存储顺序. 他的关系 ...
- python学习笔记 | 递归思想
1.引子 大师 L. Peter Deutsch 说过: To Iterate is Human, to Recurse, Divine. 中文译为:人理解迭代,神理解递归 2.什么是递归 简单理解: ...
- SDUST数据结构 - chap9 排序
判断题: 选择题: 编程题: 7-1 排序: 输入样例: 11 4 981 10 -17 0 -20 29 50 8 43 -5 输出样例: -20 -17 -5 0 4 8 10 29 43 50 ...
- ctfhub技能树—文件上传—.htaccess
首先介绍一下.htaccess(来自百度百科) .htaccess文件(或者"分布式配置文件"),全称是Hypertext Access(超文本入口).提供了针对目录改变配置的方法 ...
- ctfhub技能树—信息泄露—目录遍历
打开靶机 查看页面 点击后发现几个目录 于是开始查找 在2/1目录下发现flag.txt 成功拿到flag 练习一下最近学习的requests库 附上源码 #! /usr/bin/env python ...
- oracle rac搭建单实例DG步骤(阅读全篇后再做)
环境介绍 主库: 主机名 rac01 rac02 实体IP 10.206.132.232 10.206.132.233 私有IP 192.168.56.12 192.168.56.13 虚拟IP 10 ...
- 深入解析vue响应式原理
摘要:本文主要通过结合vue官方文档及源码,对vue响应式原理进行深入分析. 1.定义 作为vue最独特的特性,响应式可以说是vue的灵魂了,表面上看就是数据发生变化后,对应的界面会重新渲染,那么响应 ...
- LiteOS调测利器:backtrace函数原理知多少
摘要:本文将会和读者分享LiteOS 5.0版本中Cortex-M架构的backtrace软件原理及实现,供大家参考和学习交流. 原理介绍 汇编指令的执行流程 图 1 汇编指令的执行顺序 上图1所示, ...
- 琐碎的想法(三)对Java的批评的看法
编写本文的目的 在大环境下,Java是一个饱受争议的语言,一方面在工程上它的流行程度非常高:另一方面,越是资深的软件工程师就越容易对这个语言感到不满. 在这种情况下,博主希望每一个Java程序员能够耐 ...