土下座しながら探索中

主に競技プログラミング

UVa 11507 : Bender B. Rodríguez Problem

UVa演習 2014/6/8 (日) 問1

問題リンク:http://uva.onlinejudge.org/external/115/11507.html

問題概要:
長さLのワイヤーがあり、(i,0,0)の点( i in L-1 to 1 )で入力で与えられた方向に曲げる
最終的にワイヤーの先端部分はどの方向を向いているか

解法:
先端の線分がどこを向いているかさえ分かれば真ん中のところはいらないので、
先端の線分が向いている方向と曲げる方向で条件分岐させる

コード:

#include<bits/stdc++.h>

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)

using namespace std;

int main(){
  int L;
  while( cin >> L, L ){
    string opr, state = "+x";
    rep(i,L-1){
      cin >> opr;
      if( opr == "No" ) continue;
      if( state == "+x" ) {
        state = opr;
      } else if( state == "-x" ) {
        state = opr;
        state[0] = ((state[0]=='+')?'-':'+');
      } else if( state == "+y" ) {
        if( opr == "+y" ) state = "-x";
        else if( opr == "-y" ) state = "+x";
      } else if( state == "-y" ) {
        if( opr == "+y" ) state = "+x";
        else if( opr == "-y" ) state = "-x";
      } else if( state == "+z" ) {
        if( opr == "+z" ) state = "-x";
        else if( opr == "-z" ) state = "+x";
      } else if( state == "-z" ) {
        if( opr == "+z" ) state = "+x";
        else if( opr == "-z" ) state = "-x";
      }
    }
    cout << state << endl;
  }
  return 0;
}