HTTP://CODEVS.CN/PROBLEM/2495/(Topic link)
Test instructions
Give a n*n matrix, where elements have 5 colors, each time you can change the upper left corner of the element is located in the connected block of a color, connected block refers to adjacent and the same color elements within the same connected block. Ask for at least a few changes in color to make all elements the same color.
Solution
Just started to play an iterative deepened, get 20 points ... Consider how to optimize it.
One obvious optimization is that when dyeing, only one loop of the connected block is extended around the connected block, and a new outer ring element is preserved under the staining.
But this is still not enough, only get50 points.
Consider A *, how to construct a valuation function. Obviously, if the remaining elements that do not belong to the connected block have different colors of CNT, then at least the dye CNT is required, then if the cnt+ current step > enum Depth D, it is clear that this will not be the answer, directly exit.
Details
The details are a bit disgusting, just started I use the BFS to achieve dyeing operations, and then back to a variety of bad operations, directly turned the puzzle. It's really beautiful.
Code
codevs2495#include<algorithm> #include <iostream> #include <cstdlib> #include <cstring># include<cstdio> #include <cmath> #include <queue> #define LL long long#define inf 2147483640#define Pi ACOs ( -1.0) #define FREE (a) freopen (a ".", "R", stdin), Freopen (a ". Out", "w", stdout), using namespace Std;int xx[4]={0,0, -1,1};int yy[4]={1,-1,0,0};int a[10][10],vis[10][10],e[6];int n,flag,d;void color (int x,int y,int col) {vis[x][y]=1; for (int i=0;i<4;i++) {int nx=x+xx[i],ny=y+yy[i];if (nx<1 | | nx>n | | ny<1 | | ny>n | | vis[nx][ny]==1) contin Ue;vis[nx][ny]=2;if (a[nx][ny]==col) color (nx,ny,col);}} int Eva (int s) {int cnt=0;memset (e,0,sizeof (e)); for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (Vis[i][j]!=1 & &!e[a[i][j]]) E[a[i][j]]=1,cnt++;return CNT;} BOOL Judge (int col) {int tmp=0;for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (A[i][j]==col && vis[i][j]==2 ) {Tmp++;color (i,j,col);} return tmp>0;} void search (int s) {int Tmp=eva (s), t[10][10];if (tmp==0) {Flag=1;return;} else if (tmp+s>d) return;for (int i=0;i<=5;i++) {memcpy (t,vis,sizeof (t)), if (judge (i)) search (s+1); memcpy (Vis,t, sizeof (VIS)); if (flag) return;}} int main () {while (scanf ("%d", &n)!=eof && N) {flag=0;memset (vis,0,sizeof (VIS)); for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) scanf ("%d", &a[i][j]), color (1,1,a[1][1]), and for (d=0;d>=0;d++) {search (0); if (flag) { printf ("%d\n", d); break;}}} return 0;}
"codevs2495" water-jingling dance steps