[leetcode]335. Self Crossing

https://leetcode.com/problems/self-crossing/

class Solution {
public:
	bool graph[1001][1001];
    bool isSelfCrossing(vector<int>& x) {
		int a = 500, b = 500;
		for(int i = 0; i < 1001; i++){
			for(int j = 0; j < 1001; j++)
				graph[i][j] = false;
		}
		graph[a][b] = true;
		for(int i = 0; i < x.size(); i++){
			switch(i%4){
			case 0:
				for(int j = 0; j < x[i]; j++){
					b++;
					if(graph[a][b]) return true;
					graph[a][b] = true;			
				}
				break;
			case 1:
				for(int j = 0; j < x[i]; j++){
					a--;
					if(graph[a][b]) return true;
					graph[a][b] = true;
				}
				break;
			case 2:
				for(int j = 0; j < x[i]; j++){
					b--;
					if(graph[a][b]) return true;
					graph[a][b] = true;
				}
				break;
			case 3:
				for(int j = 0; j < x[i]; j++){
					a++;
					if(graph[a][b]) return true;
					graph[a][b] = true;
				}
				break;
			}
		}
		return false;
    }
};


你可能感兴趣的:(LeetCode,C++,OJ)