当前位置:网站首页>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;
}
边栏推荐
- Realization of specification management and specification option management functions
- Function realization of order system
- Alibaba cloud international receipt message introduction and configuration process
- 第2章 前台数据展现
- VS Code中#include报错(新建的头文件)
- It's better to be full than delicious; It's better to be drunk than drunk
- ROS2安装时出现Connection failed [IP: 91.189.91.39 80]
- Block, there is a gap between the block elements in the row
- 帮忙发几个招聘,有兴趣可以看看
- Explain cache consistency and memory barrier
猜你喜欢

Breadth first search

After downloading URL loader and specifying the size of the image with limit, the image will not be displayed

借生态力量,openGauss突破性能瓶颈

MCDF顶层验证方案

Massive data Xiao Feng: jointly build and govern opengauss root community and share a thriving new ecosystem

All in one 1251 - Fairy Island for medicine (breadth first search)

Login to homepage function implementation

百人参与,openGauss开源社区这群人都在讨论什么?

缓存一致性与内存屏障

"PHP Basics" uses echo statements to output information
随机推荐
User management - restrictions
[MRCTF2020]PYWebsite 1
说透缓存一致性与内存屏障
IBM3650M4实体机安装VCenter7.0
Apache SSI remote command execution vulnerability
Set set
Vcenter7.0 installation of ibm3650m4 physical machine
MCDF顶层验证方案
Introduction to depth first search (DFS)
Attack and defense world MFW
SSTI template injection
Installation and use of Supervisor
Process control - Branch
How to uninstall -- Qianxin secure terminal management system
Use of flask
Weekly learning summary
好吃难吃饱七分为宜;好喝难喝醉三分为佳
pytorch_demo1
On Valentine's day, I drew an object with characters!
缓存一致性与内存屏障