uva704

题目:
如何转动可以到达目标序列

思路:
自己画画图,把顺时针,逆时针90,180的都写出来就知道怎么做了。

代码:

#include <iostream>
using namespace std;
#include <cstring>
#include <stdio.h>
#include <queue>
#include <map>


const int N = 30;
bool ok;
char tar[N];
char start[N] = "034305650121078709a90121";


map<string,string> vis1;
map<string,int> vis2;

struct node {
    char s[30];
    char step[30];
    int stepnum;
}st,st1;

queue<node> q;

void change(int k) {

    char temp1,temp2;
    if(k == 1) {
        temp1 = st1.s[10];
        temp2 = st1.s[11];
        for(int i = 11;i >= 2; i--)
            st1.s[i] = st1.s[i-2];
        st1.s[0] = temp1;
        st1.s[1] = temp2;
        st1.s[21] = st1.s[9]; // 这时候序列已经发生了改变
        st1.s[22] = st1.s[10];
        st1.s[23] = st1.s[11];
    }
    if(k == 3) {
        temp1 = st1.s[0];
        temp2 = st1.s[1];
        for(int i = 0 ; i < 10 ; i ++)
            st1.s[i] =st1.s[i+2];
        st1.s[10] = temp1;
        st1.s[11] = temp2;
        st1.s[21] = st1.s[9];
        st1.s[22] = st1.s[10];
        st1.s[23] = st1.s[11];
    }
    if(k == 4) {
        temp1 = st1.s[22];
        temp2 = st1.s[23];
        for(int i = 23; i>= 12; i--)
            st1.s[i] = st1.s[i-2];
        st1.s[12] = temp1;
        st1.s[13] = temp2;
        st1.s[9] = st1.s[21];
        st1.s[10] = st1.s[22];
        st1.s[11] = st1.s[23];
    }
    if(k == 2) {
        temp1 = st1.s[12];
        temp2 = st1.s[13];
        for(int i =12; i <22; i++)
            st1.s[i] = st1.s[i+2];
         st1.s[23] = temp2;
         st1.s[22] = temp1;
         st1.s[11] = st1.s[23];
         st1.s[10] = st1.s[22];
         st1.s[9] = st1.s[21];
    }

}

void bfs1(char * temp) {
    vis1.clear();
    while(!q.empty()) {
        q.pop();
    }
    strcpy(st.s,temp);
    st.stepnum = 0;
    st.step[0] = '\0';
    vis1[st.s] = "st";
    q.push(st);
    while(!q.empty()) {
        st = q.front();
        q.pop();
        for(int i = 1; i<= 4; i++) {
            st1 = st;
            change(i);
            if(vis1[st1.s]== "") {
                st1.step[st1.stepnum++] = i+'0';
                st1.step[st1.stepnum] = '\0';
                vis1[st1.s] = st1.step;
                q.push(st1);
            }
        }
        if(st.stepnum == 8)
            return;
    }


}

void bfs2(char * temp) {
    vis2.clear();
    while(!q.empty())
        q.pop();
    strcpy(st.s,temp);
    st.stepnum = 0;
    st.step[0] = '\0';
    vis2[st.s] = 1;
    q.push(st);
    while(!q.empty()) {
        st = q.front();
        if(vis1[st.s]!="") {
            ok = true;
            return;
        }
        q.pop();
        for(int i = 1; i <= 4; i++) {
            st1 = st;
            change(i);
            if(vis2[st1.s] == 0) {
                st1.step[st1.stepnum++] = i + '0';
                st1.step[st1.stepnum] = '\0';
                vis2[st1.s] = 1;
                q.push(st1);
            }
        }
        if(st.stepnum == 9)
            return;
    }

}

int main() {
    int t;
    scanf("%d",&t);
    getchar();
    while(t--) {
        int x;
        ok = false;
        for(int i = 0 ; i < 24 ; i++) {
            scanf("%d",&x);
            if(x == 10)
                tar[i] = 'a';
            else
                tar[i] = '0' + x;
        }
        tar[24] = '\0';
        if(strcmp (tar,start) == 0) {
            printf("PUZZLE ALREADY SOLVED\n");
            continue;
        }
        bfs1(start);
        bfs2(tar);
        if(ok) {
            printf("%s",st.step);
            for(int i = vis1[st.s].size()-1;i >=0; i--) {
                if(vis1[st.s][i] == '1') printf("3");
                if (vis1[st.s][i] == '2') printf("4");
                if (vis1[st.s][i] == '3') printf("1");
                 if (vis1[st.s][i] == '4') printf("2");

            }
            printf("\n");
        }
        else printf("NO SOLUTION WAF FOUND IN 16 STEPS\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值