算法笔记 上机训练实战指南 第8章 提高篇(2)--搜索专题 学习笔记
8.1 深度优先搜索(DFS)
The K−P factorization of a positive integer N is to write N as the sum of the P-th power of K positive integers. You are supposed to write a program to find the K−P factorization of N for any positive integers N, K and P.
Input Specification:
Each input file contains one test case which gives in a line the three positive integers N (≤), K (≤) and P (1). The numbers in a line are separated by a space.
Output Specification:
For each case, if the solution exists, output in the format:
N = n[1]^P + ... n[K]^P
where n[i]
(i
= 1, ..., K
) is the i
-th factor. All the factors must be printed in non-increasing order.
Note: the solution may not be unique. For example, the 5-2 factorization of 169 has 9 solutions, such as 1, or 1, or more. You must output the one with the maximum sum of the factors. If there is a tie, the largest factor sequence must be chosen -- sequence { , } is said to be larger than { , } if there exists 1 such that ai=bi for i<L and aL>bL.
If there is no solution, simple output Impossible
.
Sample Input 1:
169 5 2
Sample Output 1:
169 = 6^2 + 6^2 + 6^2 + 6^2 + 5^2
Sample Input 2:
169 167 3
Sample Output 2:
Impossible
#include<cstdio> #include<vector> using namespace std; int n,k,p,maxfacSum = - 1; vector<int> temp,ans,fac; int power(int x){ int ans = 1; for(int i = 0; i < p;i++){ ans *= x; } return ans; } void init(){ int temp = 0,i = 0; while(temp <= n){ fac.push_back(temp); temp = power(++i); } } void DFS(int index,int nowK,int sum,int facSum){ if(nowK == k && sum == n){ if(facSum > maxfacSum){ ans = temp; maxfacSum = facSum; } return; } if(nowK > k || sum > n) return; if(index-1 >= 0){ temp.push_back(index); DFS(index,nowK+1,sum+fac[index],facSum+index); temp.pop_back(); DFS(index-1,nowK,sum,facSum); } } int main(){ scanf("%d%d%d",&n,&k,&p); init(); DFS(fac.size()-1,0,0,0); if(maxfacSum == -1){ printf("Impossible\n"); }else{ printf("%d = %d^%d",n,ans[0],p); for(int i =1;i<ans.size();i++){ printf(" + %d^%d",ans[i],p); } } return 0; }
8.2 广度优先搜索(BFS)
One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the results of image analysis in which the core regions are identified in each MRI slice, your job is to calculate the volume of the stroke core.
Input Specification:
Each input file contains one test case. For each case, the first line contains 4 positive integers: M, N, L and T, where M and N are the sizes of each slice (i.e. pixels of a slice are in an M×N matrix, and the maximum resolution is 1286 by 128); L (≤) is the number of slices of a brain; and T is the integer threshold (i.e. if the volume of a connected core is less than T, then that core must not be counted).
Then L slices are given. Each slice is represented by an M×N matrix of 0's and 1's, where 1 represents a pixel of stroke, and 0 means normal. Since the thickness of a slice is a constant, we only have to count the number of 1's to obtain the volume. However, there might be several separated core regions in a brain, and only those with their volumes no less than T are counted. Two pixels are connected and hence belong to the same region if they share a common side, as shown by Figure 1 where all the 6 red pixels are connected to the blue one.
Figure 1
Output Specification:
For each case, output in a line the total volume of the stroke core.
Sample Input:
3 4 5 2
1 1 1 1
1 1 1 1
1 1 1 1
0 0 1 1
0 0 1 1
0 0 1 1
1 0 1 1
0 1 0 0
0 0 0 0
1 0 1 1
0 0 0 0
0 0 0 0
0 0 0 1
0 0 0 1
1 0 0 0
Sample Output:
26
#include<cstdio> #include<queue> using namespace std; struct node{ int x,y,z; }Node; int m,n,l,T; int pixel[1290][130][61]; bool inq[1290][130][61] = {false}; int X[6]={0,0,0,0,1,-1}; int Y[6]={0,0,1,-1,0,0}; int Z[6]={1,-1,0,0,0,0}; bool judge(int x,int y,int z){ if(x<0 || x>=m || y<0 || y>=n||z<0 || z>=l){ return false; } if(inq[x][y][z] == true || pixel[x][y][z] == 0) return false; return true; } int BFS(int x,int y,int z){ int tot = 0; queue<node> Q; Node.x = x,Node.y = y,Node.z = z; Q.push(Node); inq[x][y][z] = true; while(!Q.empty()){ node top = Q.front(); Q.pop(); tot++; for(int i=0;i<6;i++){ int newX = top.x + X[i]; int newY = top.y + Y[i]; int newZ = top.z + Z[i]; if(judge(newX,newY,newZ)){ Node.x = newX,Node.y = newY,Node.z = newZ; Q.push(Node); inq[newX][newY][newZ] = true; } } } if(tot >= T) return tot; else return 0; } int main(){ scanf("%d%d%d%d",&m,&n,&l,&T); for(int z=0;z<l;z++){ for(int x=0;x<m;x++){ for(int y = 0;y <n;y++){ scanf("%d",&pixel[x][y][z]); } } } int ans = 0; for(int z=0;z<l;z++){ for(int x=0;x<m;x++){ for(int y = 0;y <n;y++){ if(pixel[x][y][z] == 1 && inq[x][y][z] == false){ ans += BFS(x,y,z); } } } } printf("%d\n",ans); return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微服务架构学习与思考:微服务拆分的原则
· 记一次 .NET某云HIS系统 CPU爆高分析
· 如果单表数据量大,只能考虑分库分表吗?
· 一文彻底搞懂 MCP:AI 大模型的标准化工具箱
· 电商平台中订单未支付过期如何实现自动关单?
· 精选 4 款免费且实用的数据库管理工具,程序员必备!
· Cursor:一个让程序员“失业”的AI代码搭子
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(6)
· 重生之我是操作系统(七)----内存管理(上)
· .NET 阻止Windows关机以及阻止失败的一些原因