P1035 I need help - Smart Online Judge

题目ID:1035

题目名称:I need help

有效耗时:31 ms

空间消耗:508 KB

程序代码:

#include<iostream>
#include<vector>
using namespace std;

vector<int> index[10];
vector<int> value[10];
int f[12][12];
int n,m;

bool has(){
    for(int i=0;i<value[n-1].size();i++){
        if(value[n-1][i]==m)
            return true;
    }
    return false;
}

bool cal(){

    for(int i=0;i<n;i++){
        while(!index[i].empty()){
            index[i].pop_back();
        }
        while(!value[i].empty()){
            value[i].pop_back();
        }
        int min=65536;
        for(int j=0;j<=i;j++){
            if(i==0){
                index[i].push_back(j);
                value[i].push_back(f[i][j]);
            }
            else{
                for(int k=0;k<index[i-1].size();k++){
                    if(index[i-1][k]==j||index[i-1][k]==j-1){
                        if(value[i-1][k]+f[i][j]<=m){//如果和小于m
                            index[i].push_back(j);
                            value[i].push_back(value[i-1][k]+f[i][j]);
                        //    cout<<value[i-1][k]+f[i][j]<<" ";
                        }
                        if(value[i-1][k]+f[i][j]<min){
                            min=value[i-1][k]+f[i][j];
                        }
                    }
                }

            }
        }//for
        if(i!=0){
            if(min>m){
                return false;
            }
            if(index[i].size()==0)
                return false;

        }
    //    cout<<endl;

    }//for
    return true;

}

int main(){
    int t;
    cin>>t;
    while(t--){
        cin>>n>>m;
        for(int i=0;i<n;i++){
            for(int j=0;j<=i;j++){
                int a;
                cin>>a;
                f[i][j]=a;
            }
        }//for
        if(cal()&&has()){
            cout<<"Yes"<<endl;
        }else
            cout<<"No"<<endl;

    }
//    system("pause");
    return 0;

}

题目描述

Johnny Q在你的帮助下终于进入了城堡,现在出现在他面前的是一条恐怖的黑水河。河中有大量传说中的食人怪兽------法克鱿,同时还有一个N层正三角梅花桩阵,每个桩上都印有一个数字,如图所示是一个4层的正三角梅花桩阵.

Johnny Q只能从离他最近的即这个三角木桩阵的最上面一个木桩开始一个桩一个桩的跳到对岸去,每次他只能向左下或右下跳一次,跳的距离只能是一个单位步长,比如最上面的7,只能跳到3和8,而3又只能跳到4和1.跳这样的木桩对身手矫健的Johnny Q当然是小菜一碟.但是CK也不是盏省油的灯,要想跳过和还有个要求,那就是从你第一个桩跳到最后一个桩,所经过桩上的数字之和必须要等于M,否则就算跳到了最后一层的桩上,这个桩也会沉下去.比如M=21时,图中7->3->1->10是一条合法的路径,7->3->4->7也是一条合法的路径.现在你需要帮助Johnny Q判断是否存在这样的路径。

输入格式

输入的第一行是一个整数T,代表有T组测试数据.
每组测试数据的一行是两个整数N, M.其中N代表梅花桩的层数(2<=N<=10), M代表合法路径的数字和.
接下来有N行,第i行有i个数,代表这个N层梅花阵每层的数字,每个数字不会超过100.

输出格式

对于每组测试数据,输出Yes或者No,代表是否存在这样的路径。

样例输入

2

4 21
7
3 8
4 1 5
7 9 10 2

4 15
7
3 8
4 1 5
7 9 10 5

样例输出

Yes
No

数据范围与提示

时间: 2024-08-27 07:25:07

P1035 I need help - Smart Online Judge的相关文章

P1154 采药 - Smart Online Judge

题目ID:1154 题目名称:采药 有效耗时:0 ms 空间消耗:4436 KB 程序代码: 1 #include<iostream> 2 #include<string> 3 #include<vector> 4 using namespace std; 5 6 int f[1001][1001]; 7 8 int main(){ 9 for(int i=0;i<1001;i++) 10 for(int j=0;j<1001;j++){ 11 f[i][j

P1015 笨小猴 - Smart Online Judge

题目ID:1015 题目名称:笨小猴 有效耗时:390 ms 空间消耗:1832 KB 程序代码: 1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStreamReader; 4 import java.util.Scanner; 5 6 public class Main { 7 static int m; 8 static boolean isPrime(int a){

P1032 合并果子 - Smart Online Judge

本题采用哈夫曼编码的思路,采用贪心算法实现. 题目ID:1032 题目名称:合并果子 有效耗时:4325 ms 空间消耗:948 KB 程序代码: 1 #include<iostream> 2 #include<list> 3 #include<algorithm> 4 5 using namespace std; 6 7 list<int> lt; 8 9 10 11 //哈夫曼编码方法 12 long long haff(){ 13 long long

P1177 公路乘车 - Smart Online Judge

简单的动态规划 题目ID:1177 题目名称:公路乘车 有效耗时:15 ms 空间消耗:516 KB 程序代码: 1 #include<iostream> 2 using namespace std; 3 4 int f[11]; 5 int g[102]; 6 int main(){ 7 for(int i=1;i<=10;i++){ 8 cin>>f[i]; 9 } 10 for(int i=1;i<=102;i++){ 11 g[i]=0; 12 } 13 14

P1163 第K极值 - Smart Online Judge

题目ID:1163 题目名称:第K极值 有效耗时:496 ms 空间消耗:740 KB 程序代码: 1 #include<iostream> 2 #include<vector> 3 #include<algorithm> 4 using namespace std; 5 6 vector<long long> list; 7 8 bool isprime(long long a){ 9 if(a<2) 10 return false; 11 if(a

P1016 [NOIP2008T2]火柴棒等式 - Smart Online Judge

不罗嗦,简单暴力枚举,由于输入规模比较小,希望能有更好的算法. 题目ID:1016 题目名称:[NOIP2008T2]火柴棒等式 有效耗时:932 ms 空间消耗:1872 KB 程序代码: 1 import java.util.Scanner; 2 3 4 public class Main { 5 static int[] a={6,2,5,5,4,5,6,3,7,6,2,2}; 6 7 static int getsum(int n){ 8 int sum=0; 9 for(int i=0

Bayesian optimisation for smart hyperparameter search

Bayesian optimisation for smart hyperparameter search Fitting a single classifier does not take long, fitting hundreds takes a while. To find the best hyperparameters you need to fit a lot of classifiers. What to do? This post explores the inner work

我所追求的简约、便携、极简的未来生活——智星空移动迷你Smart PC开箱上手体验

很奇怪,我们不屑与他人为伍,却害怕自己与众不同.——By保罗.柯艾略 我从小喜欢新奇的事物,喜欢尝试一些稀奇古怪的东西.在科技迅猛发展的这个时候,我在数码产品方面的兴趣从未减退,从以前的BB机到如今的肾6,我从未停下追随的脚步.今天给大家带来的是一款革命性的产品,它将PC做到了极致,在这个新奇事物如雨后春笋一般的时代中,脱颖而出.这就是我今天要介绍的,智星空Smart PC,全球最小mini主机. 深圳智星空技有限公司(ZXK)是中国唯一专注于微型电脑主机研发与生产的品牌. 专注于智能化.微型化

657. Judge Route Circle 判断线路成圆

Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place. The move sequence is represented by a string. And each move is represent by a characte