[Blue Bridge Cup] [2015 Sixth Zhenti] through the minefield

[Blue Bridge Cup] [2015 Sixth Zhenti] through the minefield

Time limit: 1Sec Memory Limit: 128MB submit: 5 Resolution: 3

Title Description

Planet X tanks chariot is very strange, it must cross the positive energy and negative energy radiation radiation zone area in order to maintain the normal operation alternately, otherwise it will be scrapped.
A tank needs to go from A to B zone area (A, B area itself is a safe area, no positive energy or negative energy characteristics), how to take the shortest path?

Is known to map a square, above the letters marked A, B region, the other regions are marked positive or negative sign respectively represent positive and negative energy radiation zone.
For example:
A + - + -
- + - - +
- + + + -
+ - + - +
B + - + -

tank can only be moved to an adjacent region of the horizontal or vertical direction.

 

Entry

The first input line is an integer n, denotes the size of the square, 4 <= n <100
followed by n rows, each row having n data, may be A, B, +, - one in the middle with separated by a space.
A, B only once.

Export

A required output integer represents the minimum number of steps moved between A and B from the tank area.
If no program, outputs -1

 

Sample input

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

Sample Output

10
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
char maze[109][109];
int  vis[109][109];
int  n,ans;
const int INF=0x3f3f3f;
int   dir[4][2] = { {0,1},{0,-1},{-1,0},{1,0} };
void  dfs( int x ,int y ,int l,int cnt ){
	  if( maze[x][y] == 'B' || cnt > ans ){
	  	  ans = min(ans,cnt);
	  	  return ;
	  }
	  for( int i=0;i<=3;i++ ){
	  	   int xx= x+dir[i][0];
	  	   int yy= y+dir[i][1];
	  	   if( xx>=0 && xx <n && yy >=0 && yy <n && !vis[xx][yy] ){
	  	   	   if( maze[xx][yy] == 'B' ){
	  	   	   	   dfs(xx,yy,3,cnt+1);
	  	   	   	   continue;
	  	   	   }
	  	   	   if( l==-1 ){
	  	       	   if( maze[xx][yy] == '-' ){
	  	       	   	   vis[xx][yy] = 1;
	  	       	   	   dfs(xx,yy,0,cnt+1);
					   vis[xx][yy] = 0;    
				   }
				   else if( maze[xx][yy] == '+' ){
				   	   vis[xx][yy] = 1;
	  	       	   	   dfs(xx,yy,1,cnt+1);
					   vis[xx][yy] = 0;
				   }
	  	       }
			   else  if( l==0 ){
			   	     if( maze[xx][yy] == '+' ){
				   	   vis[xx][yy] = 1;
	  	       	   	   dfs(xx,yy,1,cnt+1);
					   vis[xx][yy] = 0;
				   }
			   }
			   else if( l== 1 ){
			   	if( maze[xx][yy] == '-'  ){
	  	       	   	   vis[xx][yy] = 1;
	  	       	   	   dfs(xx,yy,0,cnt+1);
					   vis[xx][yy] = 0;    
				   }
		     }
		  }
	  } 
}
int main(void){
 	while( scanf("%d",&n)!=EOF){
		   int x,y;
		   for( int i=0;i<n;i++ ){
		   	    for( int j=0;j<n;j++){
		   	     	 scanf(" %c",&maze[i][j]);
		             if( maze[i][j] =='A'){
		             	 x = i;
		             	 y = j;
					 }	 
				}   
		   }
		   ans = INF;
		   memset(vis,0,sizeof(vis));
		   vis[x][y] = 1;
		   dfs(x,y,-1,0);
		   if( ans !=INF )
		        printf("%d\n",ans);
	       else printf("-1\n");
	} 
	return 0;
}

 

Guess you like

Origin blog.csdn.net/S_999999/article/details/94599391
Recommended