DFS+BFS

在树与图的习题中已经多次用到这些方法和思想,比如遍历什么的。下面的是针对这两类方法的典型问题,

深度优先搜索DFS

 
1103 Integer Factorization (30 分)
 

The KP 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 KP factorization of N for any positive integers N, Kand 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>
#include <math.h>
using namespace std;
int n,k,p;
vector<int> factor,ans,powv;
int maxfs=0;
void init()
{
    int i=0,temp=0;
    while(temp<=n)
    {
        powv.push_back(temp);
        temp=pow(++i,p);
    }
}
void dfs(int index,int knum,int sum,int fsum)
{
    if(knum==k&&sum==n)
    {
        if(fsum>maxfs)
        {
            maxfs=fsum;
            ans=factor;
            return;
        }
    }
    if(sum>n||knum>k) return;
    if(index>=1)
    {
        factor.push_back(index);
        dfs(index,knum+1,sum+powv[index],fsum+index);
        factor.pop_back();
        dfs(index-1,knum,sum,fsum);
    }
}
int main()
{
    scanf("%d %d %d",&n,&k,&p);
    init();
    dfs(powv.size()-1,0,0,0);
    if(ans.size()==0) printf("Impossible\n");
    else
    {
        printf("%d = ",n);
        for(int i=0;i<k;i++)
        {
            printf("%d^%d",ans[i],p);
            if(i<k-1) printf(" + ");
        }
    }
    
    return 0;
}

大致思路虽然历历在目,但在具体问题的实现中还是有很多注意的地方。关键的一点就是  剪支,化简,如此题一样,先将所有可能需要的权值计算出来,这样会大大减少递归中的运算。具体的DFS算法就要分析问题,如此题的,可以选择重复的底数,那该如何处理呢?我也一时没搞明白,看了书后恍然大悟,在分支时,不必将index-1,而是

dfs(index,knum+1,sum+powv[index],fsum+index);
这样在递归时就会包含了选重复数的情况,由于有另一不选当前数的分支,这样就可以包含跳出重复的情况。总之就是递归的神奇。


 
广度优先搜索BFS
 
1091 Acute Stroke (30 分)
 

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.

figstroke.jpg

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
作者: CHEN, Yue
单位: 浙江大学
时间限制: 600 ms
内存限制: 64 MB
#include <cstdio>
#include <queue>
using namespace std;
struct node
{
    int x,y,z;
};
int m,n,l,t;
int mat[70][1300][1300];
bool iscount[70][1300][1300];
int x[6]={1,-1,0,0,0,0},
    y[6]={0,0,1,-1,0,0},
    z[6]={0,0,0,0,1,-1};
bool judge(int i,int j,int k)
{
    if(i<0||i>=l||j<0||j>=m||k<0||k>=n) return false;
    if(mat[i][j][k]==0||iscount[i][j][k]==true) return false;
    return true;
}
int bfs(int i,int j,int k)
{
    int ans=0;
    queue<node> q;
    node p;
    p.z=i;p.x=j;p.y=k;
    q.push(p);
    iscount[i][j][k]=true;
    while(!q.empty())
    {
        node now=q.front();
        q.pop();
        ans++;
        for(int s=0;s<6;s++)
        {
            int newz=now.z+z[s];
            int newx=now.x+x[s];
            int newy=now.y+y[s];
            if(judge(newz,newx,newy))
            {
                p.z=newz;p.x=newx;p.y=newy;
                q.push(p);
                iscount[newz][newx][newy]=true;
            } 
        }
    }
    if(ans>=t) return ans;
    else return 0;
}

int main()
{
    scanf("%d%d%d%d",&m,&n,&l,&t);
    for(int i=0;i<l;i++)
    {
        for(int j=0;j<m;j++)
        {
            for(int k=0;k<n;k++)
            {
                scanf("%d",&mat[i][j][k]);
                iscount[i][j][k]=false;
            }
        }
    }
    int sum=0;
    for(int i=0;i<l;i++)
    {
        for(int j=0;j<m;j++)
        {
            for(int k=0;k<n;k++)
            {
                if(mat[i][j][k]==1&&iscount[i][j][k]==false)
                {
                    sum+=bfs(i,j,k);
                }
                
            }
        }
    }
    printf("%d",sum);
    
    return 0;
}

常规的套路,矩阵储存,遍历,判断矩阵,最开始直接用递归,果然有2个点超时了,还是改为用点坐标的结构体和队列进行迭代的BFS,和之前的树的层序遍历一样的方法。

猜你喜欢

转载自www.cnblogs.com/fremontxutheultimate/p/11332361.html
今日推荐