Blue Bridge - Crossing the Minefield

X-star's tank chariot is very strange. It must alternately pass through the positive energy radiation area and the negative energy radiation area to maintain normal operation, otherwise it will be scrapped. 
A tank needs to go from area A to area B (areas A and B are safe areas, with no positive or negative energy characteristics), how to get the shortest path?


The known map is a square matrix, on which A and B areas are marked with letters, and other areas are marked with positive or negative signs to indicate positive and negative energy radiation areas, respectively. 
For example: 
A + - + - 
- + - - + 
- + + + - 
+ - + - + 
B + - + -


Tanks can only move horizontally or vertically to adjacent areas.


Data format requirements:


the first line of input is an integer n, which represents the size of the square matrix, 4<=n<100 
is followed by n lines, each line has n data, which may be one of A, B, +, - One, separated by spaces. 
Both A and B appear only once.


It is required to output an integer representing the minimum number of steps the tank can move from area A to area B. 
If there is no scheme, output -1


For example: 

User input: 


A + - + - 
- + - - + 
- + + + - 
+ - + - + 
B + - + -

Then the program should output: 
10

#include<iostream>
#include<algorithm>
using namespace std;
char mp [105] [105];
int force[105][105]
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int mn=10000,n;
int l[50][2];
int judge=0;
void dfs(int x,int y,int way,char last)
{
	if(mp[x][y]=='B' || way>mn)
	{
		judge=1;
		mn=min(way,mn);
		return;
	}
	for(int i=0;i<4;i++)
	{
		int xx=x+dx[i];
		int yy=y+dy[i];
		if(xx<0 || xx>=n || yy<0 || yy>=n || mp[xx][yy]==last)
		{
			continue;
		}
		if(vis[xx][yy]==0)
		{
			vis[xx][yy]=1;
			dfs(xx,yy,way+1,mp[xx][yy]);
			vis[xx][yy]=0;
			
		}
	}
}
intmain()
{
	int sx, sy;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			cin>>mp[i][j];
			if(mp[i][j]=='A')
			{
				sx = i, sy = j;
			}
		}
	}
	vis [sx] [sy] = 1;
	dfs (sx, sy, 0, 'A');
	if(judge==1)
		cout<<mn<<endl;
	else
		cout<<judge<<endl;
	return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325625748&siteId=291194637