标签:des style blog http io color ar os sp
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5258 | Accepted: 2363 |
Description
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 |
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.
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.
The number of different minimal paths from the park to the station avoiding underground passages.
1 4 5 1 2 2 3 3 5 4
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; }
标签:des style blog http io color ar os sp
原文地址:http://blog.csdn.net/u013582254/article/details/41121283