Cube IV

本文探讨了如何在一个由正方形房间组成的迷宫中,找到能够移动最多步数的人,并确定其所在房间数量。通过使用深度优先搜索(DFS)算法和动态规划,我们能够解决这个问题。每间房间都有一个唯一的编号,人只能移动到下一个房间的编号比当前房间编号大1的房间。文章详细介绍了算法实现和示例输入输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



Problem

Vincenzo decides to make cube IV but only has the budget to make a square maze. Its a perfect maze, every room is in the form of a square and there are 4 doors (1 on each side of the room). There is a big number written in the room. A person can only move from one room to another if the number in the next room is larger than the number in his current room by 1. Now, Vincenzo assigns unique numbers to all the rooms (1, 2, 3, .... S2) and then places S2 people in the maze, 1 in each room where S is the side length of the maze. The person who can move maximum number of times will win. Figure out who will emerge as the winner and the number of rooms he will be able to move.
Input

The first line of the input gives the number of test cases, T. T test cases follow. Each test case consists of S which is the side length of the square maze. Then S2 numbers follow like a maze to give the numbers that have been assigned to the rooms.

1 2 9
5 3 8
4 6 7
Output

For each test case, output one line containing "Case #x: r d", where x is the test case number (starting from 1), r is the room number of the person who will win and d is the number of rooms he could move. In case there are multiple such people, the person who is in the smallest room will win.

Limits 1 ≤ T ≤ 100.
Small dataset 1 ≤ S ≤ 10
Large dataset 1 ≤ S ≤ 103.


Sample
Input 
2
2
3 4
1 2
3
1 2 9
5 3 8
4 6 7

Output
Case #1: 1 2
Case #2: 6 4

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fstream>
using namespace std;

int A[1005][1005];
int dis[1005][1005];

void init()
{
    for(int i=0;i<1005;i++)
        for(int j=0;j<1005;j++)
        {
             dis[i][j]=-1;
             A[i][j]=-1;
        }
}

int dfs(int row, int col)
{
    if((A[row-1][col]-A[row][col]!=1)&&(A[row][col-1]-A[row][col]!=1)&&(A[row+1][col]-A[row][col]!=1)&&(A[row][col+1]-A[row][col]!=1))
    {
        dis[row][col]=1;
        return 1;
    }
    if(A[row-1][col]-A[row][col]==1)
    {
        if(dis[row-1][col]!=-1)
        {
            dis[row][col]=dis[row-1][col]+1;
            return dis[row][col];
        }
        else
        {
            return dfs(row-1, col)+1;
        }
    }
    if(A[row][col-1]-A[row][col]==1)
    {
        if(dis[row][col-1]!=-1)
        {
            dis[row][col]=dis[row][col-1]+1;
            return dis[row][col];
        }
        else
        {
            return dfs(row, col-1)+1;
        }
    }
    if(A[row+1][col]-A[row][col]==1)
    {
        if(dis[row+1][col]!=-1)
        {
            dis[row][col]=dis[row+1][col]+1;
            return dis[row][col];
        }
        else
        {
            return dfs(row+1, col)+1;
        }
    }
    if(A[row][col+1]-A[row][col]==1)
    {
        if(dis[row][col+1]!=-1)
        {
            dis[row][col]=dis[row][col+1]+1;
            return dis[row][col];
        }
        else
        {
            return dfs(row, col+1)+1;
        }
    }
}

int main()
{
    int T, S, index=1;
    ifstream fcin;
    ofstream fcout;
    fcin.open("A-large-practice.in");
    fcout.open("result_large.txt");

    fcin>>T;
    while(index<=T)
    {
        fcin>>S;
        init();
        for(int i=1;i<=S;i++)
            for(int j=1;j<=S;j++)
                fcin>>A[i][j];

        int max=-1;
        int max_val=0;
        for(int i=1;i<=S;i++)
            for(int j=1;j<=S;j++)
            {
                int tmp=dfs(i,j);
                if(tmp>max)
                {
                    max=tmp;
                    max_val=A[i][j];
                }
                if(tmp==max)
                    max_val=max_val<A[i][j]?max_val:A[i][j];
            }
        fcout<<"Case #"<<index<<": "<<max_val<<" "<<max<<endl;
        index++;
    }
    fcin.close();
    fcout.close();
}


我所想到的就是DFS加上一个记录已经得到结果的数组。同时避免对数组越界的讨论,下标都从1开始,题目给定范围外的其他元素都设为-1。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值