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>>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