HDU1428 漫步校园

题目大意

中文题意无须赘述

解题思路

首先,我们可以用广搜把所有点到终点的最短路求出来,然后以这个为条件记忆化搜索就可以了
(因为起点打错调试了好久TAT)

AC代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
using namespace std;
typedef long long ll;
struct node{
    int x, y;
};
node S;
const int maxn = 55;
const int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
ll g[maxn][maxn], dis[maxn][maxn], dp[maxn][maxn];
int n;
bool inMap(int x, int y){
    return x > 0 && x <= n && y > 0 && y <= n;
}
void bfs(){
    queue<node> que;
    S.x = n, S.y = n;
    memset(dis, -1, sizeof(dis));
    dis[n][n] = g[n][n];
    que.push(S);
    while(!que.empty()){
        node u = que.front();
        que.pop();
        int x = u.x, y = u.y;
        for (int i = 0; i < 4; i++){
            int xx = x + dir[i][0];
            int yy = y + dir[i][1];
            if (!inMap(xx, yy)) continue;
            if (dis[xx][yy] == -1 || dis[x][y] + g[xx][yy] < dis[xx][yy]){
                dis[xx][yy] = dis[x][y] + g[xx][yy];
                que.push((node){xx, yy});
            }
        }
    }
}
ll dfs(int x, int y){
    if (dp[x][y]) return dp[x][y];
    for (int i = 0; i < 4; i++){
        int xx = x + dir[i][0];
        int yy = y + dir[i][1];
        if (!inMap(xx, yy)) continue;
        if (dis[xx][yy] < dis[x][y])
            dp[x][y] += dfs(xx, yy);
    }
    return dp[x][y];
}
int main(){
    while(~scanf("%d", &n)){
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                cin >> g[i][j];
        bfs();
        memset(dp, 0, sizeof(dp));
        dp[n][n] = 1;
        ll ans = dfs(1, 1);
        cout << ans << endl;
    }
    return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值