题目传送门

题目大意:

  给出一幅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. zend studio在线安装svn的插件

    这个,其实真的很简单 ,但是让我纠结了很久. 安装步骤: 1.确保电脑能够上网: 2.打开zend studio,然后选择help->welcome会弹出一个欢迎页面; 3.等待几秒后可以看到右 ...

  2. AntD01 Angular2整合AntD、Angular2整合Material、利用Angular2,AntD,Material联合打造后台管理系统 ???

    待更新... 2018-5-21 13:53:52 1 环境说明 2 搭建Angular项目 详情参见 -> 点击前往 辅助技能 -> 点击前往 3 创建共享模块 ng g m share ...

  3. 1、python与ipython的下载与安装

    1.ipython的下载与安装 下载链接: wget -c https://github.com/downloads/ipython/ipython/ipython-0.13.1.tar.gz ##下 ...

  4. PHP中static与self

    一直搞不清楚,今天百度自己也测试了一下. <?php class A { public static function closure(){ echo __CLASS__."<b ...

  5. TP5图片上传

    /*图片上传*/ public function upload(){ // 获取表单上传文件 例如上传了001.jpg $file = request()->file('file'); // 移 ...

  6. JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(1):Mybatis和Hibernate概念理解

    一.关键字说明: oop:面向对象 aop:面向切面 ioc:控制反转 orm:对象关系映射 pojo:数据库表映射的java实体类 二.常识说明:1.hibernate和mybatis都属于持久层. ...

  7. 关于Pascal(帕斯卡)以及Camel(驼峰)命名法

    小驼峰式命名法(lower camel case): 第一个单字以小写字母开始:第二个单字的首字母大写,例如:firstName.lastName,也被称为Camel命名法. 大驼峰式命名法(uppe ...

  8. Entity Framework快速入门--直接修改(简要介绍ObjectContext处理机制)

    在介绍Entity Framework的修改实体到数据库的方法之前呢,我们先简要的介绍一下ObjectContext的处理机制. 1.ObjectContext的处理机制 ObjectContext是 ...

  9. MongoDB整理笔记のGUI操作

    值得幸运的是,其实MongoDB也有像类似于PL/SQL一样的界面操作工具操作MongoDB. 下面就来介绍几款不同的界面工具,大家各取所需! MongoVUE 主页:http://www.mongo ...

  10. 【C#】CLR内存那点事(string)

    string是比特殊的类,说引用类型,但不存在堆里面,而且String str=new String("HelloWorld")这样的重装也说没有的. 我们先来看一个方法 clas ...