zoukankan      html  css  js  c++  java
  • 双端队列广搜

    电路维修

    http://acm.ocrosoft.com/problem.php?cid=1694&pid=0

    题目描述

    ​ Ha’nyu是来自异世界的魔女,她在漫无目的的四处漂流的时候,遇到了善良的少女Rika,从而被收留在地球上。Rika家里有一辆飞行车。有一天飞行车的电路板突然出现了故障,导致无法正常启动。

    ​ 电路板的整体结构是一个R行C列的网格,如图所示。每一个格点都是电线的接点。每个格子都包含一个电子元件。电子元件的主要部分是一个可旋转的,连接一条对角线上的两个连接点的短电缆。在旋转之后,它就可以连接另一条对角线的两个接点。电路板左上角的接点接入直流电源,右下角的接点接入飞行车的发动装置。

    ​ Ha‘nyu发现因为某些元件的方向不小心发生了改变,电路板可能处于断路状态。她准备通过计算,旋转最少数量的元件,使电源与发动装置通过若干条短电缆相连。不过电路的规模实在太大了,Ha‘nyu不擅长编程,希望你能够帮她解决这个问题。

    电路.png

    #include <bits/stdc++.h>
    using namespace std;
    
    #define x first
    #define y second
    
    typedef pair<int, int> PII;
    
    const int INF = 0x3f3f3f3f;
    const char nl = '
    ';
    const int N = 510;
    
    int n, m;
    char g[N][N];
    int dist[N][N];
    bool st[N][N];
    
    int bfs() {
        deque<PII> q;
        memset(dist, 0x3f, sizeof dist);
        memset(st, 0, sizeof st);
    
        char cs[5] = "\/\/";
        int dx[4] = {-1, -1, 1, 1}, dy[4] = {-1, 1, 1, -1};
        int ix[4] = {-1, -1, 0, 0}, iy[4] = {-1, 0, 0, -1};
    
        q.push_front({0, 0});
        dist[0][0] = 0;
    
        while (q.size()) {
            auto t = q.front();
            q.pop_front();
    
            int x = t.x, y = t.y;
            if (x == n && y == m) return dist[x][y];
    
            if (st[x][y]) continue;
            st[x][y] = true;
    
            for (int i = 0; i < 4; ++i) {
                int a = x + dx[i], b = y + dy[i];
                if (a < 0 || a > n || b < 0 || b > m) continue;
                int ga = x + ix[i], gb = y + iy[i];
                int w = (g[ga][gb] != cs[i]);
                int d = dist[x][y] + w;
                if (d <= dist[a][b]) {
                    dist[a][b] = d;
                    if (!w) q.push_front({a, b});
                    else q.push_back({a, b});
                }
            }
        }
        return -1;
    }
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    
        int tt;
        for (cin >> tt; tt; --tt) {
            cin >> n >> m;
            for (int i = 0; i < n; ++i)
                for (int j = 0; j < m; ++j)
                    cin >> g[i][j];
    
            if (n + m & 1) cout << "NO SOLUTION" << nl;
            else cout << bfs() << nl;
        }
    
        return 0;
    }
    
    /*
    
    1
    3 5
    \/\
    \///
    /\\
    
    1
    
    */
    
    
  • 相关阅读:
    csrf跨站请求伪造
    IO 之 InputStream 和 Reader
    javadoc tags
    java this
    递归
    java 文件中 定义一个字符串,它的默认编码是什么?
    合并数组
    << 移位运算
    final static T
    Base64.java 工具类
  • 原文地址:https://www.cnblogs.com/xiaoran991/p/14533692.html
Copyright © 2011-2022 走看看