1825. 数塔IV

ywy144

新手,可能是dp题,但我不会。dfs刚入门也写不漂亮。

#include <iostream>
#include <vector>
#include <algorithm>
using std::cin; using std::cout; using std::vector; 

void dfs(int row,int col,int mul,const vector<vector<int>>& tower,int height,vector<int>& part_ans){
    if(row==height-1) part_ans.push_back(mul*tower[row][col]%10);
    else{
        dfs(row+1,col,mul*tower[row][col]%10,tower,height,part_ans);
        dfs(row+1,col+1,mul*tower[row][col]%10,tower,height,part_ans);
    }
}
void fun(){
    int i; cin>>i;
    vector<int> ans;
    for(int j=0;j<i;++j){
        int h; cin>>h;
        vector<vector<int>> tower(h,vector<int>(h));
        vector<int> part_ans;
        for(int k=0;k<h;++k)
            for(int l=0;l<=k;++l)
                cin>>tower[k][l];

        int mul=1;
        dfs(0,0,mul,tower,h,part_ans);
        ans.push_back(*std::max_element(part_ans.begin(),part_ans.end()));
    }
    for(int j=0;j<i;++j)
        cout<<ans[j]<<'\n';
}


int main(){
    fun();
    system("pause");
    return 0;
}
你当前正在回复 博客/题目
存在问题!