uva 10085 10085 - The most distant state

点击打开链接

题目意思: 八数码问题的变形,给定一个初始状态要求找到该状态很够到达的最远的状态,输出这个最远状态和路径。(特判,答案不唯一)

解题思路:  做过八数码的同学肯定觉得这一题很水吧,确实是的,要求我么找到最远的,我么知道广搜是只要有一个节点能够扩展就会继续往下扩展,那么当广搜不能在扩展的时候就是到最远的状态(不理解的可以自己画画图想想),其它就是八数码的一些注意问题了,见我博客就有详解.

代码:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <cstdio>
#include <stack>
#include <queue>
#include <cmath>
using namespace std;
#define MAXN 400000

struct State{
    int state[9];
    int pos;
    char ch[50];
}s[MAXN];
int dir[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
int factor[9] = {1,1,2,6,24,120,720,5040,40320};
int dirtion[4] = {'U','R','D','L'};
int vis[MAXN];
int t , flag;
State star;
queue<int>q;

//哈希函数
int Hash(int s[]){
    int i , j , temp , num;
    num = 0;
    for(i = 0 ; i < 9 ; i++){
        temp = 0;
        for(j = i+1 ; j < 9 ; j++){
            if(s[j] < s[i]) temp++;
        }
        num += factor[8-i]*temp;
    }
    return num;
}

//输出函数
void output(State ans){
    for(int i = 0 ; i < 9 ; i++){
        if(ans.state[i] == 9) printf("0");
        if(ans.state[i] != 9) printf("%d" , ans.state[i]);
        if(i == 2 || i == 5) printf("\n");
        if(i != 2 && i != 5) printf(" ");
    }
    printf("\n");
    for(int i = 0 ; i < strlen(ans.ch) ; i++) printf("%c" , ans.ch[i]);
    printf("\n\n");
} 

//广搜
void Bfs(){
    while(!q.empty()){
        State cur = s[q.front()] ; q.pop();
        int x ,y , c, r;
        flag = 0;
        x = cur.pos/3 ; y = cur.pos%3;
        //printf("%d %d\n" , x , y);
        for(int i = 0 ; i < 4 ; i++){
            r = x + dir[i][0] ; c = y+dir[i][1];
            if(r >=0 && c >= 0 && r < 3 && c < 3){
                State tmp = cur;
                tmp.state[tmp.pos] = tmp.state[r*3+c];
                tmp.pos = r*3+c ; tmp.state[tmp.pos] = 9;

                int hash = Hash(tmp.state);

                if(!vis[hash]){
                    vis[hash] = 1;
                    int len = strlen(tmp.ch);
                    tmp.ch[len]= dirtion[i];
                    s[hash] = tmp;
                    //output(tmp);
                    q.push(hash);
                    flag = 1;
                }
            }
        }
        if(!flag && q.empty()) output(cur);//如果不再扩展说明找到就直接输出
    }
}

int main(){
    //freopen("input.txt" , "r" , stdin);
    scanf("%d%*c" , &t);
    int i , j , n , k , cnt;
    for(cnt = 1 ; cnt <= t ; cnt++){
        k = 0;
        for( i = 0 ; i < 3 ; i++){
            for( j = 0 ; j < 3 ; k++ , j++){
                scanf("%d" , &n);
                star.state[k] = n;
                if(n == 0) { star.state[k] = 9 ; star.pos = i*3+j;}
            }
        }
        while(!q.empty()) q.pop();
        memset(vis , 0 , sizeof(vis));
        int hash = Hash(star.state);
        vis[hash] = 1 ; s[hash] = star;
        q.push(hash);
        printf("Puzzle #%d\n" , cnt);
        Bfs();
    }
    return 0;
}
时间: 2024-08-08 13:00:29

uva 10085 10085 - The most distant state的相关文章

UVA 10085:The most distant state

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=110&page=show_problem&problem=1026 类型: 隐式图搜索 原题: The 8-puzzle is a square tray in which eight square tiles are placed. The remaining ninth square is uncove

uva 10085 - The most distant state bfs+map

10085 - The most distant state    类似8数码的一道题,只是求变化次数最多的一种,也就是队列最后一层    用的bfs+map,map保存方向和这个节点的权值,懒得搞hash函数,就用map顺便当做hash用了    用字符串s存状态,s[9]表示0的位置    最后的方向dfs输出.    这题WA了一次,输出忘加case了--   /* author:jxy lang:C/C++ university:China,Xidian University **If

Continuous Multi-Step TD, Eligibility Traces and TD(λ): A brief note

Thanks Richard S. Sutton and Andrew G. Barto for their great work in Reinforcement Learning: An Introduction. We focus on episodic case only and deal with continuous state and action spaces. Suppose you already have the basic knowledge of TD(0) metho

Shanghai 2006 / UVa 1382 Distant Galaxy:枚举&amp;amp;扫描&amp;amp;动态维护

1382 - Distant Galaxy Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=460&page=show_problem&problem=4128 You are observing a distant galaxy using a telescope above the Astronomy Tower

uva 1382 - Distant Galaxy

点击打开链接uva 1382 题意:给出平面上的n个点,找出一个矩形,使得边界上含有尽量多的点 思路: 1 很清楚,如果输入的n个点在同一行或者同一列的话那么ans = n.还有一种情况就是n个点的横坐标和纵坐标只有2种,那么这种情况ans = n. 2 对于这一题我们考虑的是枚举矩形的上下边界(纵坐标),然后利用其它的方法求左右边界,见下图 3 对于竖线i,我们用left[i]表示竖线左边位于上下边界的点数(不包括位于竖线i), on[i]表示竖线上位于上下边界之间的点数(和on2[i]的区别

UVa 10603:Fill,经典倒水问题+隐式图搜索+dfs

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=110&page=show_problem&problem=1544 类型: 隐式图搜索 原题: There are three jugs with a volume of a, b and c liters. (a, b, and c are positive integers not greater th

UVa 10274:Fans and Gems, 神牛Rujia Liu的神题(三)

题目链接: UVa :  http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1215 zoj    : http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1237 类型:  隐式图搜索+恶心模拟+哈希判重+bfs+dfs 原题: Tomy's

UVa 11198:Dancing Digits,Rujia Liu的神题

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2139 类型: 隐式图搜索,  BFS, 哈希判重,模拟 原题: Digits like to dance. One day, 1, 2, 3, 4, 5, 6, 7 and 8 stand in a line to have a wonderful

UVa 537 Artificial Intelligence? (字符串查找与匹配)

537 - Artificial Intelligence? Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=96&page=show_problem&problem=478 Physics teachers in high school often think that problems given as text