Wide search of the shortest ideological chameleon deformation +

#include <cstdio> 
#include <Queue> 
#include <CString> 
#include <algorithm>
 the using  namespace STD;
 const  int MAXN = 2500 ;
 int Map [MAXN] [MAXN];
 int n-, m, X, Y;
 int DIS [MAXN] [MAXN];
 struct Node 
{ 
    int X, Y, DIS;
     BOOL  operator <( const Node a &) const 
    { 
        return DIS> a.dis; // with a large number smaller than the root is defined as greater than the stack, so that each time taken is the minimum distance, corresponding to a wide search 
    } 
}; 

the priority_queue <Node> q;

int xx[5]={0,1,0,0,-1};
int yy[5]={0,0,1,-1,0};
void dijkstra()
{
    memset(dis,127,sizeof(dis));
    dis[x][y]=0;
    node pos;
    pos.x=x;
    pos.y=y;
    pos.dis=0;
    q.push(pos);
    while(q.size())
    {
        int x=q.top().x;
        int y=q.top().y;
        int d=q.top().dis;
        q.pop();
        for(int i=1;i<=4;i++)
        {
            int fx=x+xx[i];
            int fy=y+yy[i];
            if(fx<=0||fx>n||fy<=0||fy>m) continue;
            int op=1;//fx,fy与x,y之间的边的大小 
            if(map[fx][fy]==map[x][y]) op=0;
            if(dis[fx][fy]>d+op)
            {
                dis[fx][fy]=d+op;
                pos.x=fx;
                pos.y=fy;
                pos.dis=dis[fx][fy];
                q.push(pos);
            }
        }
    }
}
int main()
{
    scanf("%d%d%d%d",&n,&m,&x,&y);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            scanf("%d",&map[i][j]);
        }
    }
    dijkstra();
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            printf("%d ",dis[i][j]);
        }
        printf("\n");
    }
    return 0;
}

Topic background

In an  N × M upper of N × M, each grid has a square of a given color matrix, there is a chameleon in the first  X- X-line  Y , he will move in the column of the square matrix Y.

Mobile rules are as follows:

Chameleon only adjacent to the current cell toward a moving a square grid (up and down), the boundary and can not be removed.

As we all know, chameleon-specific body color will make your body as the environment changes color changes. So if you move to the grid and the current color squares different times will +1 chameleon color change, color change or the number of times the same.

However, the minimum number of steps is not intended chameleon movable, it is desirable to minimize color change times.

It tells you the color of each grid, and the grid it is currently located, it wants you to tell it the smallest color changes each grid from the current grid respectively to the number of how many.

Input Format

The first row includes four positive integers N , M , X- , the Y N, M, X-, the Y, the matrix size is described, and the coordinates of the starting point of the chameleon.

Output Format

N N lines each  M M non-negative integer, the minimum number of color changes is described beginning from the start point to each point.

Sample

input

5 5 3 3
1 1 3 1 1
1 3 3 3 1
3 2 2 2 3
1 4 4 4 6
1 1 1 5 1

output

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

data range

Time limit: 1s

Space limitations: 256M

Color size [ 1 , 100 ] [1100] in

Test point number N , M N, M range
1-3 1N,M51≤N,M≤5
4-5 1N,M1001≤N,M≤100
6-7 1N,M5001≤N,M≤500
8-10 1N×M1000000,1N,M20001≤N×M≤1000000,1≤N,M≤2000

Guess you like

Origin www.cnblogs.com/WHFF521/p/11202946.html