1533 Moving Pegs[暴力+打表]

这个题不是很难,就是暴力模拟一下,利用二进制表示状态,反正最后一共15种结果,暴力程序跑慢点也没事~,2个程序一个250行的暴力,一个150行的表,也是蛮拼的。。

14933000 1533 Moving Pegs Accepted C++ 0.009 2015-02-06 03:15:36

暴力程序:

#include<cstdio>
#include<queue>
#include<set>
#include<cstring>
#include<algorithm>
using namespace std;
typedef pair<int,int> pill;
const int maxd = 20;
int n;
int st;
vector<int>G[maxd][8];
vector<int>vis[1<<16];
struct Node{

    int st;
    vector<int>way;
    Node(int st){
        this -> st = st;
    }
};
//4164;2>;?61::7;441
//4 1 6 4 11 2 15 6 1 10 10 7 14 11 11 4 4 1
//4164;2>;?61::7;441
//4 1 6 4 11 2 14 11 15 6 1 10 10 7 11 4 4 1
queue<Node>q;
bool judge(int v){
    int cnt = 0;
    for(int i = 0; i < 15; i++)
        if(v & (1 << i))
            cnt ++;
    if(cnt == 14)
        return true;
    else
        return false;
}
void debug(int n){
    if(n == 0) return ;
    debug(n / 2);
    printf("%d",n % 2);
}
void init(){
    //6个方向
    //   1   2
    // 3       4
    //   5   6
    //编号1
    G[1][5].push_back(2);G[1][5].push_back(4);G[1][5].push_back(7);G[1][5].push_back(11);
    G[1][6].push_back(3);G[1][6].push_back(6);G[1][6].push_back(10);G[1][6].push_back(15);
    //编号2
    G[2][2].push_back(1);
    G[2][4].push_back(3);
    G[2][5].push_back(4);G[2][5].push_back(7);G[2][5].push_back(11);
    G[2][6].push_back(5);G[2][6].push_back(9);G[2][6].push_back(14);
    //编号3
    G[3][1].push_back(1);
    G[3][3].push_back(2);
    G[3][5].push_back(5);G[3][5].push_back(8);G[3][5].push_back(12);
    G[3][6].push_back(6);G[3][6].push_back(10);G[3][6].push_back(15);
    //编号4
    G[4][2].push_back(2);G[4][2].push_back(1);
    G[4][4].push_back(5);G[4][4].push_back(6);
    G[4][5].push_back(7);G[4][5].push_back(11);
    G[4][6].push_back(8);G[4][6].push_back(13);
    //编号5
    G[5][1].push_back(2);
    G[5][2].push_back(3);
    G[5][3].push_back(4);
    G[5][4].push_back(6);
    G[5][5].push_back(8);G[5][5].push_back(12);
    G[5][6].push_back(9);G[5][6].push_back(14);
    //编号6
    G[6][1].push_back(3);G[6][1].push_back(1);
    G[6][3].push_back(5);G[6][3].push_back(4);
    G[6][5].push_back(9);G[6][5].push_back(13);
    G[6][6].push_back(10);G[6][6].push_back(15);
    //编号7
    G[7][2].push_back(4);G[7][2].push_back(2);G[7][2].push_back(1);
    G[7][4].push_back(8);G[7][4].push_back(9);G[7][4].push_back(10);
    G[7][5].push_back(11);
    G[7][6].push_back(12);
    //编号8
    G[8][1].push_back(4);
    G[8][2].push_back(5);G[8][2].push_back(3);
    G[8][3].push_back(7);
    G[8][4].push_back(9);G[8][4].push_back(10);
    G[8][5].push_back(12);
    G[8][6].push_back(13);
    //编号9
    G[9][1].push_back(5);G[9][1].push_back(2);
    G[9][2].push_back(6);
    G[9][3].push_back(8);G[9][3].push_back(7);
    G[9][4].push_back(10);
    G[9][5].push_back(13);
    G[9][6].push_back(14);
    //编号10
    G[10][1].push_back(6);G[10][1].push_back(3);G[10][1].push_back(1);
    G[10][3].push_back(9);G[10][3].push_back(8);G[10][3].push_back(7);
    G[10][5].push_back(14);
    G[10][6].push_back(15);
    //编号11
    G[11][2].push_back(7);G[11][2].push_back(4);G[11][2].push_back(2);G[11][2].push_back(1);
    G[11][4].push_back(12);G[11][4].push_back(13);G[11][4].push_back(14);G[11][4].push_back(15);
    //编号12
    G[12][1].push_back(7);
    G[12][2].push_back(8);G[12][2].push_back(5);G[12][2].push_back(3);
    G[12][3].push_back(11);
    G[12][4].push_back(13);G[12][4].push_back(14);G[12][4].push_back(15);
    //编号13
    G[13][1].push_back(8);G[13][1].push_back(4);
    G[13][2].push_back(9);G[13][2].push_back(6);
    G[13][3].push_back(12);G[13][3].push_back(11);
    G[13][4].push_back(14);G[13][4].push_back(15);
    //编号14
    G[14][1].push_back(9);G[14][1].push_back(5);G[14][1].push_back(2);
    G[14][2].push_back(10);
    G[14][3].push_back(13);G[14][3].push_back(12);G[14][3].push_back(11);
    G[14][4].push_back(15);
    //编号15
    G[15][1].push_back(10);G[15][1].push_back(6);G[15][1].push_back(3);G[15][1].push_back(1);
    G[15][3].push_back(14);G[15][3].push_back(13);G[15][3].push_back(12);G[15][3].push_back(11);
}
void BFS(){
    while(!q.empty()) q.pop();
    Node start(st);
    q.push(start);
    vector<int>ans;
    int isok = 0;
    while(!q.empty()){
        Node state = q.front(); q.pop();
        int now = state.st;
        if(judge(now)){
            int e = state.way.size();
            if(state.way[e - 1] != n) continue;
//            for(int i = 0; i < state.way.size(); i++)
//                printf("%d ",state.way[i]);
//            printf("\n");
            isok = 1;
            if(!ans.size()){
                ans = state.way;
            }
            else{
                int can_add = 0;
                int L1 = state.way.size();
                int L2 = ans.size();
                if(L1 < L2){
                    ans = state.way;
                }
                else if(L1 == L2)
                for(int x = 0; x < L1 && x < L2; x++){
                     if(state.way[x] > ans[x]){
                         can_add = -1;
                         break;
                     }
                     else  if(state.way[x] < ans[x]){
                         can_add = 1;
                         break;
                     }
                }
                if(can_add == 1){
                    ans = state.way;
                }
                else if(can_add == 0 && L1 < L2){
                    ans = state.way;
                }
            }
        }
        for(int i = 1; i <= 15; i++){
            if(now & (1 << (i - 1))){                    //如果这一位为空,从6个方向找可以跳到这里的位置
                for(int j = 1; j <= 6; j++){
                    int cnt = 0;
                    for(int k = 0; k < G[i][j].size(); k++){
                         int e = G[i][j][k];
                         if(now & (1 << (e - 1)))        //如果是空白,跳出循环
                            break;
                         else{
                            cnt ++;
                            if(cnt == 1)                 //如果不是空白
                                continue;
                            int temp = now;
                            int l;
                            //-------------------------------------------------
                            for(l = 0;l <= k; l++){      //这一条线上的全部清空
                                //这条线上全部清
                                int c = G[i][j][l];
                                temp |= (1 << (c - 1));
                            }
                            temp = ~temp;
                            temp = temp | (1 << (i - 1));
                            temp = ~temp;
                            //-------------------------------------------------
                            Node tmp = state;
                            tmp.st = temp;
                            tmp.way.push_back(e);
                            tmp.way.push_back(i);
                            if(!vis[temp].size()){
                                 vis[temp] = tmp.way;
                                 q.push(tmp);
                            }
                            else{
                                int can_add = 0;
                                int L1 = tmp.way.size();
                                int L2 = vis[temp].size();
                                for(int x = 0; x < L1 && x < L2; x++){
                                     if(tmp.way[x] > vis[temp][x]){
                                         can_add = -1;
                                         break;
                                     }
                                     else  if(tmp.way[x] < vis[temp][x]){
                                         can_add = 1;
                                         break;
                                     }
                                }
                                if(can_add == 1){
                                    vis[temp] = tmp.way;
                                    q.push(tmp);
                                }
                                else if(can_add == 0 && L1 < L2){
                                    vis[temp] = tmp.way;
                                    q.push(tmp);
                                }
                            }
                         }
                    }
                }
            }
        }
    }
    if(!isok){
        printf("IM");
    }
    else{
        printf("%d\n",ans.size() / 2);
        for(int i = 0; i < ans.size(); i++)
            printf("%d ",ans[i]);
    }
    return;
}
int main(){
    int T;
    init();
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        st  = 0;
        st |= (1 << (n - 1));
        BFS();
    }
    return 0;
}

打表:

#include<cstdio>
#include<vector>
using namespace std;
const int maxd = 16;
int size[] = {18,18,18,18,20,18,18,20,20,18,18,18,18,18,18};
int L1[] = {4,1,6,4,11,2,14,11,15,6,1,10,10,7,11,4,4,1};    //18
int L2[] = {7,2,1,4,10,1,14,2,1,7,11,14,15,13,13,4,7,2};    //18
int L3[] = {10,3,1,6,7,1,12,3,1,10,14,12,11,13,13,6,15,3};  //18
int L4[] = {1,4,6,1,13,6,10,3,11,13,3,12,15,11,11,2,1,4};   //18
int L5[] = {12,5,3,8,15,12,6,13,7,9,1,7,10,8,7,9,11,14,14,5};//20
int L6[] = {1,6,4,1,13,4,7,2,15,13,2,14,11,15,15,3,1,6};    //18
int L7[] = {1,7,6,1,11,4,9,7,14,11,11,2,15,6,6,4,1,7};      //18
int L8[] = {3,8,12,5,15,3,13,6,1,10,2,9,11,2,14,5,2,9,10,8}; //20
int L9[] = {2,9,11,2,12,5,3,8,13,4,1,7,14,5,15,3,3,8,7,9};   //20
int L10[]= {1,10,4,1,11,4,4,6,12,5,10,8,15,12,12,3,1,10};   //18
int L11[]= {4,11,1,4,10,1,14,2,1,7,11,14,15,13,13,4,4,11};   //18
int L12[]= {14,12,2,14,7,2,1,4,15,13,3,15,11,14,4,13,15,12};    //18
int L13[]= {4,13,1,4,11,2,13,11,15,13,2,14,3,15,15,12,11,13};//18
int L14[]= {11,14,2,11,3,12,10,3,1,6,11,13,15,12,6,13,12,14};//18
int L15[]= {6,15,1,6,7,1,12,3,1,10,15,12,11,13,13,6,6,15};   //18
int main(){
    int T,n;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        int sz = size[n - 1];
        if(n == 1){
            printf("%d\n",sz / 2);
            for(int i = 0; i < sz; i++){
                if(i) printf(" ");
                printf("%d",L1[i]);
            }
        }
        else if(n == 2){
            printf("%d\n",sz / 2);
            for(int i = 0; i < sz; i++){
                if(i) printf(" ");
                printf("%d",L2[i]);
            }
        }
        else if(n == 3){
            printf("%d\n",sz / 2);
            for(int i = 0; i < sz; i++){
                if(i) printf(" ");
                printf("%d",L3[i]);
            }
        }
        else if(n == 4){
            printf("%d\n",sz / 2);
            for(int i = 0; i < sz; i++){
                if(i) printf(" ");
                printf("%d",L4[i]);
            }
        }
        else if(n == 5){
            printf("%d\n",sz / 2);
            for(int i = 0; i < sz; i++){
                if(i) printf(" ");
                printf("%d",L5[i]);
            }
        }
        else if(n == 6){
            printf("%d\n",sz / 2);
            for(int i = 0; i < sz; i++){
                if(i) printf(" ");
                printf("%d",L6[i]);
            }
        }
        else if(n == 7){
            printf("%d\n",sz / 2);
            for(int i = 0; i < sz; i++){
                if(i) printf(" ");
                printf("%d",L7[i]);
            }
        }
        else if(n == 8){
            printf("%d\n",sz / 2);
            for(int i = 0; i < sz; i++){
                if(i) printf(" ");
                printf("%d",L8[i]);
            }
        }
        else if(n == 9){
            printf("%d\n",sz / 2);
            for(int i = 0; i < sz; i++){
                if(i) printf(" ");
                printf("%d",L9[i]);
            }
        }
        else if(n == 10){
            printf("%d\n",sz / 2);
            for(int i = 0; i < sz; i++){
                if(i) printf(" ");
                printf("%d",L10[i]);
            }
        }
        else if(n == 11){
            printf("%d\n",sz / 2);
            for(int i = 0; i < sz; i++){
                if(i) printf(" ");
                printf("%d",L11[i]);
            }
        }
        else if(n == 12){
            printf("%d\n",sz / 2);
            for(int i = 0; i < sz; i++){
                if(i) printf(" ");
                printf("%d",L12[i]);
            }
        }
        else if(n == 13){
            printf("%d\n",sz / 2);
            for(int i = 0; i < sz; i++){
                if(i) printf(" ");
                printf("%d",L13[i]);
            }
        }
        else if(n == 14){
            printf("%d\n",sz / 2);
            for(int i = 0; i < sz; i++){
                if(i) printf(" ");
                printf("%d",L14[i]);
            }
        }
        else if(n == 15){
            printf("%d\n",sz / 2);
            for(int i = 0; i < sz; i++){
                if(i) printf(" ");
                printf("%d",L15[i]);
            }
        }
        else
            printf("IMPOSSIBLE");
        printf("\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值