*Leetcode 874. Walking Robot Simulation

Posted Z-Pilgrim

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了*Leetcode 874. Walking Robot Simulation相关的知识,希望对你有一定的参考价值。

https://leetcode.com/problems/walking-robot-simulation/description/

因为X<= 9 所以一步一步枚举就行。。 

class Solution 
public:
    
    string hash(int x, int y) 
        return to_string(x) + "->" + to_string(y);
    
    
    int robotSim(vector<int>& cmds, vector<vector<int>>& obs)      
        vector<vector<int>> dirs(0,1,1,0,0,-1, -1,0);
        
        unordered_set< string > sobs;
        for (auto v : obs) sobs.insert( hash(v[0], v[1]) );
        
        // pair <int, int> pos = make_pair( 0, 0 );
        vector<int> pos = 0, 0;
        int dir = 0, ret = 0;
        for ( int i = 0; i < cmds.size(); i++ ) 
            if ( cmds[i] == -1 ) 
                dir += 1;
                dir %= 4;
                continue;
            
            
            if ( cmds[i] == -2 ) 
                dir = (dir - 1 + 4) % 4;
                continue;
            
            
            while ( cmds[i] && sobs.find( hash( pos[0] + dirs[dir][0], pos[1] + dirs[dir][1] ) ) == sobs.end() ) 
                pos[0] += dirs[dir][0], pos[1] += dirs[dir][1];
                ret = max(ret, pos[0] * pos[0] + pos[1] * pos[1]);
                // cout << hash(pos[0], pos[1]) << endl;
                cmds[i]--;
            
            
            // if (cmds[i] > 0 || sobs.find( hash( pos[0] + dirs[dir][0], pos[1] + dirs[dir][1] ) ) != sobs.end() ) return ret;
            
        
        return ret;
    
;

 

 

以上是关于*Leetcode 874. Walking Robot Simulation的主要内容,如果未能解决你的问题,请参考以下文章

874. Walking Robot Simulation

算法:模拟机器人走路874. Walking Robot Simulation

算法:874. Walking Robot Simulation模拟机器人走路

874.Walking Robot Simulation(list不可被哈希)

leetcode 874 Robot Simulation

leetcode874