求助!!!

P2689 东南西北

还不对 ``` #include<bits/stdc++.h> using namespace std; long long sx,sy,ex,ey,n,mins=INT_MAX,cnt; bool flag[1001][1001]; 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' && flag[x][y+1]==false) cnt++; flag[x][y+1]=true; dfs(x,y+1,c+1); cnt--; flag[x][y+1]=false; if(arr[c]=='S' && flag[x+1][y]==false) cnt++; flag[x+1][y]=true; dfs(x+1,y,c+1); cnt--; flag[x+1][y]=false; if(arr[c]=='W' && flag[x][y-1]==false) cnt++; flag[x][y-1]=true; dfs(x,y-1,c+1); cnt--; flag[x][y-1]=false; if(arr[c]=='N' && flag[x-1][y]==false) cnt++; flag[x-1][y]=true; dfs(x-1,y,c+1); cnt--; flag[x-1][y]=false; dfs(x,y,c+1); } int main(){ cin>>sx>>sy>>ex>>ey>>n; for(int i=1;i<=n;i++){ cin>>arr[i]; } flag[sx][sy]=true; dfs(sx,sy,1); if(mins!=INT_MAX) cout<<mins; else cout<<-1; return 0; } ```
by liu_le_chen @ 2023-09-27 21:32:05


```cpp #include<bits/stdc++.h> using namespace std; int a[1005]; int main(){ char n; int a,b,c,d,e,ans=0; cin>>a>>b>>c>>d>>e; for(int i=1;i<=e;i++){ cin>>n; if(n=='N'&&a<c)a++,ans++; else if(n=='S'&&a>c)a--,ans++; else if(n=='E'&&b<d)b++,ans++; else if(n=='W'&&b>d)b--,ans++; } if(a==c&&b==d)cout<<ans; else cout<<"-1"; return 0; } ```
by zhengshuyue @ 2023-09-27 22:07:22


@[zhengshuyue](/user/910271) **厉害了**
by jxjddb123 @ 2024-02-26 14:11:01


|