JZOJ 3422. [NOIP2013 Simulation] Aqua dance

Time Limits: 1000 ms  Memory Limits: 262144 KB  Detailed Limits 

Description

Aqua has been a lattice-shaped colorful carpet as a birthday gift, and more specifically, the color of the carpet on the grid can be changed with the stampede.

In order to please her idol Rainbow Cat, Aqua decided to dance a dance of light on the carpet to sell Meng ~ ~ ~

plaid carpet has N rows and N columns, each grid with a number between 0 to 5 on behalf of its colour.

Aqua can choose the color of between 0 to 5, and then gently beating step, China Unicom block the upper left corner of the carpet in the grid where the grid will become all that color she chooses. Here the communication is defined as: a common edge of two cells, and the same color.

Because Aqua is cast dodge to dance, in order not to consume too many infuriating, she wanted to know how many steps in order at least to put all the grid colors become the same.

Input

Each test site contains a plurality of sets of data.

The first line of each data set is an integer N, the stall on the grid of N rows and N columns.

Next, each of a number of N * N matrix, the matrix are between 0 and 5, a color of each grid.

N = 0 represents the end of input.

Output

For each test, output an integer, represents the minimum number of steps.

Sample Input

2
0 0 
0 0
3
0 1 2
1 1 2
2 2 1
0

Sample Output

0
3

Data Constraint

For 30% of the data, N <= 5

to 50% of the data, N <= 6

to 70% of the data, N <= 7

to 100% of the data, N <= 8, not more than 20 per test group data.
 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<string.h>
 4 using namespace std;
 5 int    xx[4]={0,0,-1,1};
 6 int    yy[4]={1,-1,0,0};
 7 int    a[10][10],vis[10][10],e[6];
 8 int    n,flag,d;
 9 void color(int x,int y,int col)
10 {
11     vis[x][y]=1;
12     for(int i=0;i<4;i++)
13     {
14         int nx=x+xx[i],ny=y+yy[i];
15         if(nx<1||nx>n||ny<1||ny>n||vis[nx][ny]==1)
16             continue;
17         vis[nx][ny]=2;
18         if(a[nx][ny]==col)
19             color(nx,ny,col);
20     }
21 }
22 int eva(int s)
23 {
24     int cnt=0;
25     memset(e,0,sizeof(e));
26     for(int i=1;i<=n;i++)
27         for(int j=1;j<=n;j++)
28             if(vis[i][j]!=1&&!e[a[i][j]])
29                 e[a[i][j]]=1,cnt++;
30     return(cnt);
31 }
32 bool judge(int col)
33 {
34     int tmp=0;
35     for(int i=1;i<=n;i++)
36         for(int j=1;j<=n;j++)
37             if(a[i][j]==col&&vis[i][j]==2)
38             {
39                 tmp++;
40                 color(i,j,col);
41             }
42     return(tmp>0);
43 }
44 void search(int s)
45 {
46     int tmp=eva(s),t[10][10];
47     if(tmp==0)
48     {
49         flag=1;
50         return;
51     }else if(tmp+s>d)
52         return;
53     for(int i=0;i<=5;i++)
54     {
55         memcpy(t,vis,sizeof(t));
56         if(judge(i))
57             search(s+1);
58         memcpy(vis,t,sizeof(vis));
59         if(flag)
60             return;
61     }
62 }
63 int main()
64 {
65     while(scanf("%d",&n)!=EOF&&n)
66     {
67         flag=0;memset(vis,0,sizeof(vis));
68         for(int i=1;i<=n;i++)
69             for(int j=1;j<=n;j++)
70                 cin>>a[i][j];
71         color(1,1,a[1][1]);
72         for(d=0;d>=0;d++)
73         {
74             search(0);
75             if(flag)
76             {
77                 cout<<d<<endl;
78                 break;
79             }
80         }
81     }
82     return 0;
83 }

Guess you like

Origin www.cnblogs.com/anbujingying/p/11297589.html