DP count (UV 885 & amp; POJ 2704)

Source: Internet
Author: User

DP count (UV 885 & amp; POJ 2704)


Pascal's Travels
Time Limit:1000 MS Memory Limit:65536 K
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 wocould advance travel off the game board, then a step in that Fig 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 4x4 board shown in Figure 1, where the solid cirfies 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

423311213123131104333212131232212051110101111111111110111101-1

Sample Output

307

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

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
14 512 23 3 54

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


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.