cf1072D. Minimum path(BFS)
题意
给出一个\(n \times n\)的矩阵,允许修改\(k\)次,求一条从\((1, 1)\)到\((n, n)\)的路径。要求字典序最小
Sol
很显然的一个思路是对于每个点,预处理出从\((1, 1)\)到该点最多能经过多少个\(1\)
然后到这里我就不会做了。。
接下来应该还是比较套路的吧,就是类似于BFS一样,可以枚举步数,然后再枚举向下走了几次,有点分层图的感觉。
/*
*/
#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
//#define int long long
#define LL long long
#define rg register
#define pt(x) printf("%d ", x);
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 2001, INF = 1e9 + 10, mod = 1e9 + 7, B = 1;
const double eps = 1e-9;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, K, vis[MAXN][MAXN], f[MAXN][MAXN];
char s[MAXN][MAXN];
int main() {
N = read(); K = read();
for(int i = 1; i <= N; i++) scanf("%s", s[i] + 1);
for(int i = 1; i <= N; i++) {
for(int j = 1; j <= N; j++) {
f[i][j] = max(f[i - 1][j], f[i][j - 1]);
if(s[i][j] == 'a') f[i][j]++;
if(i + j - 1 - f[i][j] <= K) s[i][j] = 'a';
}
}
putchar(s[1][1]); vis[1][1] = 1;
for(int i = 3; i <= N << 1; i++) {//all step
char now = 'z' + 1;
for(int j = 1; j < i; j++) { // num of down
if(j > N || (i - j > N)) continue;
if(!vis[j - 1][i - j] && !vis[j][i - j - 1]) continue;
now = min(now, s[j][i - j]);
}
putchar(now);
for(int j = 1; j < i; j++) { // num of down
if(j > N || (i - j > N)) continue;
if(!vis[j - 1][i - j] && !vis[j][i - j - 1]) continue;
if(s[j][i - j] == now) vis[j][i - j] = 1;
}
}
return 0;
}
/*
*/
cf1072D. Minimum path(BFS)的更多相关文章
- codeforces 1072D Minimum path bfs+剪枝 好题
题目传送门 题目大意: 给出一幅n*n的字符,从1,1位置走到n,n,会得到一个字符串,你有k次机会改变某一个字符(变成a),求字典序最小的路径. 题解: (先吐槽一句,cf 标签是dfs题????) ...
- 【leetcode】Minimum Path Sum
Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...
- leecode 每日解题思路 64 Minimum Path Sum
题目描述: 题目链接:64 Minimum Path Sum 问题是要求在一个全为正整数的 m X n 的矩阵中, 取一条从左上为起点, 走到右下为重点的路径, (前进方向只能向左或者向右),求一条所 ...
- 【LeetCode练习题】Minimum Path Sum
Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...
- LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II
之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...
- LeetCode: Minimum Path Sum 解题报告
Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...
- [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )
Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...
- 【LeetCode】64. Minimum Path Sum
Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...
随机推荐
- Invalid bound statement (not found): com.taotao.mapper.TbItemMapper.selectByExample: 错误
在Maven工程下,想通过controller接收url中的参数Id查询数据库的数据,并且以json形式显示在页面. 在浏览器输入url后出现的问题: 八月 22, 2016 1:45:22 下午 o ...
- 15、OpenCV Python 轮廓发现
__author__ = "WSX" import cv2 as cv import numpy as np # 基于拓扑结构来发现和绘制(边缘提取) # cv.findConto ...
- JDBC_设计架构_驱动类加载_建立Connection_效率测试
JDBC(Java DataBase Connection) 访问数据库流程:驱动管理器--连接数据库--sql语句--结果集 装载mysql驱动 Class.forName("com.jd ...
- Qt随笔 - QSettings
QSettings类提供了持久的跨平台应用程序设置. 嗯,一句话概括QSettings-- 创建 来看一下原型: QSettings::QSettings(const QString &org ...
- Heap — 20181120
363. Trapping Rain Water public class Solution { /** * @param heights: a list of integers * @return: ...
- H5常见问题 微信踩过得坑
微信页面内 click事件 只在a链接的时候有效,如果是div或者span之类 一定要加上样式 cursor:pointer 点击事件才生效. <div style="cursor: ...
- Jquery 循环内间隔执行 异步执行
<script type="text/javascript"> var arr = new Array(); var arrk = 0; jQuery(function ...
- Merge Sorted Array II
Merge two given sorted integer array A and B into a new sorted integer array. Example A=[1,2,3,4] B= ...
- python 获取子目录下的所有文件的路径
import os pathss=[] for root, dirs, files in os.walk(tarpath): path = [os.path.join(root, name) for ...
- python 爬虫系列07-天气爬虫
看天气 import requests from bs4 import BeautifulSoup ALL_DATA = [] def parse_page(url): headers = { 'Us ...