链接:https://ac.nowcoder.com/acm/contest/949/E
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
小雨有一个 n×nn \times nn×n 的矩阵,起点在(1,1),终点在(n,n),只能向下或向右走,且每次只能走 1 步。矩阵上每个点都有一个点权 ai,ja_{i,j}ai,j 。
求走到终点的路径有多少不同的点权和。
输入描述:
第一行,输入一个正整数 n 。 接下来 n+1 行,每行 n 个数,表示 ai,ja_{i,j}ai,j。
输出描述:
共一行,输出有多少不同的点权和。
示例1
输入
复制
2 1 5 2 4
输出
复制
2
说明
(1,1)→(2,1)→(2,2):和为7。(1,1)→(1,2)→(2,2):和为10。(1,1) \rightarrow (2,1) \rightarrow (2,2):和为 7 。 (1,1) \rightarrow (1,2) \rightarrow (2,2):和为 10 。(1,1)→(2,1)→(2,2):和为7。(1,1)→(1,2)→(2,2):和为10。
备注:
1≤n≤8,0≤ai,j≤501 \leq n \leq 8,0 \leq a_{i,j} \leq 501≤n≤8,0≤ai,j≤50
#include<bits/stdc++.h>
using namespace std;
int a[10][10], vis[400], n, ans;
void deep( int x , int y , int sum)
{
if( x > n || y > n ) return ;
if( x == n && y == n )
{
if( vis[sum] == 0 )
vis[sum] = 1, ans++;
return ;
}
deep(x+1, y, sum + a[x+1][y]);
deep(x, y+1, sum + a[x][y+1]);
}
int main()
{
cin>>n;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
cin >> a[i][j];
deep( 1, 1, a[1][1] );
cout << ans;
return 0;
}