Dada is a witch from a different world. When she drifted around aimlessly, she met a kind girl, Han Han, and was accepted on the earth.
There is a flying car in Hanhan's house.
One day, the circuit board of the flying car broke down suddenly, which made it unable to start.
The overall structure of the circuit board is a grid of R rows and C columns (R,C ≤ 500), as shown in the figure below.
Each grid point is the contact point of the wire, and each grid contains an electronic component.
The main part of the electronic component is a rotatable short cable connecting two contacts 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.
Dada 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 starting device are connected through several short cables.
However, the scale of the circuit is too large. Dada is not good at programming. I hope you can help her solve this problem.
Input format
The input file contains multiple sets of test data.
The first line contains an integer T, which represents 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.
Then line R, C characters in each line, one of "/" and ", indicates the direction of the standard part.
Output format
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 cannot be connected to the engine in any way, NO SOLUTION will be output.
Data range
1≤R,C≤500,
1≤T≤5
Input example:
1
3 5
\/\
\///
/\\
Output example:
1
For the shortest circuit, if the diagonal points are connected, the original line cost is 0, if not, the cost is 1.
Code:
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #include<vector> #include<queue> #include<set> #define maxn 251005 #define ll long long #define maxx 1000005 #define mod 1000000007 #define INF 0x7f7f7f7f using namespace std; int head[maxn],to[maxx],_next[maxx],_w[maxx]; int edge; void addEdge(int x,int y,int w) { to[++edge]=y,_w[edge]=w,_next[edge]=head[x],head[x]=edge; } int dis[maxn]; bool vis[maxn]; void init() { memset(head,0,sizeof(head)); edge=0; memset(dis,INF,sizeof(dis)); memset(vis,0,sizeof(vis)); } int n,m; char s[505]; struct node { int p; int val; node(int p,int val):p(p),val(val){} friend bool operator < (const node& x1,const node& x2) { return x1.val>x2.val; } }; int main() { int t;cin>>t; while(t--) { cin>>n>>m; init(); for(int i=0;i<n;i++) { scanf("%s",s); //printf("%s\n",s); for(int j=0;j<m;j++) { if(s[j]=='/') { addEdge(i*(m+1)+j,(i+1)*(m+1)+j+1,1); addEdge((i+1)*(m+1)+j+1,i*(m+1)+j,1); addEdge(i*(m+1)+j+1,(i+1)*(m+1)+j,0); addEdge((i+1)*(m+1)+j,i*(m+1)+j+1,0); } else { addEdge(i*(m+1)+j,(i+1)*(m+1)+j+1,0); addEdge((i+1)*(m+1)+j+1,i*(m+1)+j,0); addEdge(i*(m+1)+j+1,(i+1)*(m+1)+j,1); addEdge((i+1)*(m+1)+j,i*(m+1)+j+1,1); } } } priority_queue<node>que; que.push(node(0,0)); dis[0]=0; while(que.size()) { node now=que.top();que.pop(); int u=now.p; if(vis[u])continue; vis[u]=true; for(int i=head[u];i;i=_next[i]) { int v=to[i]; if(dis[v]>dis[u]+_w[i]) { dis[v]=dis[u]+_w[i]; que.push(node(v,dis[v])); } } } if(dis[n*(m+1)+m]==INF)cout<<"NO SOLUTION"<<endl; else cout<<dis[n*(m+1)+m]<<endl; } return 0; }