zoukankan      html  css  js  c++  java
  • Xenia and Weights(深度优先搜索)

    Xenia and Weights
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.

    Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the(i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.

    You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on ​​the scales or to say that it can't be done.

    Input

    The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).

    Output

    In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.

    If there are multiple solutions, you can print any of them.

    数据量小,直接搜索即可

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <queue>
     4 #include <vector>
     5 #include <utility>
     6 #include <cstdio>
     7 #include <cstring>
     8 
     9 using namespace std;
    10 
    11 char s[12];
    12 vector<int> ans;
    13 int m;
    14 
    15 bool dfs(int n, int rw, int lw, bool rig)
    16 {
    17     if(n == 0) return true;
    18     for(int i = 1; i <= 10; i++){
    19         if(s[i] == '1'){
    20             if(!ans.empty() && ans.back() == i) continue;
    21             else{
    22                 if(rig){
    23                     if(rw + i > lw){
    24                         ans.push_back(i);
    25                         if(dfs(n - 1, rw + i, lw, false)) return true;
    26                         ans.pop_back();
    27                     }
    28                 } else {
    29                     if(lw + i > rw){
    30                         ans.push_back(i);
    31                         if(dfs(n - 1, rw, lw + i, true)) return true;
    32                         ans.pop_back();
    33                     }
    34                 }
    35             }
    36         }
    37     }
    38     return false;
    39 }
    40 
    41 int main()
    42 {
    43     while(scanf("%s", s + 1) != EOF){
    44         scanf("%d", &m);
    45         ans.clear();
    46         if(dfs(m, 0, 0, true)){
    47             puts("YES");
    48             vector<int>::iterator it = ans.begin();
    49             printf("%d", *it);
    50             for(it++; it != ans.end(); it++)
    51                 printf(" %d", *it);
    52             puts("");
    53         }
    54         else puts("NO");
    55     }
    56     return 0;
    57 }
    View Code
  • 相关阅读:
    免费i.MX8M mini开发板活动又来了!
    超低延时4K时代来临!米尔基于Zynq UltraScale+MPsoc边缘视觉套件VECP发布
    开发者福利!百问I.MX6ULL裸机文档发布
    看充电桩计费控制单元如何助力“新基建”?
    更加精确评估ARM IP的模型工具——ARM Cycle Models
    Arm Development Studio 最新版本2020.0 发布!
    STM32MP1开发板免费申请!动起来
    百度&米尔携手推出FZ3深度学习计算卡! 基于XCZU3EG的百度大脑EdgeBoard加速平台
    支持python的米尔PYNQ开发板来了
    静待花开,米尔同行:Xilinx下载线复工特惠仅要58元!最低1元可购
  • 原文地址:https://www.cnblogs.com/cszlg/p/3287220.html
Copyright © 2011-2022 走看看