zoukankan      html  css  js  c++  java
  • luogu1979 华容道

    题目大意

      小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次。于是,他想到用编程来完成华容道:给定一种局面,华容道是否根本就无法完成,如果能完成,最少需要多少时间。

    小 B 玩的华容道与经典的华容道游戏略有不同,游戏规则是这样的:

    1. 在一个 n*m 棋盘上有 n*m 个格子,其中有且只有一个格子是空白的,其余 n*m-1个格子上每个格子上有一个棋子,每个棋子的大小都是 1*1 的;
    2. 有些棋子是固定的,有些棋子则是可以移动的;
    3. 任何与空白的格子相邻(有公共的边)的格子上的棋子都可以移动到空白格子上。 游戏的目的是把某个指定位置可以活动的棋子移动到目标位置。

    给定一个棋盘,游戏可以玩 q 次,当然,每次棋盘上固定的格子是不会变的,但是棋盘上空白的格子的初始位置、指定的可移动的棋子的初始位置和目标位置却可能不同。第 i 次玩的时候,空白的格子在第 EX_i 行第 EY_i 列,指定的可移动棋子的初始位置为第 SX_i 行第 SY_i 列,目标位置为第 TX_i 行第 TY_i 列。
    假设小 B 每秒钟能进行一次移动棋子的操作,而其他操作的时间都可以忽略不计。请你告诉小 B 每一次游戏所需要的最少时间,或者告诉他不可能完成游戏。

    题解

      最简单的方法莫过于让空格瞎走,此法能得到70分(当初模拟时太过于紧张只想得那30分,数组开小了。。。记住,数组够,就开大)。

      我们想到,要想让移动过的控制的棋子向相邻的格子$q$动,空格必须先到达$q$。移动的时间有多长呢?因为规定了控制的棋子移动过,故之间空格一定在控制的棋子的另一个相邻格子$p$中,空格所要移动的距离便是从$p$到$q$避开棋子所在位置的最短路径长度$d$。也就是说,我们用控制棋子的位置和空格的位置作为状态(棋子移动过,故空格必定与控制棋子相邻,故可以用一条连接两个格子对应节点的有向边来表示),表示空格移动一步的两个状态间的转移权值为$d+1$。在一个新图中,把状态作为状态节点,转移为状态边,虚拟一个状态起始点向所有to节点为控制节点的边对应的状态节点连一条空格初始位置到该边from点的最短距离的状态边,所有to节点为终止节点的边对应的状态节点与虚拟终止节点连一条边权为0的状态边,跑一遍Dijkstra即可。

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    using namespace std;
    
    const int MAX_ROW = 35, MAX_COL = 35, INF = 0x3f3f3f3f;
    bool IsWall[MAX_ROW][MAX_COL];
    int TotRow, TotCol;
    int StComRow, StComCol, StEmpRow, StEmpCol, TarComRow, TarComCol;
    
    struct SPGraph//shortest path
    {
        static const int MAX_NODE = MAX_ROW * MAX_COL * 4;
        static const int MAX_EDGE = MAX_ROW * MAX_COL * 16;
    
        struct Node;
        struct Edge;
    
        struct Node
        {
            Edge *Head;
            int Dist;
            bool Done;
        }_nodes[MAX_NODE], *Start;
        int TotNode;
    
        struct Edge
        {
            Node *To, *From;
            Edge *Next;
            int Weight;
        }_edges[MAX_EDGE];
        int _eCount;
    
        struct HeapNode
        {
            Node *Cur;
            int Dist;
    
            HeapNode(Node *cur, int dist):Cur(cur),Dist(dist){}
    
            bool operator < (const HeapNode& a) const
            {
                return Dist > a.Dist;
            }
        };
    
        void Init(int n, int s)
        {
            TotNode = n;
            Start = _nodes + s;
        }
    
        void AddEdge(int u, int v, int w)
        {
            Edge *e = _edges + ++_eCount;
            e->To = _nodes + v;
            e->From = _nodes + u;
            e->Weight = w;
            e->Next = _nodes[u].Head;
            _nodes[u].Head = e;
        }
    
        void PopEdge()
        {
            Edge *e = _edges + _eCount;
            Edge **next = &e->From->Head;
            while (true)
            {
                if (*next == e)
                {
                    *next = e->Next;
                    _eCount--;
                    return;
                }
                else
                    next = &(*next)->Next;
            }
        }
    
        void Dijkstra()
        {
            static priority_queue<HeapNode> q;
            for (int i = 1; i <= TotNode; i++)
            {
                _nodes[i].Dist = INF;
                _nodes[i].Done = false;
            }
            Start->Dist = 0;
            q.push(HeapNode(Start, 0));
            while (!q.empty())
            {
                HeapNode curHeapNode = q.top();
                q.pop();
                Node *cur = curHeapNode.Cur;
                if (cur->Done)
                    continue;
                cur->Done = true;
                for (Edge *e = cur->Head; e; e = e->Next)
                {
                    if (cur->Dist + e->Weight < e->To->Dist)
                    {
                        e->To->Dist = cur->Dist + e->Weight;
                        q.push(HeapNode(e->To, e->To->Dist));
                    }
                }
            }
        }
    }s;
    
    struct OrgGraph
    {
        static const int MAX_EDGE = MAX_ROW * MAX_COL * 4;
    
        struct Node;
        struct Edge;
    
        struct Node
        {
            Edge *Head;
            int Dist;
            int Row, Col;
        }_nodes[MAX_ROW][MAX_COL];
    
        struct Edge
        {
            Node *To, *From;
            Edge *Next;
        }_edges[MAX_EDGE];
        int _eCount;
    
        void AddEdge(int row1, int col1, int row2, int col2)
        {
            Node *from = _nodes[row1] + col1, *to = _nodes[row2] + col2;
            Edge *e = _edges + ++_eCount;
            e->To = to;
            e->From = from;
            e->Next = from->Head;
            from->Head = e;
        }
    
        void GetDist_Bfs(Node *start, Node *skip)
        {
            static queue<Node*> q;
            while (!q.empty())
                q.pop();
            for (int row = 1; row <= TotRow; row++)
                for (int col = 1; col <= TotCol; col++)
                    _nodes[row][col].Dist = -1;
            start->Dist = 0;
            q.push(start);
            while (!q.empty())
            {
                Node *cur = q.front();
                q.pop();
                for (Edge *e = cur->Head; e; e = e->Next)
                {
                    if (e->To == skip)
                        continue;
                    if (e->To->Dist >= 0)
                        continue;
                    e->To->Dist = cur->Dist + 1;
                    q.push(e->To);
                }
            }
        }
    }g;
    
    void BuildG()
    {
        for (int row = 1; row <= TotRow; row++)
            for (int col = 1; col <= TotCol; col++)
                g._nodes[row][col].Row = row, g._nodes[row][col].Col = col;
        const int Dir[4][2] = { {1, 0}, {0, 1}, {-1, 0}, {0, -1} };
        for (int row = 1; row <= TotRow; row++)
            for (int col = 1; col <= TotCol; col++)
            {
                if (IsWall[row][col])
                    continue;
                for (int i = 0; i < 4; i++)
                {
                    int row1 = row + Dir[i][0], col1 = col + Dir[i][1];
                    if (IsWall[row1][col1])
                        continue;
                    g.AddEdge(row, col, row1, col1);
                }
            }
    }
    
    void BuildS()
    {
        s.Init(g._eCount + 2, g._eCount + 1);
        for (int i = 1; i <= g._eCount; i++)
        {
            OrgGraph::Edge *eCur = g._edges + i;
            g.GetDist_Bfs(eCur->From, eCur->To);
            for (OrgGraph::Edge *e = eCur->To->Head; e; e = e->Next)
            {
                if (e->To->Dist == -1)
                    continue;
                s.AddEdge(eCur - g._edges, e - g._edges, e->To->Dist + 1);
            }
        }
    }
    
    int GetAns()
    {
        int cnt = 0;
        OrgGraph::Node *StCom = g._nodes[StComRow] + StComCol;
        OrgGraph::Node *StEmp = g._nodes[StEmpRow] + StEmpCol;
        OrgGraph::Node *TarCom = g._nodes[TarComRow] + TarComCol;
        if (StCom == TarCom)
            return 0;
        g.GetDist_Bfs(StEmp, StCom);
        for (OrgGraph::Edge *e1 = StCom->Head; e1; e1 = e1->Next)
            for (OrgGraph::Edge *e2 = e1->To->Head; e2; e2 = e2->Next)
            {
                if (e2->To != StCom)
                    continue;
                if (e2->From->Dist == -1)
                    continue;
                s.AddEdge(g._eCount + 1, e2 - g._edges, e2->From->Dist);
                cnt++;
            }
        for (OrgGraph::Edge *e1 = TarCom->Head; e1; e1 = e1->Next)
            for (OrgGraph::Edge *e2 = e1->To->Head; e2; e2 = e2->Next)
            {
                if (e2->To != TarCom)
                    continue;
                s.AddEdge(e2 - g._edges, g._eCount + 2, 0);
                cnt++;
            }
        s.Dijkstra();
        while (cnt--)
            s.PopEdge();
        int ans = s._nodes[g._eCount + 2].Dist;
        if (ans == INF)
            ans = -1;
        return ans;
    }
    
    int main()
    {
        memset(IsWall, true, sizeof(IsWall));
        int qCnt;
        scanf("%d%d%d", &TotRow, &TotCol, &qCnt);
        for (int row = 1; row <= TotRow; row++)
            for (int col = 1; col <= TotCol; col++)
            {
                int x;
                scanf("%d", &x);
                IsWall[row][col] = !x;
            }
        BuildG();
        BuildS();
        while (qCnt--)
        {
            scanf("%d%d%d%d%d%d", &StEmpRow, &StEmpCol, &StComRow, &StComCol, &TarComRow, &TarComCol);
            printf("%d
    ", GetAns());
        }
        return 0;
    }
    

      

  • 相关阅读:
    智器SmartQ T7实体店试用体验
    BI笔记之SSAS库Process的几种方案
    PowerTip of the Day from powershell.com上周汇总(八)
    PowerTip of the Day2010071420100716 summary
    PowerTip of the Day from powershell.com上周汇总(十)
    PowerTip of the Day from powershell.com上周汇总(六)
    重新整理Cellset转Datatable
    自动加密web.config配置节批处理
    与DotNet数据对象结合的自定义数据对象设计 (二) 数据集合与DataTable
    在VS2003中以ClassLibrary工程的方式管理Web工程.
  • 原文地址:https://www.cnblogs.com/headboy2002/p/9734368.html
Copyright © 2011-2022 走看看