当前位置:网站首页>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;
}
边栏推荐
- php artisan
- How to specify the execution order for multiple global exception handling classes
- dataworks自定义函数开发环境搭建
- Distributed transactions
- How can the server set up multiple interfaces and install IIS? Tiantian gives you the answer!
- instanceof
- Daily question brushing record (11)
- EasyExcel
- Sorting out the core ideas of the pyramid principle
- Getting started with pytest
猜你喜欢
![[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*](/img/1f/f579110a408c5b5a094733be57ed90.jpg)
[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*

卡特兰数(Catalan)的应用场景

Practice of enterprise ab/testing platform

Asynchronous programming: async/await in asp Net

JMeter test result output

Setting up the development environment of dataworks custom function

These two mosquito repellent ingredients are harmful to babies. Families with babies should pay attention to choosing mosquito repellent products

Notes on the core knowledge of Domain Driven Design DDD

Distributed transactions

2022年华东师范大学计科考研复试机试题-详细题解
随机推荐
Resttemplate configuration use
Software testing assignment - the next day
2022 - 06 - 23 vgmp - OSPF - Inter - Domain Security Policy - nat Policy (Update)
Winter vacation work of software engineering practice
10000小時定律不會讓你成為編程大師,但至少是個好的起點
Centos切换安装mysql5.7和mysql8.0
Ruoyi interface permission verification
Basic components and intermediate components
mysql误删root账户导致无法登录
[classes and objects] explain classes and objects in simple terms
C2338 Cannot format an argument. To make type T formattable provide a formatter<T> specialization:
dataworks自定义函数开发环境搭建
Use the jvisualvm tool ----- tocmat to start JMX monitoring
Basic teaching of crawler code
Abstract learning
Modify MySQL password
Specified interval inversion in the linked list
Reading notes of "learn to ask questions"
Tool class static method calls @autowired injected service
CentOS php7.3 installing redis extensions