Go: package main

import (
    "bufio"
    "fmt"
    "strconv"
    "os"
    "strings"
    "math"
)

var rdr = bufio.NewReaderSize(os.Stdin, 1000000)

func readLine() string {
    buf := make([]byte, 0, 1000000)
    for {
        l, p, e := rdr.ReadLine()
        if e != nil {
            panic(e)
        }
        buf = append(buf, l...)
        if !p {
            break
        }
    }
    return string(buf)
}

func toInt(str string) int {
    n, _ := strconv.Atoi(str)
    return n
}

func abs(a int) int {
    return int(math.Abs(float64(a)))
}

func main() {
    wh := strings.Split(readLine(), " ")
    w := toInt(wh[0])
    h := toInt(wh[1])
    readLine()
    pString := strings.Split(readLine(), " ")
    var x1 [300]int
    var y1 [300]int
    var x2 [300]int
    var y2 [300]int
    cnt := 0
    for i := 0; i <= w - 1; i++ {
        x1[cnt] = i
        y1[cnt] = 0
        cnt ++
    }
    for i := 1; i <= h - 1; i++ {
        x1[cnt] = w - 1
        y1[cnt] = i
        cnt ++
    }

    for i := w - 2;i >= 0; i-- {
        x1[cnt] = i
        y1[cnt] = h - 1
        cnt ++
    }
    cnt = 0
    for i := 0; i <= w - 2; i++ {
        x2[cnt] = i
        y2[cnt] = 1
        cnt ++
    }
    for i := 2; i <= h - 2; i++ {
        x2[cnt] = w - 2
        y2[cnt] = i
        cnt ++
    }

    for i := w - 3;i >= 0; i-- {
        x2[cnt] = i
        y2[cnt] = h - 2
        cnt ++
    }

    pos1 := 0
    pos2 := 0
    ans := 0
    for _, value := range pString {
        p := toInt(value)
        if p == 0 {
            pos1 ++
        } else {
            pos2 ++
        }
        px1, py1 := x1[pos1], y1[pos1]
        px2, py2 := x2[pos2], y2[pos2]
        if abs(px1 - px2) + abs(py1 - py2) == 1 {
            ans ++
        }
    }
    fmt.Println(ans)
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define LL long long
#define Fi first
#define Se second
using namespace std;
static const LL INF = 1LL<<61LL;
typedef pair<int,int> PII;

PII U;
PII R;
int W,H;
int N;
int ans;
int main(){
    U.Fi=1;R.Fi=1;
    U.Se=1;R.Se=2;
    cin>>W>>H;
    cin>>N;
    rep(i,N){
        int a;
        cin>>a;
        if(a==0){
            if(U.Fi<W&&U.Se<H)U.Fi+=1;
            else if(U.Fi==W&&U.Se<H)U.Se+=1;
            else if(U.Se==H)U.Fi-=1;
        }
        else if(a==1){
            if(R.Fi<W-1&&U.Se<H-1)R.Fi+=1;
            else if(R.Fi==W-1&&R.Se<H-1)R.Se+=1;
            else if(R.Se==H-1)R.Fi-=1;
        }
        if((U.Fi==R.Fi&&U.Se+1==R.Se)||(U.Se==R.Se&&U.Fi==R.Fi+1))ans++;
        else if((R.Fi==U.Fi&&R.Se+1==U.Se)||(R.Se==U.Se&&U.Fi==R.Fi+1))ans++;
    }
    cout<<ans<<endl;
}

C++: #include<bits/stdc++.h>
using namespace std;

int n,ans,W,H,in,pp1,pp2;

int main(){

    cin >> W >> H;
    cin  >> n;

    for(int i=0;i<n;i++){
        cin >> in;
        if(in==0)pp1++;
        else pp2++;
        if(pp1<=W-1&&pp2<=W-1){
            if(pp1==pp2)ans++;
        }else if(pp1>=W+1&&pp1<=W+H-2&&pp2>=W-1&&pp2<=W+H-5){
        if(pp1==pp2)ans++;
        }else if(pp2>=W+H-5&&pp1>=W+H){
        if(pp1==pp2)ans++;
        }
    }


    cout << ans << endl;

return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int main(){
	int w,h;cin >> w >> h;
	
	int i0=0,i1=0;
	
	int ans = 0;
	
	int n;cin >> n;
	for(int i=0;i<n;i++){
		int p;
		cin >> p;
		if(p){
			i1++;
		} else {
			i0++;
		}
		if(i0 <= w-2 && i1 <= w-2 && i0 == i1){
			ans++;
		}
		if(i0 >= w-2 && i1 >= w && i0 <= w+h-5 && i1 <= w+h-3 && i0-2 == i1){
			ans++;
		}
		if(i0 >= w+h-5 && i1 >= w+h-1 && i0-4 == i1){
			ans++;
		}
	}
	cout << ans << endl;
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){
  int w,h,n;
  cin >> w >> h >> n;
  int out=0,in=0,sum=0;
  while(--n){
    int tmp;
    if(cin >> tmp,tmp){++in;}else{++out;}
    if(out<=w-2 && out==in)++sum;
    else if(out>=w && out<= w+h-3 && out-in==2)++sum;
    else if(out >= w+h-1 && out-in==4)++sum;
  }
  cout << sum << endl;
  return 0;
}

C++: #include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(),(c).end())

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

using namespace std;
int main(void)
{
  int w,h,n;
  cin >> w >> h >> n;
  int udk=0,rin=0;
  int answer=0;
  REP(i,n){
    int tmp;
    cin >> tmp;
    if(tmp) ++udk;
    else ++rin;
    if(udk==w-1||udk==w+h-2) continue;
    if(udk<w-1 && udk==rin ||
       w-1<udk && udk < w+h-2 && udk-2 == rin ||
       w+h-2 <udk && udk-4 == rin) ++answer;
  }
  cout << answer << endl;
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){
  int w,h,n;
  while(cin >> w >> h >> n){
    int out=0,in=0,sum=0;
    int tmp;
    for(int i=0; i<n; ++i){
      //cout << "frg" << endl; 
      if(out<w-2){
        int tmp;
        if(cin >> tmp,tmp){++out;}else{++in;}
        if(!(out-in))++sum;
      }else if(out<h+w-2){
        int tmp;
        if(cin >> tmp,tmp){++out;}else{++in;}
        if(!(out-in-2))++sum;
      }else if(out<2*w+h-2){
        int tmp;
        if(cin >> tmp,tmp){++out;}else{++in;}
        if(!(out-in-4))++sum;
      }
      //cout << "frg2" << endl; 
    }
    cout << sum << endl;
  }
  return 0;
}

C++: http://jud#include "bits/stdc++.h"
using namespace std;

int H, W;
int a, b;
int main() {
	cin >> W >> H;
	int N; cin >> N;
	a = 0; b = 0;
	int ans = 0;
	while (N--) {
		int p; cin >> p;
		if (!p) {
			a++;
		}
		else {
			b++;
		}
		if (a == W - 1 || a == W + H - 2) {

		}
		else if (a < W-1) {
			if (a == b)ans++;
		}
		else if (a < W + H-2) {
			if (a == b + 2)ans++;
		}
		else {
			if (a == b + 4)ans++;
		}

	}
	cout << ans << endl;
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <algorithm>

using namespace std;

int main (int argc, char *argv[]) {
    int w, h, n;
    int ans = 0;
    cin >> w >> h;
    cin >> n;
    int p[n];
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
    }

    pair<int, int> uzu = make_pair(1, h);   // out
    pair<int, int> rin = make_pair(1, h-1); // in
    for (int i = 0; i < n; ++i) {
        if (p[i] == 0) {
            if (uzu.first < w && uzu.second == h) uzu.first++;
            else if (uzu.first == w && uzu.second > 0) uzu.second--;
            else uzu.first--;
        }
        else {
            if (rin.first < w - 1 && rin.second == h - 1) rin.first++;
            else if (rin.first == w - 1 && rin.second > 1) rin.second--;
            else rin.first--;
        }
        if ((uzu.first == rin.first) || (uzu.second == rin.second)) ans++;
    }
    cout << ans << endl;
    return 0;
}

C++: #include <vector>
#include <cstdio>
#include <cstdlib>
using namespace std;
const int D[3][2]={{1,0},{0,1},{-1,0}};
int main(){
	int W,H,N,d,r=0;
	scanf("%d%d%d",&W,&H,&N);
	vector<vector<int>> v={{W,H,W},{W-1,H-2,W-1}}, p={{0,0},{0,1}}, c={{0,0},{0,0}};
	for(;N--;){
		scanf("%d",&d);
		p[d][0]+=D[c[d][0]][0];
		p[d][1]+=D[c[d][0]][1];
		c[d][1]++;
		if(c[d][1]==v[d][c[d][0]])c[d][0]++,c[d][1]=0;
		if(abs(p[0][0]-p[1][0])+abs(p[0][1]-p[1][1])<2)r++;
	}
	printf("%d\n",r);
}