Fruit elimination (DFS of search)
fzhiy 2021-07-27 22:37:00
 

Title Description

“ Fruit elimination ” It's a mobile game , I believe everyone has played or played similar games .

Here is “ Fruit elimination ” An initial state of the game .

Basic rules of elimination : If there is 2 Or 2 More than one of the same fruits are linked together , You can click and eliminate .

In a certain state , There are several options that can be clicked and eliminated .

Input

Enter an integer first n, Indicates that the total number of squares for fruit is n*n.n take 3 To 1000 Integer between ( contain 3 and 1000).

Then type in n*n Data representing fruit , Different fruits are represented by different numbers , The same fruit is represented by the same number .

The number indicating the fruit is from 1 Start , No more than 100.

Output

In the initial state corresponding to the input data , There are several options for clicking and eliminating .

The number of output schemes .

The sample input

6
1 1 2 2 2 2
1 3 2 1 1 2
2 2 2 2 2 3
3 2 3 3 1 1
2 2 2 2 3 1
2 3 2 3 2 2

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

Sample output

6
  • 1.

 

Ideas :DFS Search up, down, left, right
 
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
const int N=1001;
int a[N][N],n,num,ans,dx[]= {1,0,-1,0},dy[]= {0,1,0,-1};
using namespace std;
void dfs(int x,int y,int p)
{
if(a[x][y]==p)
{
num++;
a[x][y]=N;
for(int i=0; i<4; i++)
{
int nx=x+dx[i],ny=y+dy[i];
if(nx>=0&&nx<n&&ny>=0&&ny<n)
dfs(nx,ny,p);
}
}
}
int main()
{
int i,j;
cin>>n;
for(i=0; i<n; i++)
for(j=0; j<n; j++)
cin>>a[i][j];
for(i=0; i<n; i++)
for(j=0; j<n; j++)
if(a[i][j]!=N)
{
int p=a[i][j];
num=0;
dfs(i,j,p);
if(num>=2)
ans++;
}
cout<<ans<<endl;
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.


Please bring the original link to reprint ,thank
Similar articles