• Filp_game


    dfs算法求解,注意全局变量的初始化问题,被坑了一下午。。。

    #include <iostream>
    using namespace std;
    #define size 4
    char input[4][4];
    int  data[4][4];
    int DX[4]={-1,0,1,0};
    int DY[4]={0,1,0,-1};
    typedef struct{
        int x;
        int y;
    }pos;
    pos cur_pos,tmp_pos;
    int min_step=10000;
    //判断是否全是白色的棋子
    bool justice_all_0(){
        bool ret = true;
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                if(data[i][j]==1){
                    ret = false;
                    //return ret;
                }
            }
        }
        return ret;
    }
    //判断是否全是黑色的棋子
    bool justice_all_1(){
       bool ret = true;
       for(int i=0;i<4;i++){
           for(int j=0;j<4;j++){
                if(data[i][j]==0){
                    ret = false;
                    //return ret;
                }
            }
        }
        return ret;
    }
    //dfs
    void dfs(int step, int count){
        //printf("%d %d
    ", step, count);
       if(step>=16){return;}//每一个都翻过了也不行
    
    
       //if(step==0&&(justice_all_1()||justice_all_0())){ printf("0");return;}
       if(justice_all_1()||justice_all_0()){
           //printf("find one!
    ");
            if(count<min_step){
                min_step = count;
            }
        }
           pos cur_pos;
           cur_pos.x = step/4;//就是按照顺序走的,准确可行
           cur_pos.y = step%4;
           
           //不翻转
           dfs(step+1,count);
           cur_pos.x = step/4;//就是按照顺序走的,准确可行
           cur_pos.y = step%4;
    
            //翻转
           data[cur_pos.x][cur_pos.y] = !data[cur_pos.x][cur_pos.y];
           for(int j=0;j<4;j++){
                tmp_pos.x = cur_pos.x + DX[j];
                tmp_pos.y = cur_pos.y + DY[j];
                if(tmp_pos.x>=0&&tmp_pos.y>=0&&tmp_pos.x<size&&tmp_pos.y<size){
                    data[tmp_pos.x][tmp_pos.y] = !data[tmp_pos.x][tmp_pos.y];
                }
           }
           dfs(step+1,count+1);
           //需要回溯
           data[cur_pos.x][cur_pos.y] = !data[cur_pos.x][cur_pos.y];
           for(int k=0;k<4;k++){
                tmp_pos.x = cur_pos.x + DX[k];
                tmp_pos.y = cur_pos.y + DY[k];
                if(tmp_pos.x>=0&&tmp_pos.y>=0&&tmp_pos.x<size&&tmp_pos.y<size){
                    data[tmp_pos.x][tmp_pos.y] = !data[tmp_pos.x][tmp_pos.y];
                }
           }
    
    
    }
    int main(){
        freopen("input.txt","r",stdin);
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                cin>>input[i][j];}
        }
        for(int k=0;k<4;k++){
            for(int l=0;l<4;l++){
                if(input[k][l]=='w'){
                    data[k][l] = 0;
                }
                else if(input[k][l]=='b'){
                    data[k][l] = 1;}
            }
        }
    
        dfs(0,0);
        printf("%d
    ",min_step);  
        return 0;
    }

      要注意的就是不翻的回溯,不翻的话也是有回溯过程的,回溯过程需要对于当前的cur_pos初始化,不然想一下如果走到了最后一步向前面回溯的时候当前点坐标还是回溯前的坐标,是会出问题的。

    大多数想法要么平庸,要么更糟糕,这很大程度上因为绝妙的想法难得一见,而且他们还要在我们身边这个充斥了各种恶俗的所谓常识的环境中孕育生长。
  • 相关阅读:
    leetcode第35题--Valid Sudoku
    leetcode 34 Search Insert Position
    leetcode第33题--Search for a Range
    leetcode第32题--Search in Rotated Sorted Array
    leetcode第31题--Longest Valid Parentheses
    leetcode第30题--Next Permutation
    leetcode第29题--Substring with Concatenation of All Words
    leetcode第28题--Divide Two Integers
    leetcode第27题--Implement strStr()
    17_7_7 JDBC存储过程 + 事务
  • 原文地址:https://www.cnblogs.com/linux0537/p/6612783.html
Copyright © 2020-2023  润新知