Loading

AcWing 4318. 最短路径

题目链接

https://www.acwing.com/problem/content/4321/

题目思路

最开始做的时候只考虑了有无环和走重复路径
但是少考虑了不能走相邻路径
所以只需要将每次走过路径的上下左右四个方向记录一下,只要有一块被重复走过即为不合法路径

题目代码

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;
int st[100][100];

int main()
{
    memset(st, false, sizeof st);
    int x = 50, y = 50;
    int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
    string a;
    cin >> a;
    for(int i = 0; i < a.size(); i ++ )
    {
        
        if(a[i] == 'R') y ++ , c1 ++ ;
        if(a[i] == 'U') x ++ , c2 ++ ;
        if(a[i] == 'D') x -- , c3 ++ ;
        if(a[i] == 'L') y -- , c4 ++ ;
        st[x][y] ++ ;
    }
    
    puts("YES");
    return 0;
}
posted @ 2022-03-26 21:05  vacilie  阅读(26)  评论(0)    收藏  举报