Problem
You are given a matrix of size n×n filled with lowercase English letters. You can change no more than kletters in this matrix.
Consider all paths from the upper left corner to the lower right corner that move from a cell to its neighboring cell to the right or down. Each path is associated with the string that is formed by all the letters in the cells the path visits. Thus, the length of each string is 2n−1
Find the lexicographically smallest string that can be associated with a path after changing letters in at most k cells of the matrix.A string ais lexicographically smaller than a string b, if the first different letter in a and b is smaller in a
Input
The first line contains two integers nand k (1≤n≤2000, 0≤k≤n2) — the size of the matrix and the number of letters you can change.Each of the next n lines contains a string of n lowercase English letters denoting one row of the matrix.
Output
Output the lexicographically smallest string that can be associated with some valid path after changing no more than k letters in the matrix.
题意:有一个n*n的矩阵,有k次将字母变a的机会,从左上角到右下角,每次只能右移或下移,求一个字典序最小的轨迹
字典序最小,贪心的想那一定是尽可能将多的a安排在轨迹靠前的位置,那我们就预处理出靠前位置的所有a,直接将该位置换成a即可,之后就走一步贪一步就好
AC代码
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 2005;
char s[maxn][maxn];
int vis[maxn][maxn];
int f[maxn][maxn];
int n, k;
int main()

这篇博客介绍了一个关于矩阵的算法问题,矩阵大小为n×n,允许改变不超过k个字母。任务是找到从左上角到右下角路径中字典序最小的字符串。解决方案是通过贪心策略,首先找出路径上的所有可能的a,然后在允许的更改次数内将其替换为a,从而确保字典序最小。
最低0.47元/天 解锁文章
1017

被折叠的 条评论
为什么被折叠?



