当前位置:网站首页>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;
}
边栏推荐
- Use of "PHP Basics" Boolean
- Realization of background channel group management function
- Make a game by yourself with pyGame 01
- Software test interview questions (key points)
- General Administration of Customs: the import of such products is suspended
- 带宽 与 货币
- 海关总署:这类产品暂停进口
- [netding cup 2020 rosefinch group]nmap 1 two solutions
- All in one 1329 cells (breadth first search)
- How to view instances of software objects in QSIM?
猜你喜欢

What are the differences or similarities between "demand fulfillment to settlement" and "purchase to payment"?

"PHP Basics" PHP statements and statement blocks

Day5 - Flame restful request response and Sqlalchemy Foundation

Chapter 2 foreground data display
![[NPUCTF2020]ReadlezPHP 1](/img/d9/590446b45f917be3f077a9ea739c20.png)
[NPUCTF2020]ReadlezPHP 1

General Administration of Customs: the import of such products is suspended

Cache consistency and memory barrier

Introduction to depth first search (DFS)

How to upload qiniu cloud

Flink1.15源码阅读flink-clients客户端执行流程(阅读较枯燥)
随机推荐
Background image related applications - full, adaptive
Risk control and application of informatization project
JWT authentication and login function implementation, exit login
Apache SSI remote command execution vulnerability
Process control - Branch
pytorch_demo1
Realize SKU management in the background
Day6 --- Sqlalchemy advanced
[netding cup 2020 rosefinch group]nmap 1 two solutions
众昂矿业:新能源行业快速发展,氟化工产品势头强劲
Massive data Xiao Feng: jointly build and govern opengauss root community and share a thriving new ecosystem
Hundreds of people participated. What are these people talking about in the opengauss open source community?
03.使用引号来监听对象嵌套值的变化
pytorch_ demo1
Introduction to depth first search (DFS)
Blueprint class view method
IBM3650M4实体机安装VCenter7.0
Login to homepage function implementation
User management - restrictions
开怀一笑