uva704

来源:互联网 发布:淘宝设计师学徒招聘 编辑:程序博客网 时间:2024/06/05 23:04

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

思路:
自己画画图,把顺时针,逆时针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