当前位置:网站首页>691. 立方体IV
691. 立方体IV
2022-07-03 07:00:00 【Ray.C.L】

思路:记忆化搜索,f[i][j]表示以i,j为起点的最大步数,那么可得转移方程 f[i][j] = max(f[i][j+1],f[i+1][j],f[i-1][j],f[i][j-1])+1。
代码:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010;
int f[N][N], g[N][N];
int n;
int dx[4] = {
-1, 0, 1, 0}, dy[4] = {
0, 1, 0, -1};
int dp(int x,int y){
int v = f[x][y];
if(v != -1) return v;
v = 1;
for(int i = 0; i < 4; i++){
int a = dx[i] + x, b = dy[i] + y;
if(a >=0 && a < n && b >=0 && b < n && g[a][b] == g[x][y] + 1){
v = max(v, dp(a,b) + 1);
}
}
return v;
}
int main()
{
int T;
scanf("%d", &T);
for(int cases = 1; cases <= T; cases++){
scanf("%d", &n);
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
scanf("%d", &g[i][j]);
memset(f, -1, sizeof f);
int id,cnt = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++){
int t = dp(i,j);
if(t > cnt || t >= cnt && id > g[i][j]){
cnt = t;
id = g[i][j];
}
}
printf("Case #%d: %d %d\n", cases, id, cnt);
}
return 0;
}
边栏推荐
- File links cannot be opened or downloaded in Google browser
- Summary of remote connection of MySQL
- 2022年华东师范大学计科考研复试机试题-详细题解
- 机械观和系统观的科学思维方式各有什么特点和作用
- crontab定时任务
- Software testing assignment - day 1
- DNS forward query:
- Dbnet: real time scene text detection with differentiable binarization
- Unit test framework + Test Suite
- 机器学习 | 简单但是能提升模型效果的特征标准化方法(RobustScaler、MinMaxScaler、StandardScaler 比较和解析)
猜你喜欢

每日刷題記錄 (十一)

On the practice of performance optimization and stability guarantee

Summary of remote connection of MySQL

2022 East China Normal University postgraduate entrance examination machine test questions - detailed solution

Integration test practice (1) theoretical basis

How to specify the execution order for multiple global exception handling classes

Mise en place d'un environnement de développement de fonctions personnalisées

Software testing assignment - the next day

The 10000 hour rule won't make you a master programmer, but at least it's a good starting point

The dynamic analysis and calculation of expressions are really delicious for flee
随机推荐
UTC time, GMT time, CST time
Summary of UI module design and practical application of agent mode
Error c2017: illegal escape sequence
【code】偶尔取值、判空、查表、验证等
Software testing assignment - the next day
Integration test practice (1) theoretical basis
2022 - 06 - 23 vgmp - OSPF - Inter - Domain Security Policy - nat Policy (Update)
POI excel percentage
IC_ EDA_ All virtual machine (rich Edition): questasim, vivado, VCs, Verdi, DC, Pt, spyglass, icc2, synthesize, innovative, ic617, mmsim, process library
修改MySQL密码
Jenkins
[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*
MySQL installation
Setting up the development environment of dataworks custom function
Unit test framework + Test Suite
万卷书 - 价值投资者指南 [The Education of a Value Investor]
Class and object summary
Tool class static method calls @autowired injected service
RestHighLevelClient获取某个索引的mapping
[Code] if (list! = null & list. Size() > 0) optimization, set empty judgment implementation method