Problem A: circuit maintenance
Time limit: 1 Sec memory limit: 128 MB
Submitted by: 16 solved by: 7
[Submission][state][Discussion Edition [proposition people: add_shengjunjie][Edit] [TestData]
Title Link: http://acm.ocrosoft.com/problem.php?cid=1694&pid=0
Title Description
Ha'nyu is a witch from a different world. When she drifted around aimlessly, she met a kind-hearted girl Rika, and was accepted on the earth. Rika has a flying car at home. One day, the circuit board of the flying car suddenly failed, resulting in failure to start normally.
The overall structure of the circuit board is a grid of R rows and C columns, as shown in the figure. Each grid point is the contact point of the wire. Each cell contains an electronic component. The main part of the electronic component is a rotatable short cable connecting two connection points on a diagonal. After rotation, it can connect two contacts of another diagonal. The contact at the upper left corner of the circuit board is connected to the DC power supply, and the contact at the lower right corner is connected to the engine of the flying car.
Ha'nyu found that the circuit board may be in an open circuit state due to the inadvertent change of the direction of some components. She is going to rotate the minimum number of components by calculation, so that the power supply and the engine device are connected through several short cables. But the scale of the circuit is too large. Ha'nyu is not good at programming. I hope you can help her solve this problem.
input
The input file contains multiple sets of test data. The first row contains an integer T for the number of test data.
for each set of test data, the first row contains positive integers R and C, representing the number of rows and columns of the board.
R line after , C characters in each line. The character is one of "/" and "\", indicating the direction of the standard part.
for 100% data, R,C ≤ 500, T ≤ 5.
output
For each group of test data, output a positive integer on a separate line to indicate the number of reduction rotations required.
if the power supply and engine cannot be connected in any way, output NO SOLUTION.
sample input
1 3 5 \\/\\ \\/// /\\\\
sample output
1
Idea: it's not easy to build a map with two-dimensional array, so use adjacency table to build a map, use one-dimensional to store two-dimensional map, use the non superposition of weight 0, and use two terminal queue to go to BFS.
Code:
#include<bits/stdc++.h> using namespace std; #define maxn 1000 #define INF 0x3f3f3f3f int n, m; char tt[maxn][maxn];//Map keeping int dis[maxn*maxn]; int ans = INF; struct edge { int e, w;//Destination and journey edge(int _e, int _w)//Constructor Initializers { e = _e; w = _w; } }; vector<edge>Map[maxn*maxn];//Building adjacency list void add_edge(int x, int y, int z) { Map[x].push_back(edge(y, z)); } int bfs(int s, int t) { for (int i = 0; i < maxn*maxn; i++)dis[i] = INF;//Double ended queue BFS deque<int>p; p.push_back(s); dis[s] = 0; while (!p.empty()) { int Now = p.back(); p.pop_back(); if (Now == t) return dis[Now]; for (int i = Map[Now].size() - 1; i >= 0; i--) { int u = Map[Now][i].e, c = Map[Now][i].w; if (dis[u] > dis[Now] + c) { dis[u] = dis[Now] + c; if (c == 0) p.push_back(u);//If the edge weight is 0, put it behind else p.push_front(u);//If the edge weight is 1, put it in front } } } return dis[t]; } int main() { int T; cin >> T; while (T--) { ans = INF; for (int i = 0; i <= maxn * maxn; i++)Map[i].clear();//Adjacency table clearing cin >> n >> m; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> tt[i][j]; } } //zuo for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { add_edge((i - 1)*(m + 1) + j, i*(m + 1) + j - 1, tt[i][j] != '/');//Transforming a two-dimensional graph into a one-dimensional graph add_edge(i*(m + 1) + j - 1, (i - 1)*(m + 1) + j, tt[i][j] != '/'); add_edge((i - 1)*(m + 1) + j - 1, i*(m + 1) + j, tt[i][j] == '/'); add_edge(i*(m + 1) + j, (i - 1)*(m + 1) + j - 1, tt[i][j] == '/'); } } ans = bfs(0, n*(m + 1) + m); if (ans == INF)cout << "NO SOLUTION" << endl; else cout << ans << endl; } }