20分求助

P2689 东南西北

好像走了冤枉路... 试试这个数据: ``` 9 9 7 7 10 N N W W W S S S E E ``` 答案是$4$,走两个$S$,两个$W$. 您的程序输出了$7$. 顺便请修复一下Markdown(
by devans @ 2020-11-05 20:33:05


```cpp #include<cstdio> #include<cstring> #include<iostream> using namespace std; int T,x1,x2,y1,y2,ans=0; char a[55]; int main(){ cin>>x1>>x2; cin>>y1>>y2; cin>>T; for(int i=0;i<T;i++) cin>>a[i]; if(x1==y1&&x2==y2) cout<<0; for(int i=0;i<T;i++){ if(x1!=y1&&x2==y2){ if(x1>y1){ if(a[i]=='W'){ x1--; ans++; } else ans++; } if(x1<y1){ if(a[i]=='E'){ x1++; ans++; } else ans++; } if(x1==y1) break; } else if(x1==y1&&x2!=y2){ if(x2>y2){ if(a[i]=='S'){ x2--; ans++; } else ans++; } if(x2<y2){ if(a[i]=='N'){ x2++; ans++; } else ans++; } if(x2==y2) break; } else if(x1!=y1&&x2!=y2){ if(x1>y1&&x2>y2){ if(a[i]=='W'){ x1--; ans++; } else if(a[i]=='S'){ x2--; ans++; } else ans++; } else if(x1>y1&&x2<y2){ if(a[i]=='W'){ x1--; ans++; } else if(a[i]=='N'){ x2++; ans++; } else ans++; } else if(x1<y1&&x2>y2){ if(a[i]=='E'){ x1++; ans++; } else if(a[i]=='S'){ x2--; ans++; } else ans++; } else if(x1<y1&&x2<y2){ if(a[i]=='E'){ x1++; ans++; } else if(a[i]=='N'){ x2++; ans++; } else ans++; } else if(x1==y1&&x2==y2) break; } } if(x1==y1&&x2==y2) cout<<ans; else cout<<-1; return 0; } ```
by devans @ 2020-11-05 20:34:27


其实没有必要维护目前的位置 可以直接求需要的距离. 还有其实是选择多个时刻而**不是按顺序等到该移动的时候再移动**.
by devans @ 2020-11-05 20:46:59


您可以试试这个代码(我还没试过) ```cpp #include<cstdio> #include<cstring> #include<iostream> using namespace std; int T,x1,x2,y1,y2,ans=0; char a[55]; int main(){ cin>>x1>>x2; cin>>y1>>y2; cin>>T; for(int i=0;i<T;i++) cin>>a[i]; if(x1==y1&&x2==y2) cout<<0; for(int i=0;i<T;i++){ if(x1!=y1&&x2==y2){ if(x1>y1){ if(a[i]=='W'){ x1--; ans++; } //else ans++; } if(x1<y1){ if(a[i]=='E'){ x1++; ans++; } //else ans++; } if(x1==y1) break; } else if(x1==y1&&x2!=y2){ if(x2>y2){ if(a[i]=='S'){ x2--; ans++; } //else ans++; } if(x2<y2){ if(a[i]=='N'){ x2++; ans++; } //else ans++; } if(x2==y2) break; } else if(x1!=y1&&x2!=y2){ if(x1>y1&&x2>y2){ if(a[i]=='W'){ x1--; ans++; } else if(a[i]=='S'){ x2--; ans++; } //else ans++; } else if(x1>y1&&x2<y2){ if(a[i]=='W'){ x1--; ans++; } else if(a[i]=='N'){ x2++; ans++; } //else ans++; } else if(x1<y1&&x2>y2){ if(a[i]=='E'){ x1++; ans++; } else if(a[i]=='S'){ x2--; ans++; } //else ans++; } else if(x1<y1&&x2<y2){ if(a[i]=='E'){ x1++; ans++; } else if(a[i]=='N'){ x2++; ans++; } //else ans++; } else if(x1==y1&&x2==y2) break; } } if(x1==y1&&x2==y2) cout<<ans; else cout<<-1; return 0; } ```
by devans @ 2020-11-05 20:48:51


谢谢大佬
by ink!sans @ 2020-11-06 15:58:30


|