当前位置:网站首页>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;
}
边栏推荐
- Book recommendation~
- Winter vacation work of software engineering practice
- UTC时间、GMT时间、CST时间
- Laravel框架 踩坑(一)
- 利用C#实现Pdf转图片
- The essence of interview
- Distributed transactions
- Strategy mode
- Operation principle of lua on C: Foundation
- These two mosquito repellent ingredients are harmful to babies. Families with babies should pay attention to choosing mosquito repellent products
猜你喜欢

VMware virtual machine C disk expansion

Ruoyi interface permission verification
![[Fiddler problem] solve the problem about Fiddler's packet capturing. After the mobile network is configured with an agent, it cannot access the Internet](/img/9d/42dfef67246740f0dba0c6d8f1b625.jpg)
[Fiddler problem] solve the problem about Fiddler's packet capturing. After the mobile network is configured with an agent, it cannot access the Internet

熊市里的大机构压力倍增,灰度、Tether、微策略等巨鲸会不会成为'巨雷'?

Basic components and intermediate components

Inno Setup 制作安装包

Personally design a highly concurrent seckill system

2022 - 06 - 23 vgmp - OSPF - Inter - Domain Security Policy - nat Policy (Update)

Distributed transactions

On the practice of performance optimization and stability guarantee
随机推荐
Sorting out the core ideas of the pyramid principle
Crontab scheduled task
萬卷書 - 價值投資者指南 [The Education of a Value Investor]
EasyExcel
Basic components and intermediate components
Liang Ning: 30 lectures on brain map notes for growth thinking
UTC时间、GMT时间、CST时间
La loi des 10 000 heures ne fait pas de vous un maître de programmation, mais au moins un bon point de départ
How can I split a string at the first occurrence of “-” (minus sign) into two $vars with PHP?
instanceof
Ruoyi interface permission verification
Specified interval inversion in the linked list
HMS core helps baby bus show high-quality children's digital content to global developers
Golang operation redis: write and read hash type data
Software testing learning - day one
Dbnet: real time scene text detection with differentiable binarization
Abstract learning
centos php7.2.24升级到php7.3
JMeter test result output
php artisan