题目传送门

题目大意:

  给出一幅n*n的字符,从1,1位置走到n,n,会得到一个字符串,你有k次机会改变某一个字符(变成a),求字典序最小的路径。

题解:

  (先吐槽一句,cf 标签是dfs题????)

  这道题又学到了,首先会发现,从原点出发,走x步,所有的情况都是在一条斜线上的,而再走一步就是下一条斜线。所以用两个队列进行bfs(把当前步和下一步要处理的字符分开)。

  到了这里思路就明朗了,每次走的时候如果本身的map里是a就直接走,不是a就看k是否大于0,再看这个字符是不是比答案串里对应位置的字符小,这样bfs主体就搭建完成了。

  但是优秀的bfs显然要考剪枝(Orz)。

  首先是一个保证正确性的剪枝,由于我们会更新某一个位子的字符,(c -> b -> a),但是修改的过程中前面那些并不是最优状态也被更新了,会导致答案错误,所以我采取的方法是记录每一个状态的字符,和答案串比较,是否一样,如果不一样,则代表答案串不是由这个状态变来的,continue掉。

  其次是一个会影响时间的剪枝,由于到一个位子的k有很多种情况,显然我们应该保存k最大的那种情况,而k比较小的那些情况其实是冗余的,严重超时,所以我们每次更新答案串的时候,还应该更新一下 保证答案最优的情况下走到每个位子最大的k是多少。当某一个状态front出来的时候,先比较是否k是最大(最优)的,如果不是,也continue。

  不过还有一个剪枝,应该是k>=2*n-1就可以直接输出aaaaa了,但是看时间似乎没必要。两个优先队列就用 f^1的方法就可以变化了。

//#pragma comment(linker,"/STACK:102400000,102400000")
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<stdlib.h>
//#include<unordered_map>
#define lson l,mid,rt<<1
#define rson mid+1,r,(rt<<1)|1
#define CLR(a,b) memset(a,b,sizeof(a))
#define mkp(a,b) make_pair(a,b)
typedef long long ll;
using namespace std;
inline int read(){
int x=,f=;
char ch=getchar();while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;}
const int maxn=1e6+;
struct node{
int x,y,step;
char pre;
friend bool operator<(const node &a,const node &b)
{
return a.step<b.step;
}
};
char ans[],mp[][];
int mk[][];
int pos,n,k;
queue<node>q[];
int fx[][]={{,},{,}};
inline bool check(node &s)
{
if(s.x>n||s.y>n)return false;
if(mp[s.x][s.y]=='a'){
ans[pos]='a';
s.pre='a';
return true;
}
if(s.step>){
s.pre='a';
ans[pos]='a';
//printf("ans[%d]:%c\n",pos,ans[pos]);
s.step--;
return true;
}
if(ans[pos]>=mp[s.x][s.y]){
ans[pos]=mp[s.x][s.y];
s.pre=ans[pos];
return true;
}
return false;
}
inline void bfs()
{
int f=;
while(!q[f].empty())
{
node s=q[f].front();
q[f].pop(); if(s.x==s.y&&s.x==n)continue;
if(s.pre==ans[pos-]&&mk[s.x][s.y]==s.step){
for(int i=;i<;i++){
node ed=s;
ed.x+=fx[i][];
ed.y+=fx[i][];
if(check(ed)){
if(ed.step>mk[ed.x][ed.y]){
mk[ed.x][ed.y]=ed.step;
q[f^].push(ed);
}
}
}
} if(q[f].empty()){
f=f^;
pos++;
ans[pos]=(char)('z'+);
}
}
}
int main(){
cin>>n>>k;
CLR(mk,-);
for(int i=;i<=n;i++)
{
scanf("%s",mp[i]+);
}
if(mp[][]!='a'&&k>){
k--;
mp[][]='a';
}
ans[++pos]=mp[][];
pos++;
ans[pos]='z'+;
mk[][]=k;
q[].push({,,k,ans[pos-]});
bfs();
for(int i=;i<pos;i++)
{
printf("%c",ans[i]);
}
}
D. Minimum path
time limit per test

1.5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a matrix of size n×nn×n filled with lowercase English letters. You can change no more than kk letters 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−12n−1.

Find the lexicographically smallest string that can be associated with a path after changing letters in at most kk cells of the matrix.

A string aa is lexicographically smaller than a string bb, if the first different letter in aa and bb is smaller in aa.

Input

The first line contains two integers nn and kk (1≤n≤20001≤n≤2000, 0≤k≤n20≤k≤n2) — the size of the matrix and the number of letters you can change.

Each of the next nn lines contains a string of nn 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 kk letters in the matrix.

Examples
input

Copy
4 2
abcd
bcde
bcad
bcde
output

Copy
aaabcde
input

Copy
5 3
bwwwz
hrhdh
sepsp
sqfaf
ajbvw
output

Copy
aaaepfafw
input

Copy
7 6
ypnxnnp
pnxonpm
nxanpou
xnnpmud
nhtdudu
npmuduh
pmutsnz
output

Copy
aaaaaaadudsnz
Note

In the first sample test case it is possible to change letters 'b' in cells (2,1)(2,1) and (3,1)(3,1) to 'a', then the minimum path contains cells (1,1),(2,1),(3,1),(4,1),(4,2),(4,3),(4,4)(1,1),(2,1),(3,1),(4,1),(4,2),(4,3),(4,4). The first coordinate corresponds to the row and the second coordinate corresponds to the column.

codeforces 1072D Minimum path bfs+剪枝 好题的更多相关文章

  1. HDU6223 Infinite Fraction Path bfs+剪枝

    Infinite Fraction Path 这个题第一次看见的时候,题意没搞懂就没做,这第二次也不会呀.. 题意:第i个城市到第(i*i+1)%n个城市,每个城市有个权值,从一个城市出发走N个城市, ...

  2. 2017沈阳区域赛Infinite Fraction Path(BFS + 剪枝)

    Infinite Fraction Path Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java ...

  3. Vladik and Favorite Game CodeForces - 811D (思维+BFS+模拟+交互题)

    D. Vladik and Favorite Game time limit per test 2 seconds memory limit per test 256 megabytes input ...

  4. POJ 3126 Prime Path (BFS+剪枝)

    题目链接:传送门 题意: 给定两个四位数a.b,每次能够改变a的随意一位.而且确保改变后的a是一个素数. 问最少经过多少次改变a能够变成b. 分析: BFS,每次枚举改变的数,有一个剪枝,就是假设这个 ...

  5. cf1072D. Minimum path(BFS)

    题意 题目链接 给出一个\(n \times n\)的矩阵,允许修改\(k\)次,求一条从\((1, 1)\)到\((n, n)\)的路径.要求字典序最小 Sol 很显然的一个思路是对于每个点,预处理 ...

  6. Codeforces gym101755H Safe Path(bfs)

    题意: 给以一个网格图,有起点终点和一些怪兽,可以上下左右走,不能走到距离怪兽曼哈顿距离为d以内的地方,问到终点最短路径 n*m<=2e5,d<=2e5 思路: 因为n*m的范围,不能直接 ...

  7. 刷题64. Minimum Path Sum

    一.题目说明 题目64. Minimum Path Sum,给一个m*n矩阵,每个元素的值非负,计算从左上角到右下角的最小路径和.难度是Medium! 二.我的解答 乍一看,这个是计算最短路径的,迪杰 ...

  8. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

  9. LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II

    之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...

随机推荐

  1. 使用递归函数,输出n个元素的所有子集

    题目描述: 请编写一个递归函数,用来输出n个元素的所有子集.例如,三个元素{a,b,c}的所有子集是:{},{a},{b},{c},{a,c},{ac},{b,c},{a,b,c}. 解题思路: 根据 ...

  2. IDEA03 连接数据库、自动生成实体类

    1 版本说明 JDK:1.8 MAVEN:3.5 SpringBoot:2.0.4 IDEA:旗舰版207.2 MySQL:5.5 2 利用IDEA连接数据库 说明:本案例以MySQL为例 2.1 打 ...

  3. Docker学习笔记_删除某个镜像

    实验:删除某个镜像 sudo docker rmi [Image ID] 1.查看镜像的ID                  sudo docker images 2.删除镜像            ...

  4. Mips下交叉编译dropbear

    1. 编译zlib-1.2.8 在编译dropbear的时候,会遇到“configure: error: *** zlib missing - install first or check confi ...

  5. 19、SOAP安装,运用与比对结果解释

    转载:http://www.dengfeilong.com/post/Soap2.html https://blog.csdn.net/zhu_si_tao/article/details/71108 ...

  6. vue.js的生命周期 及其created和mounted的部分

    网上很多人有所总结,转载自: https://segmentfault.com/a/1190000008570622   关于created和mounted的部分,也可以参考: https://blo ...

  7. 内存中DataTable去除重复行

    删除内存中DataTable表的重复行 假设在内存中(不是数据库中)有两个表: 表一:TableA Name Phone 张三 123456 李四 123457 王五 1234568 表二:Table ...

  8. 【原创】ListView快速滚动至新添加一行(自动滚动)

    在C#开发中我们经常要开发一些日志系统,尤其是基于ListView的日志显示系统.但是当日志增多是你是否有一些困扰,就是它为什么不会自动滚动至最后一行. 以下是一小段代码,希望可以帮助你. publi ...

  9. JavaScript 的 export default 命令

    export default 指定模块的默认输出,一个模块只能有一个默认输出. 举个例子. export-default.js export default { name: 'hello', data ...

  10. 时间日期控件的处理-Selenium

    很多人问时间日期的空间怎么处理,但是时间日期控件各种各样,你可能遇到正常点的像这样: 当然也可能遇到难点的,像这样: 当然,也不排除会遇到变态的,像这样: 呵呵,真要一个个想着怎么去选择,简直是非人类 ...