Luo Gu p3956 [chessboard]

Daily blog (✧◡✧)

[Link] board topic

algorithm:

This then is the universal set 2017;


 

. first on color processing: Let c [i] [j] = color + 1; so colorless = 0, 1 = red, 2 = yellow;

Then the fact of memory, will remember the answer to the first array is initialized to a large number of (my initial order 0x3f3f3f3f);

. SECOND, the DFS main parts:

1. four variables:

x, y store the searched location point;

num store the current spending

used to store ever used magic;

2. The return of several border

. ① When out of the map, return;

. ② The current value of the search to the search before inferior to superior (also including the same, if not identical, repeated repeatedly found), return;

③. When the search to the most bottom right (m, m), first determine the value (in fact, I do not judge qwq catch the foot ah pro-test not) because if the current value is not superior to the previous search is better than the current value in early ② it will be return;

So if we can enter a recursive determination, certain more preferably, less direct update like;

3. True main parts:

1. Search for the four directions (in the media is like a broad search)

If the node after the expansion of color, then the color judgment expand the node and the current node is the same

Same words, it is not necessary to increase cost, direct dfs (xx, yy, num, 0);

If different, it takes need to add 1, dfs (xx, yy, num + 1,0);

If you expand the node has no color, it is judged that the current node is not used magic, if it is used magic point, then obviously go elsewhere, if not used magic, use Infoprogramme energy, expand the node becomes into the same color as the current node, dfs (xx, yy, num + 2,1);

Finally, do not forget the back: c [xx] [yy] = 0;

Judgment no solution:

If no solution, the value will not update ans for 0x3f3f3f3f, if dfs finished, ans = 0x3f3f3f3f, indicating no solution, or a solution, output ans;

CODE:

#include<bits/stdc++.h>

using namespace std;

inline int read(){
    int ans=0;
    char last=' ',ch=getchar();
    while(ch>'9'||ch<'0') last=ch,ch=getchar();
    while(ch>='0'&&ch<='9') ans=(ans<<1)+(ans<<3)+ch-'0',ch=getchar();
    if(last == ' - ' ) ans = - years;
    return years; 
} 

Int m, n;
int c [ 101 ] [ 101 ], a [ 101 ] [ 101 ];
int years = 0x3f3f3f3f ;
int dx [ 5 ] = { 0 , 1 , - 1 , 0 , 0 };
int dy [ 5 ] = { 0 , 0 , 0 , 1 , - 1 }; 

void dfs(int x,int y,int num,int used){
    if(x<1||y<1||y>m||x>m) return;
    if(num>=a[x][y]) return;
    a[x][y]=num;
    if(x==m&&y==m){
        ans=num;
        return;
    }
    for(int i=1;i<=4;i++){
        int xx=x+dx[i],yy=y+dy[i];
        if(c[xx][yy]){//have color
            if(c[xx][yy]==c[x][y]) dfs(xx,yy,num,0);
            else dfs(xx,yy,num+1,0);
        }
        else {
            if(!used){
                c[xx][yy]=c[x][y];
                dfs(xx,yy,num+2,1);
                c[xx][yy]=0;
            }
        }
    }
}

int main(){
    memset(a,63,sizeof(a));
    m=read();n=read();
    int x,y,color;
    for(int i=1;i<=n;i++){
        x=read();y=read();color=read();
        c[x][y]=color+1;//no color:0
        //red color:1 yellow color:2
    }
    dfs(1,1,0,0);
    if(ans==0x3f3f3f3f) printf("-1");
    else printf("%d",ans);
    return 0;
}

end-

Guess you like

Origin www.cnblogs.com/zhuier-xquan/p/11114707.html