当前位置:网站首页>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;
}
边栏推荐
- DNS forward query:
- dataworks自定义函数开发环境搭建
- php安装composer
- 每日刷題記錄 (十一)
- These two mosquito repellent ingredients are harmful to babies. Families with babies should pay attention to choosing mosquito repellent products
- Software testing assignment - day 3
- [Fiddler actual operation] how to use Fiddler to capture packets on Apple Mobile Phones
- Pytest -- write and manage test cases
- 保险公司怎么查高血压?
- 服务器如何设置多界面和装IIS呢?甜甜给你解答!
猜你喜欢
Journal quotidien des questions (11)
The 10000 hour rule won't make you a master programmer, but at least it's a good starting point
Inno setup production and installation package
机器学习 | 简单但是能提升模型效果的特征标准化方法(RobustScaler、MinMaxScaler、StandardScaler 比较和解析)
[Fiddler actual operation] how to use Fiddler to capture packets on Apple Mobile Phones
2022-06-23 VGMP-OSPF-域間安全策略-NAT策略(更新中)
My 2020 summary "don't love the past, indulge in moving forward"
Practice of enterprise ab/testing platform
10000小时定律不会让你成为编程大师,但至少是个好的起点
The pressure of large institutions in the bear market has doubled. Will the giant whales such as gray scale, tether and micro strategy become 'giant thunder'?
随机推荐
[Fiddler actual operation] how to use Fiddler to capture packets on Apple Mobile Phones
Climb movie paradise 2021 hot
The essence of interview
Shim and Polyfill in [concept collection]
Specified interval inversion in the linked list
每日刷題記錄 (十一)
Strategy mode
Software testing assignment - the next day
vmware虚拟机C盘扩容
Thoughts on project development
VMware virtual machine C disk expansion
IC_ EDA_ All virtual machine (rich Edition): questasim, vivado, VCs, Verdi, DC, Pt, spyglass, icc2, synthesize, innovative, ic617, mmsim, process library
Practice of enterprise ab/testing platform
JUC forkjoinpool branch merge framework - work theft
Journal quotidien des questions (11)
Search engine Bing Bing advanced search skills
Laravel Web框架
These two mosquito repellent ingredients are harmful to babies. Families with babies should pay attention to choosing mosquito repellent products
php artisan
The dynamic analysis and calculation of expressions are really delicious for flee