Repair circuit (deque shortest × √)

Dada is a witch from another world, when she was aimlessly drifting around, he met a kind-hearted girl Han Han, thereby shelter on Earth.

Han Han's family has a flying car.

One day flying car circuit board suddenly malfunctioned, leading to not start.

The overall configuration of the circuit board is a grid (R, C≤500) one of R rows and C columns, as shown in FIG.

Circuit .png

Each grid point is a contact wire, each grid contains an electronic component.

The main part of the electronic component is a rotatable, two short cables connecting contacts on a diagonal line.

After the rotation, it can be connected to the other diagonal two points.

Upper left corner of the DC power supply circuit board access point, the access point lower right corner of the flight vehicle launching apparatus.

People found that because the direction of some of the elements accidentally changed, the circuit board may be in the off state.

She prepared by calculating a minimum number of rotating elements, the power supply means connected to the engine by a plurality of short cable.

However, the scale of the circuit is too great, Dada is not good at programming, I hope you can help her solve this problem.

Input Format

Input file contains several test cases.

The first row contains an integer T, represents the number of test data.

For each test, the first line contains a positive integer R and C, the number of rows and columns of the circuit board.

After the R lines of C characters, the character is "/"and "\"one, showing the direction of standard parts.

Output Format

For each test case, the output of a single line in the positive integer representing the number of required reduced rotation.

If not no matter how such communication between the power source and the engine output NO SOLUTION.


emmmmm, the title, the book on an "algorithm Advanced" given method is double-ended queue, but as konjac how I could, so I thought SPFA algorithm.

Each of the grid as a node, the total of (n + 1) * (m + 1) nodes, in the figure, the right side will be 1 to 4, this edge is set to 0, and 2-3 this edge it is 1, 

 

 By such a construction diagram, we 1 as a starting point, while the shortest run, the output end of the distance can be, noted that when the distance to the end of infinity, that can not reach the end, output NO SOLUTION;

Comparison of the pit is actually solving the problem of data card SPFA, so use Dijkstra on the line

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e6 + 100;
const int MAXM = 3e3 + 10;

template < typename T > inline void read(T &x) {
    x = 0; T ff = 1, ch = getchar();
    while(!isdigit(ch)) {
        if(ch == '-') ff = -1;
        ch = getchar();
    }
    while(isdigit(ch)) {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    x *= ff;
} 

template < typename T > inline void write(T x) {
    if(x < 0) putchar('-'), x = -x;
    if(x > 9) write(x / 10);
    putchar(x % 10 + '0');
} 
 
int T, n, m;
int vis[MAXN], dis[MAXN];
int lin[MAXN], tot = 0;
char ch;
struct edge {
    int y, v, next;
}e[MAXN];

inline void add(int xx, int yy, int vv) {
    e[++tot].y = yy;
    e[tot].v = vv;
    e[tot].next = lin[xx];
    lin[xx] = tot;
}

/*void SPFA() {
    memset(dis, 0x3f, sizeof(dis));
    memset(vis, false, sizeof(vis));
    queue < int > q;
    q.push(1);
    dis[1] = 0;
    while(!q.empty()) {
        int x = q.front(); q.pop(); vis[x] = false;
        for(int i = lin[x], y; i; i = e[i].next) {
            if(dis[y = e[i].y] > dis[x] + e[i].v) {
                dis[y] = dis[x] + e[i].v;
                if(!vis[y]) {
                    vis[y] = true;
                    q.push(y);
                }
            }
            
        }
    }
    
}*/

void Dijkstra() {
    memset(dis, 0x3f, sizeof(dis));
    memset(vis, false, sizeof(vis));
    priority_queue < pair < int, int > > q;
    dis[1] = 0;
    q.push(make_pair(0, 1));
    while(!q.empty()) {
        int x = q.top().second; q.pop();
        if(vis[x]) continue;
        vis[x] = true;
        for(int i = lin[x], y; i; i = e[i].next) {
            if(dis[y = e[i].y] > dis[x] + e[i].v) {
                dis[y] = dis[x] + e[i].v;
                if(!vis[y]) q.push(make_pair(-dis[y], y));
            }
        }
    }
}

int main() {
    read(T);
    while(T--) {
        read(n); read(m);
        memset(lin, 0, sizeof(lin));
        memset(e, 0, sizeof(e)); 
        tot = 0;
        for(int i = 1; i <= n; ++i) {
            for(int j = 1; j <= m; ++j) {
                ch = getchar();
                if(ch == '/') {
                    add(((i - 1) * (m + 1) + j), i * (m + 1) + j + 1, 1);
                    add(i * (m + 1) + j + 1, (i - 1) * (m + 1) + j, 1);
                    add(i * (m + 1) + j, (i - 1) * (m + 1) + j + 1, 0); 
                    add((i - 1) * (m + 1) + j + 1, i * (m + 1) + j, 0); 
                } 
                else {
                    add(((i - 1) * (m + 1) + j), i * (m + 1) + j + 1, 0);
                    add(i * (m + 1) + j + 1, (i - 1) * (m + 1) + j, 0);
                    add(i * (m + 1) + j, (i - 1) * (m + 1) + j + 1, 1); 
                    add((i - 1) * (m + 1) + j + 1, i * (m + 1) + j, 1); 
                }
            }
            ch = getchar();
        }
//        SPFA();    
        Dijkstra();
        if(dis[(n + 1) * (m + 1)] == INF) puts("NO SOLUTION");
        else write(dis[(n + 1) * (m + 1)]), puts("");
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/AK-ls/p/11407702.html