HDU Tobo or not Tobo (IDA*)

本文介绍了一款名为Tobo的游戏,玩家需要通过旋转棋盘上的四个转盘来将棋盘恢复到初始状态。文章详细解释了如何使用IDA*搜索算法结合曼哈顿距离作为估价函数,来找出解决游戏所需的最小步骤数。通过将曼哈顿距离加上3/4,可以得到至少需要多少步的下界,从而实现有效的剪枝。

Tobo or not Tobo

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 9   Accepted Submission(s) : 6
Problem Description
The game of Tobo is played on a plastic board designed into a 3 × 3 grid with cells numbered from 1 to 9 as shown in figure (a). The grid has four dials (labeled ``A" to ``D" in the figure.) Each dial can be rotated in 90 degrees increment in either direction. Rotating a dial causes the four cells currently adjacent to it to rotate along. For example, figure (b) shows the Tobo after rotating dial ``A" once in a clockwise direction. Figure (c) shows the Tobo in figure (b) after rotating dial ``D" once in a counterclockwise direction.  
Kids love to challenge each other playing the Tobo. Starting with the arrangement shown in figure (a), (which we'll call the standard arrangement,) one kid would randomly rotate the dials, X number of times, in order to ``shuffle" the board. Another kid then tries to bring the board back to its standard arrangement, taking no more than X rotations to do so. The less rotations are needed to restore it, the better. This is where you see a business opportunity. You would like to sell these kids a program to advise them on the minimum number of steps needed to bring a Tobo back to its standard arrangement.
 

 

Input
Your program will be tested on one or more test cases. Each test case is specified on a line by itself. Each line is made of 10 decimal digits. Let's call the first digit Y . The remaining 9 digits are non-zeros and describe the current arrangement of the Tobo in a row-major top-down, left-to-right ordering. The first sample case corresponds to figure (c).
The last line of the input file is a sequence of 10 zeros.
 

 

Output
For each test case, print the result using the following format:
k . R
where k is the test case number (starting at 1,) is a single space, and R is the minimum number of rotations needed to bring the Tobo back to its standard arrangement. If this can't be done in Y dials or less, then R = -1.
 

 

Sample Input
3413569728 1165432789 0000000000
 

 

Sample Output
1. 2 2. -1
 

 

Source
2008 ANARC
 
 

继续IDA*搜索,估价函数H仍然是曼哈顿距离,每一次转换会改变4个位置的曼哈顿距离,分别改变1,所以把曼哈顿距离和+3/4便可以作为H函数,表示至少需要多少步,一个DFS的剪枝。

这题最多九步,BFS应该也无压力

 

 

 

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

int a[9];
int depth,flag;
char str[10];

int rotation[4][4]={{0,1,4,3},{1,2,5,4},{3,4,7,6},{4,5,8,7}};

int abs(int x){
    return x<0?-x:x;
}

int H(int *b){
    int ans=0;
    for(int i=0;i<9;i++)
        ans+=abs(i/3-(b[i]-1)/3)+abs(i%3-(b[i]-1)%3);
    return (ans+3)/4;
}

void change(int *b,int k){
    if(k&1){    //顺时针旋转
        k>>=1;
        int tmp=b[rotation[k][3]];
        for(int i=3;i>0;i--)
            b[rotation[k][i]]=b[rotation[k][i-1]];
        b[rotation[k][0]]=tmp;
    }else{  //逆时针旋转
        k>>=1;
        int tmp=b[rotation[k][0]];
        for(int i=1;i<4;i++)
            b[rotation[k][i-1]]=b[rotation[k][i]];
        b[rotation[k][3]]=tmp;
    }
}

void IDAstar(int *b,int curDepth,int dir){
    if(flag)
        return ;
    if(H(b)>curDepth)
        return ;
    if(curDepth==0 && H(b)==0){
        flag=1;
        return ;
    }
    for(int i=0;i<8;i++){
        if(dir!=-1 && dir/2==i/2 && (dir%2)^(i%2))
            continue;
        int tmp[9];
        for(int j=0;j<9;j++)
            tmp[j]=b[j];
        change(tmp,i);
        IDAstar(tmp,curDepth-1,i);
    }
}

int main(){

    //freopen("input.txt","r",stdin);

    int cases=0;
    while(~scanf("%s",str) && strcmp(str,"0000000000")){
        int t=str[0]-'0';
        for(int i=0;i<9;i++)
            a[i]=str[i+1]-'0';
        flag=0;
        for(depth=H(a);depth<=t;depth++){
            IDAstar(a,depth,-1);
            if(flag){
                printf("%d. %d\n",++cases,depth);
                break;
            }
        }
        if(!flag)
            printf("%d. -1\n",++cases);
    }
    return 0;
}

 

 

 

<think>好的,我需要帮助用户找到HDU OJ 1004问题的C语言解决方案。首先,我得回忆一下HDU 1004的具体内容。这个问题通常是关于统计出现次数最多的颜色或字符串。题目大意是输入多个测试用例,每个用例包含n个字符串,要求输出出现次数最多的那个。如果有多个并列最多,则按输入顺序输出最先出现的那个。 接下来,我需要确认用户的需求。他们明确提到需要C语言的实现方法,所以我要确保提供的代码是用C写的,并且符合题目的要求。同时,用户可能还希望了解解题思路,这样他们能更好地理解代码逻辑。 首先,我应该考虑如何高效统计每个字符串的出现次数。在C语言中,可以使用结构体数组来存储字符串及其出现次数。每次输入一个新的字符串时,遍历已有的结构体数组,检查是否已经存在。如果存在,则增加计数;如果不存在,则添加到数组中。最后,遍历整个数组找出出现次数最多的字符串。 接下来,需要注意内存管理。由于题目中每个字符串的最大长度可能有限,比如通常OJ题目中的字符串长度限制为100字符左右,所以需要为每个字符串分配足够的空间,比如char color[101]这样的数组。结构体数组的大小也需要足够大,比如假设最多有1000个不同的字符串,但具体可能需要根据题目要求调整。 然后,处理输入输出。HDU OJ的输入通常是多个测试用例,每个用例以n=0结束。在代码中,需要使用循环读取每个测试用例,直到n为0时停止。对于每个用例,先读取n,然后读取n个字符串。需要注意的是,每次处理完一个测试用例后,要重置结构体数组,避免上一个用例的数据影响当前结果。 在代码结构方面,主函数应该包含读取输入、处理每个测试用例的循环。处理每个字符串时,使用一个结构体数组来跟踪每个字符串的出现次数。使用strcmp来比较字符串,strcpy来复制字符串到结构体中。 此外,还需要考虑效率问题。如果n很大,比如达到1000,每次都要遍历整个数组,时间复杂度是O(n²),但在OJ中这可能仍然可以通过,因为实际测试数据可能不会太大。如果时间超限,可能需要优化数据结构,比如使用哈希表,但在C语言中实现哈希表较为复杂,对于新手来说可能较难理解,所以可能还是以简单方法为主。 然后,编写代码示例。需要包含必要的头文件,如stdio.h和string.h。定义结构体,比如struct ColorCount { char color[101]; int count; }; 然后声明结构体数组,并在每个测试用例开始时将其重置。 最后,测试代码的正确性。例如,输入多个测试用例,检查输出是否正确,特别是当有多个字符串出现次数相同的情况,是否输出最先出现的那个。 可能遇到的问题包括:字符串输入时的缓冲区问题,比如使用scanf读取字符串时可能会遇到空格,但题目中说明每个颜色是一个单独的字符串,所以可以使用scanf的%s格式符,它自动以空格或换行分隔。此外,结构体数组的索引需要在每次测试用例开始时重置为0,以确保每个用例独立处理。 总结一下,解题步骤为: 1. 读取测试用例数量n,直到n为0。 2. 对于每个用例,初始化结构体数组和索引。 3. 读取n个字符串,逐个处理,更新结构体数组中的计数。 4. 遍历结构体数组,找到出现次数最多的字符串。 5. 输出结果,并处理多个测试用例之间的换行要求。 现在,根据这些思路,编写C语言代码,并确保代码符合HDU OJ的要求,比如输入输出格式正确,没有多余的打印内容,处理多个测试用例等。</think>### HDU OJ 1004问题C语言解决方案 **问题描述** HDU OJ 1004要求统计多个测试用例中每个字符串出现的次数,输出出现次数最多的字符串。如果有多个并列最多的字符串,输出最先出现的那个。 --- #### 解题思路 1. **数据结构选择**:使用结构体数组存储字符串及其出现次数。 ```c struct ColorCount { char color[101]; // 存储字符串(假设最大长度为100) int count; // 出现次数 }; ``` 2. **输入处理**:循环读取测试用例,直到输入`n=0`时结束。 3. **字符串统计**: - 对每个新字符串,遍历结构体数组检查是否已存在。 - 若存在则增加计数,否则添加到数组中。 4. **结果输出**:遍历结构体数组,找到最大计数的字符串。 --- #### C语言实现代码 ```c #include <stdio.h> #include <string.h> #define MAX_STR_LEN 100 #define MAX_COLORS 1000 // 假设最多有1000种不同字符串 struct ColorCount { char color[MAX_STR_LEN + 1]; int count; }; int main() { int n; while (scanf("%d", &n) == 1 && n != 0) { struct ColorCount colors[MAX_COLORS]; int color_count = 0; // 当前已记录的不同字符串数量 for (int i = 0; i < n; i++) { char current_color[MAX_STR_LEN + 1]; scanf("%s", current_color); // 检查是否已存在 int found = 0; for (int j = 0; j < color_count; j++) { if (strcmp(colors[j].color, current_color) == 0) { colors[j].count++; found = 1; break; } } // 若不存在则添加 if (!found) { strcpy(colors[color_count].color, current_color); colors[color_count].count = 1; color_count++; } } // 查找出现次数最多的字符串 int max_count = 0; char result[MAX_STR_LEN + 1]; for (int i = 0; i < color_count; i++) { if (colors[i].count > max_count) { max_count = colors[i].count; strcpy(result, colors[i].color); } } printf("%s\n", result); } return 0; } ``` --- #### 代码说明 1. **输入处理**: - 使用`while (scanf("%d", &n) == 1 && n != 0)`循环读取测试用例。 - 每次循环初始化结构体数组`colors`和计数器`color_count`[^1]。 2. **字符串统计**: - 对每个输入的字符串,遍历结构体数组检查是否已存在(`strcmp`比较)。 - 不存在时添加到数组末尾,并初始化计数为1。 3. **性能优化**: - 时间复杂度为$O(n^2)$,适用于$n \leq 1000$的情况。 - 若$n$较大,可改用哈希表优化到$O(n)$,但实现复杂度较高。 4. **输出规则**: - 当多个字符串出现次数相同时,按输入顺序输出第一个出现的字符串(隐含逻辑:后添加的字符串不会覆盖先前的最大值)。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值