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()