T143990 路径相交检测
题目描述:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9234a76bda2ff828f3a675f722012727.png)
思路:
①:采用标记法,使用一个数组标记x和y,每走过一次新的坐标标记一下,如果走过的坐标已经被标记过了,说明这个坐标在之前就已经走过,值得注意的是,数组下标不能出现为0,所以我们要从数组大小的对称中心开始标记。
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 1e4+4 ;
//const int M = 1e8+5;
int book[N][N];
int main()
{
string str;
cin>>str;
int len=str.size();
int x=N/2,y=N/2;
book[x][y]=1;
for(int i=0;i<len;i++){
if(str[i]=='N'){
x++;
if(book[x][y]==1) {
printf("Yes");
return 0;
}
book[x][y]=1;
}
else if(str[i]=='S'){
x--;
if(book[x][y]==1) {
printf("Yes");
return 0;
}
book[x][y]=1;
}
else if(str[i]=='E'){
y++;
if(book[x][y]==1) {
printf("Yes");
return 0;
}
book[x][y]=1;
}
else if(str[i]=='W'){
y--;
if(book[x][y]==1) {
printf("Yes");
return 0;
}
book[x][y]=1;
}
}
printf("No");
return 0;
}