POJ 1077 Eight IDA*

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;

int a[10];
int goal[][2] = {{0,0},{0,0},{0,1},{0,2},{1,0},{1,1},{1,2},{2,0},{2,1},{2,2}};
int move[4][2]={{-1,0},{0,-1},{0,1},{1,0}};
int flag;
char op[] = "ulrd";
int path[1000];
int ans;
int nixu(){
    int ni  = 0;
    for(int i = 0;i < 9;i++){
        if(a[i] == 9) continue;
        for(int j = i+1;j < 9;j++){
            if(a[j] == 9) continue;
            if(a[j] < a[i]){
                ni ++;
            }
        }
    }
    return !(ni%2);
}
int co(){
    int cost = 0;
    int cc = 0;
    for(int i = 0;i < 3;i++){
        for(int j = 0;j < 3;j++){
            cost += abs(i-goal[a[cc]][0])+ abs(j-goal[a[cc]][1]);
            cc++;
        }
    }
    return cost;
}
void dfs(int cur,int len,int limit,int pre){
    int cc = co();
    if(cc == 0){
        flag = 1;
        ans = len;
        return;
    }
    if(len <= limit){
        int x = cur/3;
        int y = cur%3;
        for(int i = 0;i < 4;i++){
            if(len == 0 || (i + pre != 3)){
                int sx = x + move[i][0];
                int sy = y + move[i][1];
                if(sx < 3 && sx >=0 && sy < 3 && sy >= 0){
                    swap(a[cur],a[sx*3+sy]);
                    cc = co();
                    if(cc + len <= limit && !flag){
                        path[len] = i;
                        dfs(sx*3+sy,len+1,limit,i);
                        if(flag) return;
                    }
                    swap(a[cur],a[sx*3+sy]);
                }
            }
        }
    }
}
int main(){
    char ch[10];
    int st;
    //freopen("out.txt","w",stdout);
    for(int i = 0;i < 9;i++){
        scanf("%s",ch);
        a[i] = ch[0];
        if(ch[0] == ‘x‘) st = i,a[i]  = ‘9‘;
        a[i] -= ‘0‘;
    }
    int tmp[15];
    for(int i = 0;i < 9;i++){
        tmp[i] = a[i];
    }
    if(nixu()){
        flag = 0;
        int limit = 0;
        while(!flag){
            for(int i = 0;i < 9;i++){
                a[i] = tmp[i];
            }
            dfs(st,0,limit,0);
            if(!flag)
                limit++;
        }
        for(int i = 0;i < ans;i++){
            putchar(op[path[i]]);
        }
        printf("\n");
    }
    else{
        printf("unsolvable\n");
    }
    return 0;
}

IDA* 第一题,,,调了整整的一天!醉了~

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-11 07:43:05

POJ 1077 Eight IDA*的相关文章

HDU 1043 POJ 1077 八数码问题

以下内容转载自:http://www.cnblogs.com/goodness/archive/2010/05/04/1727141.html 八数码的八境界 研究经典问题,空说不好,我们拿出一个实际的题目来演绎.八数码问题在北大在线测评系统中有一个对应的题,题目描述如下: Eight Time Limit: 1000MS    Memory Limit: 65536K  Special Judge Description The 15-puzzle has been aroundfor ove

POJ - 1077 Eight

题意:经典八数码问题 思路:HASH+BFS #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 500000; const int size = 1000003; typedef int State[9]; char str[30]; int state[9],goal[9]={

poj 1077 八数码(BFS+康托展开)

1 /* 2 题意:八数码问题,给出3*3的矩阵含1~8以及x,给出一个符合的解使得移动后的矩阵的顺序为1~8,最后为x 3 4 题解:BFS 5 需要用到康托展开来表示状态,不然数组无法完全表示所有状态,这样BFS就无法判断找不到解的情况(status 6 的0ms,0KB究竟是怎么做到的,简直不能想象=.=) 7 */ 8 #include <cstdio> 9 #include <cstring> 10 #include <queue> 11 #include &

POJ 1077 Eight(康托展开+BFS)

Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30176   Accepted: 13119   Special Judge Description The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've seen it. It is constructed with 15

HDU - 1043 - Eight / POJ - 1077 - Eight

先上题目: Eight Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11243    Accepted Submission(s): 3022Special Judge Problem Description The 15-puzzle has been around for over 100 years; even if you

poj 1077 Eight(双向bfs)

题目链接:http://poj.org/problem?id=1077 思路分析:题目要求在找出最短的移动路径,使得从给定的状态到达最终状态. <1>搜索算法选择:由于需要找出最短的移动路径,所以选择bfs搜索 <2>判重方法: 将空格视为数字9,则可以将状态的集合视为1-9的排列组合的集合,根据康托展开,将每一个状态映射到一个正整数中: 在由哈希表进行判重. <3>状态压缩:在搜索时使用将每个状态转换为一个由1-9组成的9位数字即可进行状态压缩与解压. <4&g

POJ 1077 A*

链接: http://poj.org/problem?id=1077 题意: 经典8数码问题,直接暴力bfs也能做,但是一定要先hash一下 题解: 这里的估价函数为当前状态下,所有的数字与其位置的之差的绝对值总和 话说我又被c++的string坑惨了 代码: 31 int vis[MAXN]; 32 int fac[] = { 1,1,2,6,24,120,720,5040,40320,362880 }; 33 34 int find(char s[]) {//康托展开 35 int res

Eight hdu 1043 poj 1077

Description The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've seen it. It is constructed with 15 sliding tiles, each with a number from 1 to 15 on it, and all packed into a 4 by 4 frame with one tile mis

BFS(八数码) POJ 1077 || HDOJ 1043 Eight

题目传送门1 2 题意:从无序到有序移动的方案,即最后成1 2 3 4 5 6 7 8 0 分析:八数码经典问题.POJ是一次,HDOJ是多次.因为康托展开还不会,也写不了什么,HDOJ需要从最后的状态逆向搜索,这样才不会超时.判重康托展开,哈希也可. POJ //#include <bits/stdc++.h> #include<iostream> #include<algorithm> #include<string> #include<stack