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 ...
随机推荐
- FastApi 进阶
前言 终于有了第一个使用 FastApi 编写的线上服务, 在开发的过程中还是遇到了些问题, 这里记录一下 正文 目录结构 我们知道, FastApi 的启动方式推荐使用 uvicorn, 其启动方式 ...
- 【Oracle】CBO优化详解
SQL优化是数据优化的重要方面,本文将分析Oracle自身的CBO优化,即基于成本的优化方法.Oracle为了自动的优化sql语句需要各种统计数据作为优化基础.外面会通过sql的追踪来分析sql的执行 ...
- PyTorch 于 JupyterLab 的环境准备
PyTorch 是目前主流的深度学习框架之一,而 JupyterLab 是基于 Web 的交互式笔记本环境.于 JupyterLab 我们可以边记笔记的同时.边执行 PyTorch 代码,便于自己学习 ...
- JavaScript小记
JavaScript小记 1. 简介 1. 语言描述 JavaScript 是一门跨平台.面向对象的弱类型动态脚本编程语言 JavaScript 是一门基于原型.函数先行的语言 JavaScript ...
- ElasticSearch Python 基本操作
创建索引 from elasticsearch import Elasticsearch es = Elasticsearch('192.168.149.96:9200') mappings = { ...
- uwsgi 启动django
1, django 官方文档可配置项如下: 2,启动django 的配置: 1,和settings.py 同级目录下新建wsgi.py (该配置和manager.py 的配置基本是一样的) impo ...
- python多线程和GIL全局解释器锁
1.线程 线程被称为轻量级进程,是最小执行单元,系统调度的单位.线程切换需要的资源一般,效率一般. 2.多线程 在单个程序中同时运行多个线程完成不同的工作,称为多线程 3.并 ...
- Java语法糖详解
语法糖 语法糖(Syntactic Sugar),也称糖衣语法,是由英国计算机学家 Peter.J.Landin 发明的一个术语,指在计算机语言中添加的某种语法,这种语法对语言的功能并没有影响,但是更 ...
- YARN运行流程
- Lua大量字符串拼接方式效率对比及原因分析
Lua大量字符串拼接方式效率对比及原因分析_AaronChan的博客-CSDN博客_lua字符串拼接消耗 https://blog.csdn.net/qq_26958473/article/detai ...