zoukankan      html  css  js  c++  java
  • 九宫重排

    源地址:http://www.dotcpp.com/oj/problem1426.html#

    问题 1426: [蓝桥杯][历届试题]九宫重排

    时间限制: 1Sec 内存限制: 128MB 提交: 298 解决: 57

    题目描述

    如下面第一个图的九宫格中,放着  1~8  的数字卡片,还有一个格子空着。与空格子相邻的格子中的卡片可以移动到空格中。经过若干次移动,可以形成第二个图所示的局面。

    我们把第一个图的局面记为:12345678. 
    把第二个图的局面记为:123.46758 
    显然是按从上到下,从左到右的顺序记录数字,空格记为句点。 
    本题目的任务是已知九宫的初态和终态,求最少经过多少步的移动可以到达。如果无论多少步都无法到达,则输出-1。
    输入
    输入第一行包含九宫的初态,第二行包含九宫的终态。 
    输出
    输出最少的步数,如果不存在方案,则输出-1。
    样例输入
    12345678. 
    123.46758 
    样例输出
    3

    还未AC,先记录一下
    #include"iostream"
    using namespace std;
    
    char map[3][3];
    char map1[3][3];
    char v[262114][9];
    int v1 = 0,max1 = -1;
    void show(){
        cout<<"----------------"<<endl;
        for(int j = 0;j < 3;j++){
            for(int i = 0;i < 3;i++){
                cout<<map[j][i]<<ends;
            }
            cout<<endl;
        }
    }
    void atos(){        //每种情况存入v数组
        for(int j = 0;j < 3;j++){
            for(int i = 0;i < 3;i++){
                v[v1][3 * j + i] = map[j][i];
            }
        }
        v1++;
    }
    int visited(){        //不重复
        for(int k = 0;k < v1;k++){
            int flag = false;
            for(int j = 0;j < 3;j++){
                for(int i = 0;i < 3;i++){
                    if(v[k][3 * j + i] != map[j][i]){
                        flag = true;
                        break;
                    }
                }
            }
            if(!flag){
                return false;
            }
        }
        return true;
    }
    void invi(){
        int j;
        for(j = 0;j < 3;j++){
            for(int i = 0;i < 3;i++){
                cin>>map[j][i];
            }
        }
        for(j = 0;j < 3;j++){
            for(int i = 0;i < 3;i++){
                cin>>map1[j][i];
            }
        }
    }
    int compare(){
        for(int j = 0;j < 3;j++){
            for(int i = 0;i < 3;i++){
                if(map1[j][i] != map[j][i]){
                    return 0;
                }
            }
        }
        return 1;
    }
    
    void move(int y,int x,int count = 0){        //上下左右0,1,2,3
        show();
        atos();        //将这种情况存入v数组
    /*    for(int k = 0;k < v1;k++){
            for(int i = 0;i < 9;i++){
                cout<<v[k][i];    
            }
            cout<<endl;
        }
    */
        if(!compare()){
            if(y - 1 >= 0){        //上移
                map[y][x] = map[y - 1][x];
                map[y - 1][x] = '.';
                if(visited()){
                    move(y - 1,x,count + 1);
                }
                map[y - 1][x] = map[y][x];
                map[y][x] = '.';
            }
            if(y + 1 < 3){        //下移
                map[y][x] = map[y + 1][x];
                map[y + 1][x] = '.';
                if(visited()){
                    move(y + 1,x,count + 1);
                }
                map[y + 1][x] = map[y][x];
                map[y][x] = '.';
            }
            if(x + 1 < 0){        //右移
                map[y][x] = map[y][x + 1];
                map[y][x + 1] = '.';
                if(visited()){
                    move(y,x + 1,count + 1);
                }
                map[y][x + 1] = map[y][x];
                map[y][x] = '.';    
            }
            if(x - 1 >= 0){        //左移
                map[y][x] = map[y][x - 1];
                map[y][x - 1] = '.';
                if(visited()){
                    move(y,x - 1,count + 1);
                }
                map[y][x - 1] = map[y][x];
                map[y][x] = '.';
            }
        }
        else{
            if(max1 < count){
                max1 = count;
            }
        }
    }
    
    
    int main(){
        invi();
    //    show();
        for(int i = 0;i < 3;i++){
            for(int j = 0;j < 3;j++){
                if(map[i][j] == '.'){
                    move(i,j);
                }
            }
        }
    //    show();
        cout<<max1;
        return 0;
    }
    /*
    123
    456
    78.
    
    23.
    146
    758
    
    12345678.
    .12346758
    */
  • 相关阅读:
    hdu 5366 简单递推
    hdu 5365 判断正方形
    hdu 3635 并查集
    hdu 4497 数论
    hdu5419 Victor and Toys
    hdu5426 Rikka with Game
    poj2074 Line of Sight
    hdu5425 Rikka with Tree II
    hdu5424 Rikka with Graph II
    poj1009 Edge Detection
  • 原文地址:https://www.cnblogs.com/oleolema/p/9051391.html
Copyright © 2011-2022 走看看