Q11661: Burger Time (漢堡時刻)

http://luckycat.kshs.kh.edu.tw/homework/q11661.htm


#include<iostream>
using namespace std;

int main(){
  int L;//資料長度
  char road, state;
  int position;
  int distance; //藥局與餐廳的距離,也就是案答

  while( cin>>L && L!=0 ){

    state = '.';
    position = -1;
    distance = 2000001;

    for( int i = 0 ; i < L ; i++ ){

      cin>>road;

      //如果有藥局跟餐廳合併的店,距離直接為0
      if( road == 'Z' ) distance = 0;

      //遇到藥局或是餐廳
      if( road != '.' ){
        //  代表R遇到D或是D遇到R
        if( state != road && state != '.' ){
          //目前的位置與前一個(R或D)的距離有比目前的最短路徑還短嗎?
          if( i - position < distance ){
            distance = i - position;//將最短距離更新
          }
        }
        state = road;//紀錄最新的藥局或是餐廳
        position = i;//紀錄位置

      }
    }
    cout<< distance <<endl;
  }
  return 0;
}

results matching ""

    No results matching ""