zoukankan      html  css  js  c++  java
  • uva-10422-骑士-搜索题

    题意:

    给你一个5X5的图,棋盘上骑士的走法自己去百度,问能不能在10步内走到目标图,

    解题思路:

    从目标图开始往前走10步,保存所有能走到的图,然后输入,查找是否存在这个图,存在就是可以走到,不存在,走不到

    使用map集合保存状态图,优先队列对状态重排序

    #include <iostream>
    #include <stdio.h>
    #include <string>
    #include <map>
    #include <queue>
    
    using namespace std;
    
    struct Dir
    {
            int x, y;
    };
    
    struct Node
    {
            int x,y;
            string str;
            int step;
            bool operator () (const Node& n1,const Node& n2)const
            {
                return n1.step>n2.step;
            }
    };
    //使用优先队列对状态重排序
    priority_queue<Node, vector<Node>, Node> q;
    
    Dir dir[] =
    {
            { +1, -2 },
            { +2, +1 },
            { +2, -1 },
            { -1, +2 },
            { -2, +1 },
            { -2, -1 },
            { -1, -2 },
            { +1, +2 }
    };
    
    
    
    map<string, int> maps;
    const int N=5;
    
    string nextStr(int cr, int cc, int r, int c, string cur)
    {
        string str(cur);
        char curc = cur[cr * N + cc];
        char nextc = cur[r * N + c];
        str[cr * N + cc] = nextc;
        str[r * N + c] = curc;
        return str;
    }
    
    
    void bfs()
    {
        while(q.empty()==false)
        {
            Node n = q.top();
            q.pop();
            string cur = n.str;
            int depth = n.step;
            int cr = n.x;
            int cc = n.y;
            //已经到10步
            if(depth==10)
                return;
            for(int i = 0; i < 8; i++)
            {
                    //nx row
                    int r = dir[i].x + cr;
                    //col
                    int c = dir[i].y + cc;
                    if(r < 0 || c < 0 || r >= N || c >= N)
                        continue;
                    string str=nextStr(cr, cc, r, c, cur);
                    if(maps.find(str)!=maps.end())
                        continue;
                    maps[str] = depth+1;
                    Node nNode;
                    nNode.x=r;
                    nNode.y=c;
                    nNode.step=depth+1;
                    nNode.str=str;
                    q.push(nNode);
            }
        }
    }
    
    int main()
    {
        freopen("d://1.text", "r", stdin);
        //5x5图
        //目标图
        string aim =
                "11111"
                "01111"
                "00 11"
                "00001"
                "00000";
        Node iNode;
        iNode.step=0;
        iNode.str = aim;
        iNode.x = 2;
        iNode.y = 2;
        q.push(iNode);
        maps[aim] = 0;
        bfs();
        int total;
        cin >> total;
        getchar();
        while (total--)
        {
            string str = "";
            string inStr = "";
            for(int i = 0; i < 5; i++)
            {
                getline(cin, inStr);
                str += inStr;
            }
            map<string, int>::iterator s;
            int ok = 0;
            for(s = maps.begin(); s != maps.end(); ++s)
            {
                if(s->first == str)
                {
                    ok=1;
                    cout<<"Solvable in "<<s->second<<" move(s)."<<endl;
                    break;
                }
            }
            if(!ok)
                cout<<"Unsolvable in less than 11 move(s)."<<endl;
    
        }
    
        return 0;
    }
  • 相关阅读:
    [luoguP2342] 叠积木(并查集)
    [luoguP2147] [SDOI2008]Cave 洞穴勘测(并查集 || lct)
    [POJ1611]The Suspects(并查集)
    [POJ2912]Rochambeau(并查集)
    网站图片增强JS插件2.0(兼容IE&FF)
    jQuery简单纯文字提示条
    复选框--全选/全不选/反选。简易版
    Magic Grid ComboBox JQuery 版
    SWFObject 的基本使用方法
    全局Timestamp管理器 检测js执行时间
  • 原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/9350366.html
Copyright © 2011-2022 走看看