Recursion--Algorithm improvement-Super Marie

Source: Internet
Author: User

problem description Everyone knows " Super Marie " is a very good jumping explorer, his forte is jumping, but it can only leap forward one or two steps at a time. Once, he had to pass a long n trail, the path of M traps, these traps are located in the integer position, respectively, is A1,A2,.... am, into which will be dead.  Obviously, if there were two traps next to each other, Mary would have to jump at it anyway. Now give the length of the path n, the number and location of the trap. Find out how many methods of jumping can reach the shore of victory (arrival N), starting at position 1. Input format first behavior two integers n,m second behavior m integer that represents the location of the trap output format an integer. A sample of the number of scenarios that Mary jumps to n input 412 sample output 1 data size and conventions  >=n  >=3, m>=1  n>m; Traps are not located on 1 and N

Code with the problem

#include <iostream> #include <cmath>using namespace std;int n;int a[100];int b[100];int ans=0;void dfs (int i) {if (i==n) {ans++;} else{for  (int j=0;j<pow (2,n); j + +) {if (b[j]!=1) {if (b[j+1]==0) DFS (i+1); Elsedfs (i+2);}}} int main () {int m;cin>>n;cin>>m;for (int i=0;i<m;i++) {cin>>a[i];b[a[i]]=1;} DFS (0); return 0;}

  

Recursion--Algorithm improvement-Super Marie

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.