牛客小白月赛16 E 小雨的矩阵 ( 暴搜)

链接:https://ac.nowcoder.com/acm/contest/949/E
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

小雨有一个 n×nn×n 的矩阵,起点在(1,1),终点在(n,n),只能向下或向右走,且每次只能走 1 步。矩阵上每个点都有一个点权 ai,jai,j 。
求走到终点的路径有多少不同的点权和。

输入描述:

第一行,输入一个正整数 n 。
接下来 n+1 行,每行 n 个数,表示 ai,jai,j。

输出描述:

共一行,输出有多少不同的点权和。
示例1

输入

2
1 5
2 4

输出

2

说明

(1,1)(2,1)(2,2)7(1,1)(1,2)(2,2)10(1,1)→(2,1)→(2,2):和为7。(1,1)→(1,2)→(2,2):和为10。

备注:

1n8,0ai,j50

思路:签到题,暴搜就好了,第一遍没过(在本地能编译到牛客就有错2333),改了好久才改好...

代码:
#include <iostream>
#include <cstdio>

using namespace std;
const int MAXN = 9;
int ans, id, n, a[MAXN][MAXN], b[1100];

void dfs(int x,int y,int temp)
{
    if(x >= n || y >= n)   return;
    if(x == n - 1 && y == n - 1)
    {
        if(b[temp] == 0)
        {
            b[temp] = 1;
            ans ++ ;
        }
        
    }
    temp += a[x][y];
    dfs(x + 1,y,temp);
    dfs(x,y + 1,temp);
}

int main()
{
    cin >> n;
    for(int i = 0; i < n; i ++ )
       for(int j = 0; j < n; j ++ )
          cin >> a[i][j];
          
    dfs(0,0,0);
    cout << ans << endl;
    return 0;
}

 

 
posted @ 2019-07-18 19:02  chuyds  阅读(246)  评论(0编辑  收藏  举报