Problem p00517 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
#include<iostream>
#include<queue>
#include<utility>
using namespace std;
int bfs();
const int INF = 100000000;
typedef pair<int, int> P;
int N, M;
int sx, sy;
int gx, gy;
int d[100][101];
int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
int main() {
        cin >> N >> M;
        int res = bfs();
        printf("%d\\n", res);
        return 0;
}
int bfs() {
        queue<P> que;
        for (int i = 0; i < N; i++) {
                for (int j = 0; j < M; j++) {
                        d[i][j] = INF;
                }
        }

}
*/
#include <iostream>
using namespace std;
long W, H, N;
long long huX = 1, huY = 1, ans = 0;
long kaX, kaY;
void kita();
void higashi();
void minami();
void nishi();
void naname();
int main() {
  cin >> W >> H >> N;
  for (int i = 0; i < N; i++) {
    cin >> kaX >> kaY;
    if (i != 0) {
      if (huX == kaX && huY < kaY) {
        kita();
      } else if (huY == kaY && huX < kaX) {
        higashi();
      } else if (huY == kaY && huX > kaX) {
        nishi();
      } else if (huX == kaX && huY > kaY) {
        minami();
      } else {
        naname();
      }
    }
    if (i == 0) {
      huX = kaX;
      huY = kaY;
    }
  }
  cout << ans << endl;
}
void kita() {
  ans += kaY - huY;
  huY = kaY;
  huX = kaX;
  return;
}
void higashi() {
  ans += kaX - huX;
  huX = kaX;
  huY = kaY;
  return;
}
void minami() {
  ans += huY - kaY;
  huY = kaY;
  huX = kaX;
  return;
}
void nishi() {
  ans += huX - kaX;
  huY = kaY;
  huX = kaX;
  return;
}
void naname() {
  int saX, saY;
  saX = kaX - huX;
  saY = kaY - huY;
  if (saX == saY) {
    if (saX > 0) {
      ans += saX;
    } else {
      ans += saX * -1;
    }
    huX = kaX;
    huY = kaY;
  } else if (saX == saY * -1 || saX * -1 == saY) {
    if (saY > 0) {
      ans += saY * 2;
    } else {
      ans += saX * 2;
    }
    huX = kaX;
    huY = kaY;
  } else if (saX < saY && saX > 0 && saY > 0) { // hokuhokutou nannansei

    ans += saY;
    huX = kaX;
    huY = kaY;
  } else if (saX > saY && saX > 0 && saY > 0) { // touhokutou
    ans += saX;
    huX = kaX;
    huY = kaY;
  } else if (saX > saY && saX > 0 && saY < 0) { // tounantou
    ans += kaX - huX + huY - kaY;
    huX = kaX;
    huY = kaY;
  } else if (saX < saY && saX < 0 && saY < 0) { // seinansei

    ans += huX - kaX;
    huX = kaX;
    huY = kaY;
  } else if (saX > saY && saX < 0 && saY < 0) {
    ans += huY - kaY;
    huX = kaX;
    huY = kaY;
  } else if (saX < saY && saX < 0 && saY > 0) { // seihokusei hokuhokusei
    ans += huX - kaX + kaY - huY;
    huX = kaX;
    huY = kaY;
  } else if (saX > saY && saX > 0 && saY > 0) { // nannnantou
    ans += kaX - huX + huY - kaY;
    huX = kaX;
    huY = kaY;
  }
  return;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int ze(int);
int main() {
  int playerX = 1, playerY = 1;
  int W, H, N, ans = 0, count = 0, a, c = 0;
  int kankouX, kankouY;
  bool bo = false;
  cin >> W >> H >> N;
  for (int j = 0; j < N; j++) {
    cin >> kankouX >> kankouY;
    if (j == 0) {
      playerX = kankouX;
      playerY = kankouY;
    }
    if (kankouX == playerX && kankouY == playerY) {

    } else if (kankouX - playerX == kankouY - playerY) {
      a = kankouX - playerX;
      ans += ze(a);
      playerX = kankouX;
      playerY = kankouY;
    } else if (kankouX != playerX && kankouY == playerY) {
      count += kankouX - playerX;
      ans += ze(count);
      playerX = kankouX;
      count = 0;
    } else if (kankouY != playerY && kankouX == playerX) {
      count += kankouY - playerY;
      ans += ze(count);
      playerY = kankouY;
      count = 0;
    } else if (kankouX - playerX > 0 && kankouY - playerY < 0) {
      while (1) {
        if (kankouY == playerY) {
          break;
        }
        playerY--;
        ans++;
      }
      while (1) {
        if (kankouX == playerX) {
          break;
        }
        playerX++;
        ans++;
      }
    } else if (kankouX - playerX > kankouY - playerY) {
      while (1) {
        if (kankouY == playerY) {
          break;
        }
        if (kankouX - playerX > 0) {
          playerX++;
          playerY++;
        } else if (kankouX - playerX < 0) {
          playerX--;
          playerY--;
        }
        ans++;
      }
      count += kankouX - playerX;
      ans += ze(count);
      playerX = kankouX;
      count = 0;
    } else if (kankouX - playerX < kankouY - playerY) {
      if (kankouX - playerX < 0 && kankouY - playerY > 0) {
        while (1) {
          if (playerX == kankouX) {
            break;
          }
          playerX--;
          ans++;
        }
        ans += kankouY - playerY;
        playerY = kankouY;
      } else if (kankouX - playerX < 0) {
        while (1) {
          if (playerY == kankouY) {
            break;
          }
          playerX--;
          playerY--;
          ans++;
        }
        ans += playerX - kankouX;
        playerX = kankouX;

      } else if (kankouX - playerX > 0) {
        while (1) {
          if (playerY == kankouY) {
            break;
          }
          playerY--;
          ans++;
        }
        ans = kankouX - playerX;
        playerX = kankouX;
      }
    }
  }
  std::cout << ans << endl;

  return 0;
}
int ze(int a) {
  if (a < 0) {
    a *= -1;
  }
  return a;
}