POJ 1664 put Apple "DFS"

Source: Internet
Author: User

Test instructions: Give n apples, m plates, ask how many different Apple placement methods

It can be abstracted as a number n, split into A1,A2,A3,---, am, so that they are N,

Say this topic is the study of PPT inside, it is the idea is to search

Search criteria settings: When placing apples to the K-plate, the number of apples on the K-plate is greater than the number of apples on the k-1 plate, if it is greater, place it on the K-plate, if not greater, continue to place the apples, if the remaining apples are smaller than the apples on the k-1 plate, Stop the search on this branch.

Learning to search is still a misty---

This problem if you simulate the sample is how to search, so understand a little

7 3

8

These are the 8 cases, respectively.

0 0 7

0 1 6

0 2 5

0 3 4

1 1 5

1 2 4

1 3 3

2 2 3

------------------------

#include <iostream>  #include <cstdio>  #include <cstring>  #include <algorithm>  using namespace Std;int num[10010],cnt,n,m;void dfs (int k,int W)//k represents the number of plates that have been used now, w means that several apples have been placed {if (k==m)  // When placed on the last plate to determine {if (n-w>=num[k-1])//If the remaining number of apples at this time is greater than the number of apples on the k-1 plate, then put the remaining apples on the last plate {num[k]=n-w;cnt++;} return;} for (int i=0;i<=n;i++) {if (i>=num[k-1]) {Num[k]=i;w=w+i;k=k+1;dfs (K,W);//Continue to find the next dish on the Apple W=w-i;k=k-1;}} int main () {int ncase;scanf ("%d", &ncase), while (ncase--) {scanf ("%d%d", &n,&m); Cnt=0;dfs (1,0);// At first it was a plate, with 0 apple printf ("%d\n", CNT);}

  

POJ 1664 put Apple "DFS"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.