Sicily 13062. SubDiagonal Paths

  1. SubDiagonal Paths
    Constraints
    Time Limit: 1 secs, Memory Limit: 256 MB
    Description
    You are to find all of the paths on the bottom diagonal of a n x n grid. The path must only go from lefttoright
    or bottomtotop. Given a dimension of the grid (ex. n = 4), specify the number of such paths (ex. solution = 14).
    Input
    The input consists of a single integer n, the dimension of the square grid, 1 <= n <= 30, on each line. A
    zero will indicate the end of the input and should not be processed. Output
    For each input n, you should output the number of paths, as described above, that exist in an nxn grid, one
    per line. Sample Input
    1
    2
  • 6 -
    3
    4
    0
    Sample Output
    1
    2
    5
    14
    将方格中的左方向和下方向分别设为正方向,可以用数组 a[i][j]来表示在位置
    (i,j)走到点(0,0)的方案数,由于其活动范围被限定为下三角,所以当 i>j 的时候,
    那个位置是走不上去的,所以可以令 a[i][j]=0,当 i>j 的时候由于下一步只能往
    上走或者往右走,所以 a[i][j]=a[i-1][j]+a[i]j-1
#include<iostream> 
#include<cstring>
using namespace std;
long long int a[35][35];
int main(){
	int n;
	while(cin>>n&&n!=0){
		memset(a,0,sizeof(a));
		a[0][0]=1;
		for(int i=0;i<=n;i++){
			for(int j=0;j<=n;j++){
				if(i>j){
					a[i][j]=0;
				}
				if(i==0){
					a[i][j]=1;
				}
				if(i<=j&&i!=0){
					a[i][j]=a[i-1][j]+a[i][j-1];
				}
			}
		}
		cout<<a[n][n]<<endl;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值