1. Portal

Question three: Portal

 

【Problem Description】

N * n is represented by a matrix of an island, the island has land and waters, land with 0, 1 represented by the waters, you can walk on land, walking on land not need to spend any cost, given the starting point coordinates (sx, sy) and end coordinates (ex, ey), two points must be to ensure that land. 
You can build a portal and can only build a portal on any two land, the portal enables two points of each other, the cost is between two horizontal axis, vertical axis of the square of the difference and that (sx -ex) ^ 2 + (sy-  ey) ^ 2.
The minimum cost required to play from the beginning of the end, if the need to build the portal can directly reach the end of the cost is zero.

 

[Input Format]

In order to effectively prevent a plurality of sets of test data points lie. 
The first line enter a positive integer t (2≤t≤10), represents a group of test data. 
For the first set of test data as input: 
a first input line of a positive integer n (1≤n≤100). 
Enter the second line starting coordinates sx, sy. 
The third input line end point coordinates ex, ey. 
The next n input lines 01 of a matrix of n * n, no space between 0 and 1, see examples.

 

[Output format]

For each line the test data output, i.e., a minimum cost of the set of integer data.

 

[Sample input]



1 1 
5 5 
00001 
11111 
00111 
00110 
00110 

1 3 
3 1 
010 
101 
010 

1 1 
2 2 
00 
10

 

[Sample output]

10 

0

 

[Data] scale

To 100% of the data, 2≤t≤10,1≤n≤100.

 

【Analysis of Algorithms】

This question maps, positional relationship between the start and end points, nothing more than two situations, 
1. beginning and end of the run through the entire map of the waters to separate (ie river), then they are in two different "continent" on , as

2. The start and end on the same piece of the continent, as


So we can start and end point are again starting to do a search, the start and end points can reach the mark are marked, then will appear both cases, 
1. to end marking time, hit can be the starting point to the point (that is, the point has been marked played), this case illustrates the start and end in the same piece on the "mainland", "0" to 
2 has not played to the end of the marking process able to point to the beginning of this note start and end points on two different "continent", then we need to enumerate open transfer between each point and the starting point to be able to go to the end of each point gate price, whichever is the minimum you can.

 

[AC] Code

 
#include<bits/stdc++.h>
#define For(i,j,n) for(int (i)=(j);(i)<=(n);(i)++)
using namespace std;
int n,sx,sy,ex,ey;
char a[110][110];
int flag[110][110],river[110][110];
bool tt=false;
void work1(int x,int y) {
    flag[x][y]=1;
    if((a[x+1][y]=='1'||flag[x+1][y]==1)&&(a[x][y+1]=='1'||flag[x][y+1]==1)&&(a[x-1][y]=='1'||flag[x-1][y]==1)&&(a[x][y-1]=='1'||flag[x][y-1]==1)) return;
    if(a[x+1][y]=='0'&&!flag[x+1][y]) {
        flag[x+1][y]=1;
        work1(x+1,y);
    }
    if(a[x][y+1]=='0'&&!flag[x][y+1]) {
        flag[x][y+1]=1;
        work1(x,y+1);
    }
    if(a[x-1][y]=='0'&&!flag[x-1][y]) {
        flag[x-1][y]=1;
        work1(x-1,y);
    }
    if(a[x][y-1]=='0'&&!flag[x][y-1]) {
        flag[x][y-1]=1;
        work1(x,y-1);
    }
}
void work2(int x,int y) {
    if(flag[x][y]==1) {
        cout<<0<<endl;
        tt=true;
        return ;
    }
    flag[x][y]=2;
    if((a[x+1][y]=='1'||flag[x+1][y]==2)&&(a[x][y+1]=='1'||flag[x][y+1]==2)&&(a[x-1][y]=='1'||flag[x-1][y]==2)&&(a[x][y-1]=='1'||flag[x][y-1]==2)) return;
    if(a[x+1][y]=='0'&&flag[x+1][y]!=2) {
        flag[x+1][y]=2;
        work2(x+1,y);
    }
    if(a[x][y+1]=='0'&&flag[x][y+1]!=2) {
        flag[x][y+1]=2;
        work2(x,y+1);
    }
    if(a[x-1][y]=='0'&&flag[x-1][y]!=2) {
        flag[x-1][y]=2;
        work2(x-1,y);
    }
    if(a[x][y-1]=='0'&&flag[x][y-1]!=2) {
        flag[x][y-1]=2;
        work2(x,y-1);
    }
}
int ans;
int main() {
    ios::sync_with_stdio(false);
    int q;
    cin>>q;
    while(q--) {
        tt=false;
        cin>>n;
        cin>>sx>>sy>>ex>>ey;
        For(i,1,n) {
            for(int j=1; j<=n; j++) {
                cin>>a[i][j];
                flag[i][j]=0;
            }
        }
        For(i,0,n+1) {
            a[i][0]=a[i][n+1]=a[0][i]=a[n+1][i]=1;
            flag[i][0]=flag[i][n+1]=flag[0][i]=flag[n+1][i]=0;
        }
        For(i,0,n+1) For(j,0,n+1) flag[i][j]=0;
        work1(sx,sy);
        work2(ex,ey);
        if(tt) continue;
        ans=0x7ffffff;
        For(i,1,n) For(j,1,n) For(i1,1,n) For(j1,1,n) if(flag[i][j]==1&&flag[i1][j1]==2)ans=min(ans,(i-i1)*(i-i1)+(j-j1)*(j-j1));
        cout<<ans<<endl;
    }
}

  

Guess you like

Origin www.cnblogs.com/Ghouls/p/11320396.html