迷路的牛牛题解
迷路的牛牛
http://www.nowcoder.com/questionTerminal/fc72d3493d7e4be883e931d507352a4a
傻瓜式思路,输入L左转,输入R右转
- 时间复杂度
- 空间复杂度
#include using namespace std; int main(){ ios::sync_with_stdio(0); cin.tie(0); int n = 0; cin>>n; char turn[n]; for(int i=0; i<n; i++){ cin>>turn[i]; } char direction[] = {'N','E','S','W'}; int direction_i = 0; for(int i=0; i<n; i++){ if(turn[i] == 'R' && direction_i != 3) direction_i++; else if(turn[i] == 'R' && direction_i == 3) direction_i = 0; else if(turn[i] == 'L' && direction_i != 0) direction_i--; else direction_i = 3; } cout<<direction[direction_i]<<'\n'; }