求助!代码疑似有编译隔阂
查看原帖
求助!代码疑似有编译隔阂
1127157
LanQiao1234楼主2024/5/18 17:02
#include <iostream>
#include <array>
#include <unordered_set>
std::array<std::array<char, 12>, 12> map{{
    {'*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'},
    {'*', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '*'},
    {'*', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '*'},
    {'*', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '*'},
    {'*', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '*'},
    {'*', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '*'},
    {'*', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '*'},
    {'*', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '*'},
    {'*', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '*'},
    {'*', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '*'},
    {'*', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '*'},
    {'*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'}
}};

struct Point{
    enum class Direction{
        UP,
        RIGHT,
        DOWN,
        LEFT,
    } direction=Direction::UP;
    short x, y;
    struct pos{
        short x, y;
        bool operator==(const pos& other) const{
            return this->x==other.x && this->y==other.y;
        }
        bool operator!=(const pos& other) const{
            return !(*this==other);
        }
    };
    pos get_pos() const{
        return pos{x, y};
    }
    Point():x(1), y(1){}
    Point(short x, short y):x(x), y(y){}
    void move(){
        std::pair<short, short> offset=(std::pair<short, short>[4])
            {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}[static_cast<int>(direction)];

        std::pair<short, short> located_offset{x+offset.first, y+offset.second};
        if(map[located_offset.second][located_offset.first]!='*')
            x+=offset.first, y+=offset.second;
        else
            turn_right();
    }
    void turn_right(){
        (reinterpret_cast<int&>(direction)+=1)%=4;
    }
    bool operator==(const Point& other) const{
        return this->x==other.x && this->y==other.y && this->direction==other.direction;
    }
};

template <>
struct std::hash<Point>{
    size_t operator()(const Point& p) const{
        return p.x|size_t(p.y)<<8|size_t(static_cast<int>(p.direction))<<16;
    }
};

template <typename _T1, typename _T2>
struct std::hash<std::pair<_T1, _T2>>{
    size_t operator()(const std::pair<_T1, _T2>& p) const{
        return std::hash<_T1>()(p.first)^(std::hash<_T2>()(p.second)<<32);
    }
};

int main(){
    Point farmer,cows;
    std::unordered_set<std::pair<Point, Point>> path_set;
    for(short i=1; i<=10; ++i)
        for(short j=1; j<=10; ++j)
            switch(std::cin.get()){
                [[likely]] case '*':
                    map[i][j]='*';
                [[likely]] case '.':
                    break;
                [[unlikely]] case 'F':
                    farmer=Point(j, i);
                    break;
                [[unlikely]] case 'C':
                    cows=Point(j, i);
                    break;
                default:
                    if(j==1){
                        j=10;
                        --i;
                    }else
                        --j;
                    break;
            }
    while(farmer.get_pos()!=cows.get_pos()){
        path_set.insert(std::make_pair(farmer, cows));
        farmer.move();
        cows.move();
        if(path_set.count(std::make_pair(farmer, cows))){
            std::cout<<0<<std::endl;//Cannot reach cows in the given number of days
            return 0;
        }
    }
    std::cout<<path_set.size();
    return 0;
}
2024/5/18 17:02
加载中...