作者:
trudbot
,
2022-06-23 22:24:30
,
所有人可见
,
阅读 6
//
// 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;
}