문제

I was looking through the solutions of a problem on Topcoder, and came across this one:

http://community.topcoder.com/stat?c=problem_solution&rm=249419&rd=9996&pm=6621&cr=309453

At present I am not interested to know how the algorithm works, but what's the usage of "< ?=" operator in the code? I tried compiling the code on my machine, and also on ideone.com, but to my surprise, the code fails to compile. What does the operator do, and why doesn't it work on standard compilers like ideone, and the code passes system tests on Topcoder?

The code is here:

using namespace std;

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>

#define PB push_back
#define SZ size()
#define REP(v, hi) for (int v=0; v<(hi); v++)
#define REPD(v, hi) for (int v=((hi)-1); v>=0; v--)
#define FOR(v, lo, hi) for (int v=(lo); v<(hi); v++)
#define FORD(v, lo, hi) for (int v=((hi)-1); v>=(lo); v--)

typedef vector <int> VI;
typedef vector <VI> VVI;
typedef vector <VVI> VVVI;

/* ############################ THE REAL CODE ############################ */

class RoboRace {
  public:
  int startTime(vector <string> m, vector <string> _c) {
    string c;
    REP(i,_c.SZ) c+=_c[i];
    int N=c.SZ;
    int Y=m.SZ, X=m[0].SZ;
    VVVI best(Y, VVI(X, VI(N+1, 99999)));
    int ey=-1,ex=-1;
    int yy=-1,yx=-1;
    int fy=-1,fx=-1;
    REP(y,Y) REP(x,X) {
      if (m[y][x]=='Y') { yy=y; yx=x; m[y][x]='.'; }
      if (m[y][x]=='F') { fy=y; fx=x; m[y][x]='.'; }
      if (m[y][x]=='X') { ey=y; ex=x; m[y][x]='.'; }
    }
    REP(n,N+1) best[ey][ex][n]=n;
    REPD(n,N) REP(y,Y) REP(x,X) {
      if (m[y][x]=='#') continue;
      best[y][x][n] <?= best[y][x][n+1];
      if (c[n]=='N' && y>0)   best[y][x][n] <?= best[y-1][x][n+1];
      if (c[n]=='S' && y<Y-1) best[y][x][n] <?= best[y+1][x][n+1];
      if (c[n]=='W' && x>0)   best[y][x][n] <?= best[y][x-1][n+1];
      if (c[n]=='E' && x<X-1) best[y][x][n] <?= best[y][x+1][n+1];
    }

    REP(n,N) if (best[yy][yx][n] < best[fy][fx][n]) return n;
    return -1;
  }
};
도움이 되었습니까?

해결책

That's a gcc extension, compound minimum operator, a binary operator that assign the minimum of its operands to the left-hand operand. It's mentioned in the deprecated features list of gcc manual.

A <?= B means assign the minimum of A and B to A.

There are (were) also

<?     minimum operator
>?     maximum operator
>?=    compound form of maximum operator

Sane code nowadays would use std::min instead.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top