POJ 2286 The Rotation Game

Description The rotation game uses a # shaped board, which can hold 24
pieces of square blocks (see Fig.1). The blocks are marked with
symbols 1, 2 and 3, with exactly 8 pieces of each kind. Initially,
the blocks are placed on the board randomly. Your task is to move the
blocks so that the eight blocks placed in the center square have the
same symbol marked. There is only one type of valid move, which is to
rotate one of the four lines, each consisting of seven blocks. That
is, six blocks in the line are moved towards the head by one block and
the head block is moved to the end of the line. The eight possible
moves are marked with capital letters A to H. Figure 1 illustrates two
consecutive moves, move A and move C from some initial configuration.

题意


【题目分析】
就是写起来比较麻烦的一道A*算法的搜索题。状态的储存用结构体压入队列中显得十分方便。只是写起来比较麻烦。
结果不到三十分钟写完了,但是又WA了好几次才发现自己的程序多打了一个空格。(POJ真**严格)


【代码】(写的不是很美观,随便看看就好)

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
using namespace std;
int map[9][9]={0};
char list[201];
int change(char c)
{
    if (c=='A'){
        for (int i=1;i<=7;i++)
          map[i-1][3]=map[i][3];
        map[7][3]=map[0][3];
    }
    if (c=='B'){
        for (int i=1;i<=7;i++)
          map[i-1][5]=map[i][5];
        map[7][5]=map[0][5];
    }
    if (c=='C'){
        for (int i=8;i>=1;i--)
          map[3][i]=map[3][i-1];
        map[3][1]=map[3][8];
    }
    if (c=='D'){
        for (int i=8;i>=1;i--)
          map[5][i]=map[5][i-1];
        map[5][1]=map[5][8];
    }
    if (c=='G'){
        for (int i=0;i<=6;i++)
          map[5][i]=map[5][i+1];
        map[5][7]=map[5][0];
    }
    if (c=='E'){
        for (int i=8;i>=1;i--)
          map[i][5]=map[i-1][5];
        map[1][5]=map[8][5];
    }
    if (c=='F'){
        for (int i=8;i>=1;i--)
          map[i][3]=map[i-1][3];
        map[1][3]=map[8][3];
    }
    if (c=='H'){
        for (int i=0;i<=6;i++)
          map[3][i]=map[3][i+1];
        map[3][7]=map[3][0];
    }
    return 0;
}
int print(int x)
{

    for (int i=0;i<=x-1;i++)
      cout<<list[i];
      cout<<endl; 
      return 0;
}
int search(int step,int maxd)
{
    int num[4],maxn=0,maxnum=-1;
    memset(num,0,sizeof(num));
    for (int i=3;i<=5;i++)
        for (int j=3;j<=5;j++)
            num[map[i][j]]++;
    for (int i=1;i<=3;i++){
        if (num[i]==8){
            print(step-1);
            cout<<i<<endl; 
            return true;
        }
        if (num[i]>maxn){
            maxnum=i;
            maxn=num[i];
        }
    }
    if (8-maxn+step>maxd||step>maxd)
     return false;
    int x;
    for (x=(int)'A';x<=(int)'H';x++){
        char ch=char(x);
        change(ch);
        list[step-1]=ch;
        if (search(step+1,maxd)) return true;
        if (ch=='A') change('F');
        if (ch=='B') change('E');
        if (ch=='C') change('H');
        if (ch=='D') change('G');
        if (ch=='E') change('B');
        if (ch=='F') change('A');
        if (ch=='G') change('D');
        if (ch=='H') change('C');
    }
    return 0;
}
int main()
{
    while (true){
        scanf("%d",&map[1][3]);
        if (map[1][3]==0) return 0;
        scanf("%d",&map[1][5]);
        scanf("%d",&map[2][3]);
        scanf("%d",&map[2][5]);
        for (int i=1;i<=7;i++) scanf("%d",&map[3][i]);
        scanf("%d",&map[4][3]);
        scanf("%d",&map[4][5]);
        for (int i=1;i<=7;i++) scanf("%d",&map[5][i]);
        scanf("%d",&map[6][3]);
        scanf("%d",&map[6][5]);
        scanf("%d",&map[7][3]);
        scanf("%d",&map[7][5]);
    int flag=1;
    int num[4],maxn=0,maxnum=-1;
    memset(num,0,sizeof(num));
    for (int i=3;i<=5;i++)
        for (int j=3;j<=5;j++)
            num[map[i][j]]++;
    for (int i=1;i<=3;i++){
        if (num[i]==8){
            cout<<"No moves needed"<<endl;
            cout<<i<<endl; 
            flag=0;
        }
    }
    if (flag==1){
       int t=0;
        while (!search(1,t))t++;}
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值