The fourth question appears on the screen:
[Search and Backtracking Algorithm] Largest Platform (Standard IO)
Time limit: 1000 ms Space limit: 262144 KB
Topic description:
The following is a 4×4 matrix. Its characteristics are: (1) The elements of the matrix are all positive integers; (2) Elements with equal values are adjacent. In this way, this matrix forms a first-level "platform" with the largest "platform" area of 8 and a height (element value) of 6. If there is an N×N matrix that also has the characteristics of the above matrix, find the area and height of the largest "platform" of the matrix.
6 6 6 7
1 6 3 7
1 6 6 7
6 6 7 7enter:
The first line is N (1≤N≤100), and the following is an N×N matrix.
Output:
The first line is the maximum area of the platform;
the second line is the element value.Sample input:
4 6 6 6 7 1 6 3 7 1 6 6 7 6 6 7 7Sample output:
8 6
"It should still be a search. The condition is that it is above, below, left, and right of the current platform, and the numbers are equal... " Xiaohang said.
#include<iostream>
using namespace std;
int n,plat[105][105],check[150],ans,maxx=-1,num,lnum;
void dg(int x,int y)
{
++ans;
plat[x][y]=0;
if(plat[x+1][y]==lnum) dg(x+1,y);
if(plat[x-1][y]==lnum) dg(x-1,y);
if(plat[x][y+1]==lnum) dg(x,y+1);
if(plat[x][y-1]==lnum) dg(x,y-1);
}
int main()
{
check[0]=1;
cin>>n;
for(int i=1;i<=n;++i)
{
for(register int j=1;j<=n;++j)
{
cin>>plat[i][j];
}
}
for(int i=1;i<=n;++i)
{
for(int j=1;j<=n;++j)
{
if(check[plat[i][j]]==0)
{
lnum=plat[i][j];
ans=0;
dg(i,j);
if(ans>maxx)
{
maxx=ans;
num=lnum;
}
check[plat[i][j]]=1;
}
}
}
cout<<maxx<<endl<<num;
}
The code came out quickly, and 100 points followed quickly. The fifth question popped up in an instant...