AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 校园
  • 应用
  • 文章
    • 题解
    • 分享
    • 问答
  • 吐槽
  • 登录/注册

AcWing 691. 立方体IV 0.04 AC币

作者: 作者的头像   trudbot ,  2022-06-23 22:24:30 ,  所有人可见 ,  阅读 6


1


//
// Created by trudbot on 2022/6/23.
//

#include <bits/stdc++.h>
using namespace std;
#define MaxN 1010

int rooms[MaxN][MaxN];
int visited[MaxN][MaxN];
int MaxSteps[MaxN][MaxN];
int s;
map<int, pair<int, int>> pos;

void CountMaxSteps(int row, int col)
{
    if(visited[row][col])
        return;

    visited[row][col] = true;

    if(row > 1)
    {
        if(rooms[row-1][col] == rooms[row][col]+1)
        {
            CountMaxSteps(row-1, col);
            MaxSteps[row][col] += MaxSteps[row-1][col];
        }
    }

    if(col > 1)
    {
        if(rooms[row][col-1] == rooms[row][col]+1)
        {
            CountMaxSteps(row, col-1);
            MaxSteps[row][col] += MaxSteps[row][col-1];
        }
    }

    if(row < s)
    {
        if(rooms[row+1][col] == rooms[row][col]+1)
        {
            CountMaxSteps(row+1, col);
            MaxSteps[row][col] += MaxSteps[row+1][col];
        }
    }

    if(col < s)
    {
        if(rooms[row][col+1] == rooms[row][col]+1)
        {
            CountMaxSteps(row, col+1);
            MaxSteps[row][col] += MaxSteps[row][col+1];
        }
    }

}

int main() {

    int T;
    cin >> T;

    for(int i=1; i<=T; i++)
    {
        cin >> s;
        for(int j=1; j<=s; j++)
            for(int k=1; k<=s; k++)
            {
                cin >> rooms[j][k];
                pos[rooms[j][k]] = {j, k};
                visited[j][k] = false;
                MaxSteps[j][k] = 1;
            }

        for(int j=1; j<=s*s; j++)
        {
            if(!visited[pos[j].first][pos[j].second])
            {
                CountMaxSteps(pos[j].first, pos[j].second);
            }
        }

        int res = 1;
        for(int j=2; j<=s*s; j++)
        {
            if(MaxSteps[pos[j].first][pos[j].second] > MaxSteps[pos[res].first][pos[res].second])
                res = j;
        }

        cout << "Case #" << i << ": " << res << " " << MaxSteps[pos[res].first][pos[res].second] << endl;
    }


    return 0;
}

0 评论

你确定删除吗?

© 2018-2022 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标
请输入绑定的邮箱地址
请输入注册信息