当前位置:网站首页>691. 立方体IV
691. 立方体IV
2022-07-27 08:37:00 【追寻远方的人】
Vincenzo 决定制作立方体 IV,但所有预算只够制作一个正方形迷宫。
它是一个完美的迷宫,每个房间都呈正方形,并具有 4 扇门(四个边一边 1 个)。
每个房间里都有一个号码。
一个人只有在下一个房间的号码比当前房间的号码大 1 的情况下,才能从当前房间移动到下一个房间。
现在,Vincenzo 为所有房间分配了唯一的号码(1,2,3,…S2)然后将 S2 个人放在了迷宫中,每个房间 1 个,其中 S 是迷宫的边长。
能够移动次数最多的人将获胜。
弄清楚谁将成为赢家,以及他将能够到达的房间数量。
输入格式
第一行包含整数 T,表示共有 T 组测试数据。
每组测试数据第一行包含整数 S,表示迷宫的边长。
接下来 S 行,每行包含 S 个整数,表示具体的迷宫的房间号分布,需注意 1,2,3,…S2 这 S2 个数字,每个数字只出现一次。
输出格式
每组数据输出一个结果,每个结果占一行。
结果表示为 Case #x: r d,其中 x 是组别编号(从 1 开始),r 是获胜的人最初所在房间的房间号,d 是他可以到达的房间数量。
如果有多个人可到达的房间数相同,那么最初所在房间的房间号最小的人将获胜。
数据范围
1≤T≤100,
1≤S≤1000
输入样例:
2
2
3 4
1 2
3
1 2 9
5 3 8
4 6 7
输出样例:
Case #1: 1 2
Case #2: 6 4
代码:
// 记忆化搜索
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n, g[N][N], f[N][N];
int dx[4] = {
-1, 0, 1, 0}, dy[4] = {
0, 1, 0, -1};
int dfs(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 = x + dx[i], b = y + dy[i];
if (a >= 0 && a < n && b >= 0 && b < n && g[a][b] == g[x][y] + 1)
v = max(v, dfs(a, b) + 1);
}
return v;
}
int main()
{
int T;
cin >> T;
for (int k = 1; k <= T; k++)
{
cin >> n;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
cin >> 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 = dfs(i, j);
if (t > cnt || t == cnt && id > g[i][j])
{
cnt = t;
id = g[i][j];
}
}
}
printf("Case #%d: %d %d\n", k, id, cnt);
}
return 0;
}
边栏推荐
- "PHP Basics" PHP statements and statement blocks
- vCenter7.0管理Esxi7.0主机
- Luogu Taotao picks apples
- Query and association of flask to database
- Creation and simple application of QPushButton button button
- arguments
- Flink1.15源码阅读flink-clients客户端执行流程(阅读较枯燥)
- Fluent rendering mechanism - GPU thread rendering
- JS rotation chart
- [ciscn2019 southeast China division]web11 1
猜你喜欢

User management - restrictions

OSI seven layer model and tcp/ip four layer (TCP and UDP) (notes)

All in one 1329 cells (breadth first search)

On Valentine's day, I drew an object with characters!

Block, there is a gap between the block elements in the row

Alibaba cloud international receipt message introduction and configuration process
![[netding cup 2020 rosefinch group]nmap 1 two solutions](/img/fa/b1349cb42b5768b7510217239ba73a.png)
[netding cup 2020 rosefinch group]nmap 1 two solutions

Create a project to realize login and registration, generate JWT, and send verification code

Background image related applications - full, adaptive

第2章 前台数据展现
随机推荐
Interviewer: what is scaffolding? Why do you need scaffolding? What are the commonly used scaffolds?
Connection failed during installation of ros2 [ip: 91.189.91.39 80]
借生态力量,openGauss突破性能瓶颈
List delete collection elements
[NPUCTF2020]ReadlezPHP 1
好吃难吃饱七分为宜;好喝难喝醉三分为佳
UVM Introduction Experiment 1
Containerd failed to pull private database image (kubelet)
Realization of backstage brand management function
Use of elastic box / expansion box (Flex)
Flask's operations on model classes
1178 questions of Olympiad in informatics -- ranking of grades
情人节,我用字符画出了一个对象!
pytorch_demo1
【uni-app高级实战】手把手带你学习一个纯实战复杂项目的开发1/100
Help send some recruitment. If you are interested, you can have a look
STM32小bug汇总
arguments
Software test interview questions (key points)
Transaction, order system add transaction