程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> DP計數(UVA 885&&POJ 2704)

DP計數(UVA 885&&POJ 2704)

編輯:C++入門知識

DP計數(UVA 885&&POJ 2704)



Pascal's Travels Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5258 Accepted: 2363

Description

An n x n game board is populated with integers, one nonnegative integer per square. The goal is to travel along any legitimate path from the upper left corner to the lower right corner of the board. The integer in any one square dictates how large a step away from that location must be. If the step size would advance travel off the game board, then a step in that particular direction is forbidden. All steps must be either to the right or toward the bottom. Note that a 0 is a dead end which prevents any further progress.


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

The input contains data for one to thirty boards, followed by a final line containing only the integer -1. The data for a board starts with a line containing a single positive integer n, 4 <= n <= 34, which is the number of rows in this board. This is followed by n rows of data. Each row contains n single digits, 0-9, with no spaces between them.

Output

The output consists of one line for each board, containing a single integer, which is the number of paths from the upper left corner to the lower right corner. There will be fewer than 263 paths for any board.

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

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.


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.

\epsfbox{p825.eps}

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
#include
#include
#include
#include
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;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved