#UVa:12503-Robot Instructions

趙題目模擬指令即可得解。

C++(0.000)

/*******************************************************/
/* UVa 12503 Robot Instructions                        */
/* Author: Maplewing [at] knightzone.studio            */
/* Version: 2018/10/03                                 */
/*******************************************************/
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;

int main(){
  int T;
  while(scanf("%d", &T) != EOF){
    for(int caseNumber = 1 ; caseNumber <= T ; ++caseNumber){
      int n;
      scanf("%d", &n);

      vector<int> instructions(n, 0);
      int position = 0;
      for(int i = 0 ; i < n ; ++i){
        string instruction;
        cin >> instruction;
        if(instruction == "LEFT"){
          instructions[i] = -1;
          --position;
        }
        else if(instruction == "RIGHT"){
          instructions[i] = 1;
          ++position;
        }
        else{
          cin >> instruction; // don't care string "AS"
          int index;
          scanf("%d", &index); 
          position += instructions[i] = instructions[index-1];
        }
      }
      printf("%d\n", position);
    }
  }
  return 0;
}
廣告

1 Comment

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料

%d 位部落客按了讚: