+ Thinking shortest variant [USACO07OCT] Obstacle Course S (Los Valley P1649)

[USACO07OCT]Obstacle Course S

Title Description

Consider an N x N (1 <= N <= 100) square field composed of 1

by 1 tiles. Some of these tiles are impassible by cows and are marked with an ‘x’ in this 5 by 5 field that is challenging to navigate:
图略:
Bessie finds herself in one such field at location A and wants to move to location B in order to lick the salt block there. Slow, lumbering creatures like cows do not like to turn and, of course, may only move parallel to the edges of the square field. For a given field, determine the minimum number of ninety degree turns in any path from A to B. The path may begin and end with Bessie facing in any direction. Bessie knows she can get to the salt lick.

N * N (1 <= N <= 100) in the grid, 'X' denotes the lattice can not walk, '' represents the lattice can walk. Carmen fat, and therefore bad turn. Now go to point B from point A, will have to turn at least 90 degree bend a few times?

Input Format

The first line of an integer N, the following N lines of N characters, only the characters appear: '.', 'X', 'A', 'B', showing the above mentioned matrix grid, after each character has a space.

[Data] scale

2<=N<=100

Output Format

An integer: the minimum number of turns. If not reach, the output of -1.


This question can be calculated bfs all paths, and obtains the minimum number of paths of a turning all;

But you can also directly seek the shortest, I use dij algorithm;

With the average shortest path different from the shortest This question is not the same wording, a first node to each with three properties:

  1. The node point coordinates x and y
  2. The direction of the point f
  3. Come to this point from the starting point of a minimum number of steering

The difference is that most judges, and the general shortest dij in determining whether the team is not playing generally equal sign, but this must fight, because even if the same path, the direction of the point may be different, and then determine whether there traversed this point, it is different; the code annotated;

Code:

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define lson k<<1
#define rson k<<1|1
using namespace std;
const int N=10000100;
const int M=200100;
const LL mod=2e9+7;
char s[110][110];
int sx,sy,ex,ey,dis[110][110],n;
bool vis[110][110];
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
struct Node{
	int x,y,w,f;
	bool operator < (const Node& rhs) const{return w>rhs.w;}
};
void dij(){
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++) dis[i][j]=2e9;
	}
	dis[sx][sy]=0;
	priority_queue<Node>qu;
	qu.push((Node){sx,sy,0,-1});
	while(!qu.empty()){
		Node n1=qu.top();
		qu.pop();
//		if(vis[n1.x][n1.y]) continue;//这个判断条件不能有 
		vis[n1.x][n1.y]=true;
		for(int i=0;i<=3;i++){
			int x=n1.x+dx[i];
			int y=n1.y+dy[i];
			if(x<1||x>n||y<1||y>n||s[x][y]=='x'||vis[x][y]) continue;
			if(n1.f==-1){
				dis[x][y]=1;
				qu.push((Node){x,y,1,i});
			}
			else{
				if(abs(n1.f-i)==2||n1.f==i){//不转向 
					if(dis[x][y]>=n1.w) dis[x][y]=n1.w,qu.push((Node){x,y,dis[x][y],i});
					//必须有等号 
				}
				else{//转向 
					if(dis[x][y]>=n1.w+1) dis[x][y]=n1.w+1,qu.push((Node){x,y,dis[x][y],i});
					//必须有等号 	
				}
			}
		}
	}
}
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			cin>>s[i][j];
			if(s[i][j]=='A') sx=i,sy=j;
			if(s[i][j]=='B') ex=i,ey=j;
		}
	}
	dij();
	if(dis[ex][ey]==2e9) cout<<-1<<endl;
	else cout<<dis[ex][ey]-1<<endl;
	return 0;
}

Published 264 original articles · won praise 46 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_44291254/article/details/105347940