首页 > 代码库 > DP计数(UVA 885&&POJ 2704)
DP计数(UVA 885&&POJ 2704)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5258 | Accepted: 2363 |
Description
Consider the 4 x 4 board shown in Figure 1, where the solid circle identifies the start position and the dashed circle identifies the target. Figure 2 shows the three paths from the start to the target, with the irrelevant numbers in each removed.
Figure 1 | Figure 2 |
Input
Output
Sample Input
4 2331 1213 1231 3110 4 3332 1213 1232 2120 5 11101 01111 11111 11101 11101 -1
Sample Output
3 0 7
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> typedef long long LL; using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) const int maxn=50; LL dp[maxn][maxn]; int val[maxn][maxn]; char str[maxn]; int n; int main() { std::ios::sync_with_stdio(false); while(cin>>n&&n!=-1) { REPF(i,1,n) { cin>>str; REPF(j,1,n) val[i][j]=str[j-1]-'0'; } CLEAR(dp,0); dp[1][1]=1; REPF(i,1,n) { REPF(j,1,n) { if(val[i][j]==0) continue; if(i+val[i][j]<=n) dp[i+val[i][j]][j]+=dp[i][j]; if(j+val[i][j]<=n) dp[i][j+val[i][j]]+=dp[i][j]; } } cout<<dp[n][n]<<endl; } return 0; }
Walking on the Safe Side
Square City is a very easy place for people to walk around. The two-way streets run North-South or East-West dividing the city into regular blocks. Most street intersections are safe for pedestrians to cross. In some of them, however, crossing is not safe and pedestrians are forced to use the available underground passages. Such intersections are avoided by walkers. The entry to the city park is on the North-West corner of town, whereas the railway station is on the South-East corner.Walking on the Safe Side |
Suppose you want to go from the park to the railway station, and do not want to walk more than the required number of blocks. You also want to make your way avoiding the underground passages, that would introduce extra delay. Your task is to determine the number of different paths that you can follow from the park to the station, satisfying both requirements.
The example in the picture illustrates a city with 4 E-W streets and 5 N-S streets. Three intersections are marked as unsafe. The path from the park to the station is 3 + 4 = 7 blocks long and there are 4 such paths that avoid the underground passages.
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.The first line of the input contains the number of East-West streets W and the number of North-South streets N. Each one of the following W lines starts with the number of an East-West street, followed by zero or more numbers of the North-South crossings which are unsafe. Streets are numbered from 1.
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.The number of different minimal paths from the park to the station avoiding underground passages.
Sample Input
1 4 5 1 2 2 3 3 5 4
Sample Output
4
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> typedef long long LL; using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) int t,w,n; char str[1100]; int mp[1100][1100]; int dp[1100][1100]; int main() { std::ios::sync_with_stdio(false); scanf("%d",&t);int x; while(t--) { scanf("%d%d",&w,&n); getchar(); CLEAR(mp,0); CLEAR(dp,0); REPF(i,1,w) { scanf("%d",&x); gets(str); int len=strlen(str); int cnt=0; REPF(j,0,len) { if(str[j]-'0'>=0&&str[j]-'0'<=9) cnt=cnt*10+str[j]-'0'; else { mp[x][cnt]=1; cnt=0; } } } dp[0][1]=1; REPF(i,1,w) { REPF(j,1,n) { if(mp[i][j]==1) dp[i][j]=0; else dp[i][j]=dp[i-1][j]+dp[i][j-1]; } } printf("%d\n",dp[w][n]); if(t) puts(""); } return 0; }
DP计数(UVA 885&&POJ 2704)