PAT甲级--图的遍历

A 1103 Integer Factorization

思路:
深度优先遍历
递归式(岔道口):选不选这个数
递归边界(死胡同):sum==N且取到了k个数
因为要底数字典序最大,所以考虑从大到小取数

#include <cstdio>
#include <algorithm>
#include <vector>
#include <algorithm>
#include<iostream>
#include<math.h>
using namespace std;

//const int maxn=10010;
int n,k,p,maxfacsum=-1;

vector<int> fac,ans,temp;
//fac记录连续数列的p次方
//ans存放最优底数序列
//temp存放递归中的临时底数序列

//预处理fac数组
void init()
{
    int i=0,temp=0;
    while(temp<=n)
    {
        fac.push_back(temp);
        int ans=1;
        i++;
        for(int j=0;j<p;j++)
        {
            ans*=i;
        }
        temp=ans;
    }
}

//当前访问fac[index]
//nowwork当前选中个数
//sum当前选中的数之和
//facsum为当前选中的底数之和
void DFS(int index,int nowk,int sum,int facsum)
{
    if(sum==n&&nowk==k)//找到一个满足的序列
    {
        if(facsum>maxfacsum)//底数之和更优
        {
            ans=temp;
            maxfacsum=facsum;
        }
        return;
    }
    if(sum>n||nowk>k)
        return;
    if(index-1>=0)
    {
        temp.push_back(index);//选index号
        DFS(index,nowk+1,sum+fac[index],facsum+index);
        temp.pop_back();//当这条分支结束时,就把它从temp中去除
        DFS(index-1,nowk,sum,facsum);//不选index,因为是从数组末尾开始循环,所以是inde-1;
    }
}

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;
}

A 1091 Acute Stroke

思路:
广度优先遍历

#include <cstdio>
#include <algorithm>
#include <queue>
#include <algorithm>
#include<iostream>
#include<math.h>
using namespace std;

struct Node
{
    int x,y,z;
}node;

int n,m,slice,t;
//矩阵为n*m
//共有slice层
//t为1的下限

int pixel[1290][130][61];
bool inq[1290][130][61]={false};//记录(x,y,z)是否已入过队

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};


//判断坐标(x,y,z)是否需要访问
bool judge(int x,int y,int z)
{
    if(x>=n||x<0||y>=m||y<0||z>=slice||z<0)
        return false;
    if(pixel[x][y][z]==0||inq[x][y][z]==true)
        return false;
    return true;
}

int BFS(int x,int y,int z)
{
    int tot=0;//计数当前块中1的个数
    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",&n,&m,&slice,&t);
    for(int z=0;z<slice;z++)
    {
        for(int x=0;x<n;x++)
        {
            for(int y=0;y<m;y++)
            {
                scanf("%d",&pixel[x][y][z]);

            }
        }
    }
    int ans=0;
     for(int z=0;z<slice;z++)
    {
        for(int x=0;x<n;x++)
        {
            for(int y=0;y<m;y++)
            {
                if(pixel[x][y][z]==1&&inq[x][y][z]==false)
                    ans+=BFS(x,y,z);

            }
        }
    }
    cout<<ans;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值