Visit-Dynamic planning

Source: Internet
Author: User

Now there is a city sales manager, need to start from the company, to visit the city's business, know his location and the location of the business, but because of the city road traffic, he can only choose a direction in the left and right, choose a direction in the top and bottom, and now ask him how many kinds of solutions to reach the merchant address.

Given a map and its long width n and M, where 1 represents a manager position, 2 represents a business location, 1 represents an area that cannot be passed, and 0 represents an area that can be passed, please return the number of scenarios to ensure that there must be a legal path. Ensure that the length and width of the matrix are less than or equal to 10.

Test examples:
[[0,1,0],[2,0,0]],2,3
Returns: 2


1 Lass Visit {2  Public:3     4    5     intCountpath (vector<vector<int> > Map,intNintm) {6         //Write code here7         intx1,y1;8         intX2,y2;9          for(intI=0; i<n;i++)Ten         { One              for(intj=0; j<m;j++) A             { -                 if(map[i][j]==1) -                 { thex1=i; -y1=J; -                 } -                 if(map[i][j]==2) +                 { -X2=i;y2=J; +                 } A             } at         } -         if(x1==x2&&y1==y2) -             return 0; -         intXt=x1<x2?1:-1; -         intYt=y1<y2?1:-1; -vector<vector<int&GT;&GT;DP (n,vector<int> (M,1)); in          for(intI=x1+xt;i!= (X2+XT); i+=XT) -         { todp[i][y1]=map[i][y1]==-1?0:d p[i-XT] [Y1]; +         } -          for(intJ=y1+yt;j!= (Y2+YT); j+=YT) the         { *dp[x1][j]=map[x1][j]==-1?0:d p[x1][j-YT]; $         }Panax Notoginseng          for(intI=x1+xt;i!= (X2+XT); i+=XT) -        { the              for(intJ=y1+yt;j!= (Y2+YT); j+=YT) +             { Adp[i][j]=map[i][j]==-1?0:d p[i-xt][j]+dp[i][j-YT]; the             } +         } -         returnDp[x2][y2]; $     } $};

Matrix 1

0 0 1) 0 0

0 0 0) 0 0

0 0 0) 0 0

0 0 0) 0 0

0 0 0) 2 0

Dp

0 0 1 1 1 0

0 0 1 0 0 0

0 0 1 0 0 0

0 0 1 0 0 0

0 0 1 0 0 0

DP[I][J]=DP[I-1][J]+DP[I][J-1]

0 0 1 1 1 0

0 0 1 2 3 0

0 0 1 3 6 0

0 0 1 4 10 0

0 0 1 5 15 0

Visit-Dynamic planning

Related Article

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.