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。