UVa1343 - The Rotation Game

因为8个转轮对应的位置比较没有规律,需要提前将这些位置存在数组中,方便旋转操作和回溯法的归位操作。

利用数组来人为储存没有规律的数字。

IDA*合了bfs步数最少和dfs字典序最小的优点。

#include<cstdio>
#include<algorithm>
#define maxn 500
using namespace std;
int block[24];
int roller[8][7]={
{0,2,6,11,15,20,22},{1,3,8,12,17,21,23},
{10,9,8,7,6,5,4},{19,18,17,16,15,14,13},
{23,21,17,12,8,3,1},{22,20,15,11,6,2,0},
{13,14,15,16,17,18,19},{4,5,6,7,8,9,10}
};
int back[8]={5,4,7,6,1,0,3,2};
int goalpos[8] = {6,7,8,11,12,15,16,17};
int op[100000];
int rot(int *blk,int dir){
    for(int i=1;i<7;i++){
        int tmp = blk[roller[dir][i]];
        blk[roller[dir][i]] = blk[roller[dir][i-1]];
        blk[roller[dir][i-1]]=tmp;
    }
}
int dfs(int d,int maxd){
    if(d == maxd){
        int ok=1;
        for(int i=1;i<8;i++){
            if(block[goalpos[i]]!=block[goalpos[i-1]]){
                ok=0;
                break;
            }
        }
        if(ok){
            if(!d){
                printf("No moves needed\n%d\n",block[6]);
                return 1;
            }
            for(int i=0;i<d;i++) printf("%c",op[i]+'A');
            printf("\n%d\n",block[6]);
            return 1;
        }
        return 0;
    }
    else {

        int diff=0;
        int a=0,b=0,c=0;
        for(int i=0;i<8;i++){
            if(block[goalpos[i]]==1) a++;
            if(block[goalpos[i]]==2) b++;
            if(block[goalpos[i]]==3) c++;
        }
        diff = 8-max(max(a,b),c);
        if(d + diff > maxd) return 0;

        for(int i=0;i<8;i++){
            op[d]=i;
            rot(block,i);
            if(dfs(d+1,maxd)) return 1;
            rot(block, back[i]);
        }
    }
    return 0;
}
int main()
{
    while(1)
    {
        for(int i=0;i<24;i++){
            scanf("%d",&block[i]);
            if(!block[i]) return 0;
        }
        for(int maxd=0;;maxd++){
            if(dfs(0,maxd)){
                break;
            }
        }
    }
    return 0;
}
时间: 2024-10-24 13:02:13

UVa1343 - The Rotation Game的相关文章

UVA-1343 The Rotation Game (IDA*)

题目大意:数字1,2,3都有八个,求出最少的旋转次数使得图形中间八个数相同.旋转规则:对于每一长行或每一长列,每次旋转就是将数据向头的位置移动一位,头上的数放置到尾部.若次数相同,则找出字典序最小旋转次序. 题目分析:IDA*,若当前在第cur层,中间八个数中1,2,3的个数分别为a,b,c.则d=8-max(a,b,c)便是达到目标还需要的理想次数,若cur+d>maxd,则剪枝.<入门经典>上提供了一种BFS的思路,分别以1,2,3为目标广搜3次,不过自己的码力还是太弱,并没有用那种

UVa 1343 The Rotation Game (状态空间搜索 &amp;&amp; IDA*)

题意:有个#字型的棋盘,2行2列,一共24个格. 如图:每个格子是1或2或3,一共8个1,8个2,8个3. 有A~H一共8种合法操作,比如A代表把A这一列向上移动一个,最上面的格会补到最下面. 求:使中心8个格子数字一致的最少步骤,要输出具体的操作步骤及最终中心区域的数字.如果有多个解,输出字典序最小的操作步骤. 分析 : 还是状态空间的搜索,对象就是一个数字序列,判断中心位置是否一样,可以看出如果使用BFS,每一层还是爆炸,所以使用IDA*,关键还是模拟操作和h函数,这里的h函数是这样定义的,

(IDA*)POJ 2286 The Rotation Game

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

outdated: 4.Rotation

修改部分位于双行星号内. 1 #include <windows.h> 2 #include <gl/glew.h> 3 #include <gl/glut.h> 4 #include <GL/GLAUX.H> 5 6 /* 7 * Every OpenGL program is linked to a Rendering Context. 8 * A Rendering Context is what links OpenGL calls to the D

HDU 4708 Rotation Lock Puzzle (贪心+模拟)

Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1668    Accepted Submission(s): 530 Problem Description Alice was felling into a cave. She found a strange door with a number

hduoj 4708 Rotation Lock Puzzle 2013 ACM/ICPC Asia Regional Online —— Warmup

http://acm.hdu.edu.cn/showproblem.php?pid=4708 Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Alice was felling into a cave. She found a strange door with a number square m

【HackerRank】Game Of Rotation

题目连接:Game Of Rotation Mark is an undergraduate student and he is interested in rotation. A conveyor belt competition is going on in the town which Mark wants to win. In the competition, there's A conveyor belt which can be represented as a strip of 1

Centos 7周期性任务、日志rotation、过滤和core压缩的实现

Centos 7周期性任务日志rotation.过滤和core压缩的实现 1.centos7中cron脚本及其调用脚本的实现示例 [[email protected] cron.d]# pwd /etc/cron.d [[email protected] cron.d]# cat syslog # Run mysys activity accounting tool every 10 minutes */5 * * * * root /usr/sbin/mysyslog.sh 2>&1 &g

POJ2286 The Rotation Game

The Rotation Game Time Limit: 15000MS   Memory Limit: 150000KB   64bit IO Format: %I64d & %I64u 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,