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;
}
时间: 2024-11-10 08:27:25

1533 Moving Pegs[暴力+打表]的相关文章

UVA - 1533 - Moving Pegs

题目链接:UVA-1533 题目大意: 有一个棋盘如下,黑色表示有棋子,白色表示空,棋盘初始状态为有一个位置n为空,其他位置都有棋子. 每次可以选择一个棋子在一条直线上隔一个或连续多个棋子跳到空白位置,然后这一个或多个棋子就被拿走,问最少几步可以使棋盘上的棋子拿走到只剩下一个且位置和初始空白位置相同.输出几步和每步的起始与落子位置.输出字典序最小的解,无解输出IMPOSSIBLE. 题目分析: 可以直接BFS计算. 但是状态太多,需要进行状态压缩. 将当前状态用二进制保存. 每个点最多可以有六种

HDU 1012 u Calculate e【暴力打表,水】

u Calculate e Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 46844    Accepted Submission(s): 21489 Problem Description A simple mathematical formula for e is where n is allowed to go to infini

HDU 5179 beautiful number (数位dp / 暴力打表 / dfs)

beautiful number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 801    Accepted Submission(s): 518 Problem Description Let A=∑ni=1ai?10n?i(1≤ai≤9)(n is the number of A's digits). We call A as "

筛法暴力打表 --- hdu : 12876 Quite Good Numbers

Quite Good Numbers Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 77, Accepted users: 57 Problem 12876 : No special judgement Problem description A "perfect" number is an integer that is equal to the sum

【ZOJ】3785 What day is that day? ——浅谈KMP应用之ACM竞赛中的暴力打表找规律

首先声明一下,这里的规律指的是循环,即找到最小循环周期.这么一说大家心里肯定有数了吧,“不就是next数组性质的应用嘛”. 先来看一道题 ZOJ 3785 What day is that day? Time Limit: 2 Seconds      Memory Limit: 65536 KB It's Saturday today, what day is it after 11 + 22 + 33 + ... + NN days? Input There are multiple tes

hdu 1431 素数回文(暴力打表,埃托色尼筛法)

这题开始想时,感觉给的范围5 <= a < b <= 100,000,000太大,开数组肯定爆内存,而且100000000也不敢循环,不超时你打我,反正我是不敢循环. 这题肯定得打表,筛素数肯定用埃托色尼筛法(不好意思把大名鼎鼎的埃拉托色尼名字打错了,表打我). 再看当你所找的回文数的位数为偶数时,有如下定理除11外所有偶数位数的回文数都能被11整除,所以所有偶数位数的回文都不是素数. 证明看如下(我手写的) 手机像素渣(凑活着吧)字丑也凑和着. 证完后我们在来说题目给的数据范围 所以当

UVA-1533 Moving Pegs (BFS+位压缩)

Description Venture MFG Company, Inc. has made a game board. This game board has 15 holes and these holes are filled with pegs except one hole. A peg can jump over one or more consecutive peg s to the nearest empty hole along the straight line. As a

XTU OJ 1210 Happy Number (暴力+打表)

Problem Description Recently, Mr. Xie learn the concept of happy number. A happy number is a number contain all digit 7 or only 1 digit other than 7. For example, 777 is a happy number because 777 contail all digit 7, 7177 and 87777 both happy number

ACM/ICPC 之 暴力打表-编码

///找到一个数字序列包含所有n位数(连续)一次且仅一次 ///暴力打表 ///Time:141Ms Memory:2260k #include<iostream> #include<cstring> #include<cstdio> using namespace std; #define MAX 1000010 bool v[MAX]; char num[6][MAX]; int main() { //freopen("in.txt", &quo