求助!!!
查看原帖
求助!!!
1037273
封禁用户楼主2023/9/27 20:50
#include<bits/stdc++.h>
using namespace std;
long long sx,sy,ex,ey,n,mins=INT_MAX,cnt;
char arr[1001];
void dfs(int x,int y,int c){
	if(x==ex && y==ey){
		mins=min(cnt,mins);
		return;
	}
	if(arr[c]=='E') cnt++; dfs(x,y+1,c+1); cnt--;
	if(arr[c]=='S') cnt++; dfs(x+1,y,c+1); cnt--;
	if(arr[c]=='W') cnt++; dfs(x,y-1,c+1); cnt--;
	if(arr[c]=='N') cnt++; dfs(x-1,y,c+1); cnt--;
	dfs(x,y,c+1);
}
int main(){
	cin>>sx>>sy>>ex>>ey>>n;
	for(int i=1;i<=n;i++){
		cin>>arr[i];
	} 
	dfs(sx,sy,1);
	if(mins!=INT_MAX) cout<<mins;
	else cout<<-1;
	return 0;
}

2023/9/27 20:50
加载中...