八数码

Problem 8: 八数码


Time Limit:1 Ms| Memory Limit:128 MB
Difficulty:3


Description
在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字。棋盘中留有一个空格,空格用0来表示。空格周围的棋子可以移到空格中。要求解的问题是:给出一种初始布局(初始状态)和目标布局(为了使题目简单,设目标状态为123804765),找到一种最少步骤的移动方法,实现从初始布局到目标布局的转变。 


Input
输入初试状态,一行九个数字,空格用0表示
Output
输入初试状态,一行九个数字,空格用0表示
Sample Input
283104765
Sample Output
4
Hint

搜索练习

思路:广搜,每个状态用字符串表示,因为用strcpy, strcmp更省时,并在结构体里记录0所在的位置,总共四种情况可以走,若空格位置在下两行,则它可以跟上面一个交换,即pos 和 pos - 3交换,若在上两行,可以跟下面一个交换,即和pos + 3 的位置, 若在后两列,可以跟前面一个交换,即pos - 1位置,若在前两列,则可以跟后面一个交换,即pos + 1,判断这个状态未走过,则入队。标记状态是否走过,用康托展开,利用0 - 8的全排列跟一个整数一一对应的关系来标记一个字符串是否走过。


#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
 
using namespace std;
 
typedef struct{
    char str[15];
    int pos;
    int step;
}node;
 
int vis[370000], f[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320};
char des[15] = "123804765";
 
int bfs(char str[]);
int cantor(char str[]);
int main(){
    char str[15];
     
    scanf("%s", str);
    printf("%d\n", bfs(str));
    return 0;
}
 
int bfs(char str[]){
    node q, p;
    queue <node> Q;
    int i, pos;
     
    strcpy(p.str, str);
    for(i = 0; i < 9; i++){
        if(str[i] == '0'){
            p.pos = i;
            break;
        }
    }
    p.step = 0;
    vis[cantor(p.str)] = 1;
    Q.push(p);
     
    while(!Q.empty()){
        q = Q.front();
        Q.pop();
    //  printf("pop:%s\n", q.str);
    //  getchar();
        if(!strcmp(q.str, des))
            return q.step;
         
        for(i = 0; i < 9; i++){
            if(q.str[i] == '0'){
                pos = i;
                break;
            }
        }
         
        if(pos > 2){
            strcpy(p.str, q.str);
            swap(p.str[pos], p.str[pos - 3]);
            p.pos = pos - 3;
            p.step = q.step + 1;
            if(!vis[cantor(p.str)]){
                Q.push(p);
                vis[cantor(p.str)] = 1;
            }
        }
         
        if(pos < 6){
            strcpy(p.str, q.str);
            swap(p.str[pos], p.str[pos + 3]);
            p.pos = pos + 3;
            p.step = q.step + 1;
            if(!vis[cantor(p.str)]){
                Q.push(p);
                vis[cantor(p.str)] = 1;
            }
        }
         
        if(pos % 3){
            strcpy(p.str, q.str);
            swap(p.str[pos], p.str[pos - 1]);
            p.pos = pos - 1;
            p.step = q.step + 1;
            if(!vis[cantor(p.str)]){
                Q.push(p);
                vis[cantor(p.str)] = 1;
            }
        }
         
        if(pos % 3 != 2){
            strcpy(p.str, q.str);
            swap(p.str[pos], p.str[pos + 1]);
            p.pos = pos + 1;
            p.step = q.step + 1;
            if(!vis[cantor(p.str)]){
                Q.push(p);
                vis[cantor(p.str)] = 1;
            }
        }
    }
}
 
int cantor(char str[]){
    int i, j, ans, temp;
    int record[15];
     
    memset(record, 0, sizeof(record));
    ans = 1;
    for(i = 0; i < 9; i++){
        temp = 0;
        for(j = '0'; j < str[i]; j++){
            if(!record[j - '0']){
                temp++;
            }
        }
        record[str[i] - '0'] = 1;
        ans += temp * f[8 - i];
    }
     
    return ans;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值