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

Ideas : Memory search ,f[i][j] Said to i,j Is the maximum number of steps from the starting point , Then the transfer equation can be obtained f[i][j] = max(f[i][j+1],f[i+1][j],f[i-1][j],f[i][j-1])+1.
Code :
#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;
}
边栏推荐
- 2022 East China Normal University postgraduate entrance examination machine test questions - detailed solution
- [Code] occasionally take values, judge blanks, look up tables, verify, etc
- php artisan
- How to specify the execution order for multiple global exception handling classes
- 深度学习参数初始化(一)Xavier初始化 含代码
- UTC time, GMT time, CST time
- “百度杯”CTF比赛 2017 二月场,Web:爆破-1
- Troubleshooting of high CPU load but low CPU usage
- Distributed ID
- Modify MySQL password
猜你喜欢

Use the jvisualvm tool ----- tocmat to start JMX monitoring

HMS core helps baby bus show high-quality children's digital content to global developers

Selenium key knowledge explanation

Distributed transactions

How to migrate or replicate VMware virtual machine systems

Inno Setup 制作安装包

Interfaces and related concepts
![[untitled]](/img/72/4ff9354634342580a135debb58b60e.jpg)
[untitled]

golang操作redis:写入、读取kv数据

Asynchronous programming: async/await in asp Net
随机推荐
Search engine Bing Bing advanced search skills
php artisan
修改MySQL密码
Golang operation redis: write and read kV data
Selenium key knowledge explanation
MySQL installation
Stream stream
dataworks自定义函数开发环境搭建
File links cannot be opened or downloaded in Google browser
Dbnet: real time scene text detection with differentiable binarization
vmware虚拟机C盘扩容
CentOS switches and installs mysql5.7 and mysql8.0
Centos切换安装mysql5.7和mysql8.0
保险公司怎么查高血压?
Software testing assignment - the next day
Troubleshooting of high CPU load but low CPU usage
Understand software testing
10 000 volumes - Guide de l'investisseur en valeur [l'éducation d'un investisseur en valeur]
The essence of interview
卡特兰数(Catalan)的应用场景