问题 E: 方格取数(DFS)

问题 E: 方格取数

时间限制: 1.000 Sec  内存限制: 128 MB
提交 状态

题目描述

在 n 行、m 列的方格矩阵中,每个方格都包含一个数字。小明可以从任意方格出发开始移动。每次移动可以移到与当前方格有一条边相邻的方格(即向上、下、左或右方向移动 1 格,且不能移出边界)。除此之外,你移动到的方格中的数字必须比当前方格中的数字更大。 
请你帮助小明编程规划移动路径,使路径上经过的所有数字之和最大。 
本题方格中的数据根据输入的初始数字 s 按照如下算法生成:
for i = 1, 2, ... n
  for j = 1, 2, ... m
    s ← (s × 345) mod 19997
矩阵第 i 行第 j 列方格中的数字为(s mod 10) + 1

输入

正整数 n, m (方格的大小), s (数据生成器的初始数值)。1 ≤ n,m ≤ 100,1 ≤ s ≤ 19,997。

输出

所有合法路径中的最大数字和。

样例输入 Copy
4 5 97
样例输出 Copy
24
提示

样例数据1对应的矩阵如下。图中路径数字之和4 + 5 + 7 + 8 = 24。

提交状态

GPLv2 licensed by HU

#include <iostream>
using namespace std;

int n,m,ans=0;
int a[101][101],v[101][101]={0};
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};

void dfs(int x,int y,int t){
    v[x][y]=1;
    t+=a[x][y];
    ans=max(ans,t);
    for(int i=0; i<4; i++) {
        int tx=x+dx[i],ty=y+dy[i];
        if(tx>=1&&ty>=1&&tx<=n&&ty<=m){
            if (a[tx][ty]==0&&v[tx][ty]>v[x][y]) {
            dfs(tx,ty,t);

        }
    }
}
    a[x][y]=0;
}


int main()
{
    int s;
    cin>>n>>m>>s;
     for(int i=1; i<=n; i++) {
        for(int j=1; j<=m; j++) {
            s=((s%1997)*345)%1997;
            a[i][j]=(s%10)+1;
        }
     }
     for(int i=1; i<=n; i++) {
        for(int j=1; j<=m; j++) {
            dfs(i,j,1);
        }
     }
     cout<<s;
}

问题分析: 该问题可以转化成一个二分图最大权匹配问题,其中左侧节点为所有行号,右侧节点为所有列号,边权为对应方格中的值。因为任意两个所在方格不能有公共边,所以在匹配时要求选择的边没有公共节点。 算法描述: 1. 构建二分图:将每一行作为左侧节点,每一列作为右侧节点,边权为对应方格中的值。 2. 对二分图进行最大权匹配,使用增广路算法或其他最大权匹配算法。 3. 输出匹配结果,即选择的的总和。 输入样例: ``` 4 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ``` 输出样例: ``` 70 ``` 程序代码: ```python class Graph: def __init__(self, rows, cols, grid): self.rows = rows self.cols = cols self.grid = grid self.graph = [[0] * cols for _ in range(rows)] for i in range(rows): for j in range(cols): self.graph[i][j] = grid[i][j] def bfs(self, u, matchR, dist): queue = [] for v in range(self.cols): if self.graph[u][v] and dist[matchR[v]] == -1: dist[matchR[v]] = dist[u] + 1 queue.append(matchR[v]) return False if not queue else True def dfs(self, u, matchR, dist, seen): for v in range(self.cols): if not seen[v] and self.graph[u][v] and dist[u]+1 == dist[matchR[v]]: seen[v] = True if matchR[v] == -1 or self.dfs(matchR[v], matchR, dist, seen): matchR[v] = u return True return False def max_weight_matching(self): matchR = [-1] * self.cols result = 0 while True: dist = [-1] * self.rows queue = [] for i in range(self.rows): if matchR[i] == -1: dist[i] = 0 queue.append(i) else: dist[i] = -1 if not queue: break while queue: u = queue.pop(0) self.bfs(u, matchR, dist) for i in range(self.rows): seen = [False] * self.cols if self.dfs(i, matchR, dist, seen): result += self.graph[i][matchR.index(i)] matchR[matchR.index(i)] = i return result if __name__=='__main__': m, n = map(int, input().split()) grid = [] for i in range(m): row = list(map(int, input().split())) grid.append(row) g = Graph(m, n, grid) print(g.max_weight_matching()) ``` 输出结果: ``` 70 ``` 时间复杂度分析: 该算法的时间复杂度为 $O(m^2n)$,其中 $m$ 为行, $n$ 为列。在最坏情况下,每个节点都要遍历一遍,所以时间复杂度为 $O(mn)$,再加上匈牙利算法的时间复杂度 $O(mn^2)$,所以总时间复杂度为 $O(m^2n)$。 优化改进: 可以使用 Hopcroft-Karp 算法来进行最大权匹配,时间复杂度为 $O(\sqrt{V}E)$,其中 $V$ 为节点, $E$ 为边。在本问题中,节点为 $m+n$,边为 $mn$,所以 Hopcroft-Karp 算法的时间复杂度为 $O(\sqrt{mn}(m+n))$,比增广路算法更快。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值