Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

// 文字列を1行取得
func StrStdin() (stringReturned string) {
//	scanner := bufio.NewScanner(os.Stdin)

	scanner.Scan()
	stringInput := scanner.Text()

	stringReturned = strings.TrimSpace(stringInput)
	return
}

// 整数値1つ取得
func IntStdin() (int, error) {
	stringInput := StrStdin()
	return strconv.Atoi(strings.TrimSpace(stringInput))
}

// 空白や空文字だけの値を除去したSplit()
func SplitWithoutEmpty(stringTargeted string, delim string) (stringReturned []string) {
	stringSplited := strings.Split(stringTargeted, delim)

	for _, str := range stringSplited {
		if str != "" {
			stringReturned = append(stringReturned, str)
		}
	}

	return
}

// デリミタで分割して文字列スライスを取得
func SplitStrStdin(delim string) (stringReturned []string) {
	// 文字列で1行取得
	stringInput := StrStdin()

	// 空白で分割
	// stringSplited := strings.Split(stringInput, delim)
	stringReturned = SplitWithoutEmpty(stringInput, delim)

	return
}

// デリミタで分割して整数値スライスを取得
func SplitIntStdin(delim string) (intSlices []int, err error) {
	// 文字列スライスを取得
	stringSplited := SplitStrStdin(" ")

	// 整数スライスに保存
	for i := range stringSplited {
		var iparam int
		iparam, err = strconv.Atoi(stringSplited[i])
		if err != nil {
			return
		}
		intSlices = append(intSlices, iparam)
	}
	return
}

func main() {
	nk, _ := SplitIntStdin(" ")
	n, k := nk[0], nk[1]
	kind := make([]int, n)
	for i := 0; i < k; i++ {
		var tmp []int
		var err error
		if tmp, err = SplitIntStdin(" "); err != nil {
			panic(err)
		}
		kind[tmp[0]-1] = tmp[1]
	}
	dp := make([][3][3]int, n)
	if kind[0] > 0 {
		dp[0][0][kind[0]-1] = 1
	} else {
		for i := 0; i < 3; i++ {
			dp[0][0][i] = 1
		}
	}
	if kind[1] > 0 {
		for i := 0; i < 3; i++ {
			dp[1][i][kind[1]-1] = dp[0][0][i]
		}
	} else {
		for i := 0; i < 3; i++ {
			for j := 0; j < 3; j++ {
				dp[1][i][j] = dp[0][0][i]
			}
		}
	}
	for i := 1; i < n-1; i++ {
		if kind[i+1] > 0 {
			for j := 0; j < 3; j++ {
				if j == kind[i+1]-1 {
					for k := 0; k < 3; k++ {
						if j != k {
							dp[i+1][j][kind[i+1]-1] += dp[i][k][j]
							dp[i+1][j][kind[i+1]-1] %= 10000
						}
					}
				} else {
					for k := 0; k < 3; k++ {
						dp[i+1][j][kind[i+1]-1] += dp[i][k][j]
						dp[i+1][j][kind[i+1]-1] %= 10000
					}
				}
			}
		} else {
			for j := 0; j < 3; j++ {
				for k := 0; k < 3; k++ {
					for l := 0; l < 3; l++ {
						if j != k || k != l {
							dp[i+1][k][j] += dp[i][l][k]
							dp[i+1][k][j] %= 10000
						}
					}
				}
			}
		}
	}
	result := 0
	for i := 0; i < 3; i++ {
		for j := 0; j < 3; j++ {
			result += dp[n-1][i][j]
		}
	}
	// fmt.Println(dp)
	// fmt.Println(kind)
	fmt.Println(result % 10000)
}

C++: #include <iostream>
#include <functional>
#include <algorithm>
#include <climits>
#include <string>
#include <vector>
#include <map>
#include <cmath>

using namespace std;

namespace _{struct _{_(){ // initialize
  cin.tie(0); ios::sync_with_stdio(false);
}}_;}
template <typename T, size_t size>
inline size_t len(const T (&)[size]) { return size; }
#define println(s) cout << s << '\n';
#define str to_string
#define rep(i, n) for (int i=0; i<int(n); i++)
#define all(c) (c).begin(), (c).end()

#define MAX_N 110

int N, K;
int table[MAX_N];

bool invalid(int x) {
  return x != 0 && x != 1 && x != 2;
}

bool isConst(int offset) {
  return !invalid(table[offset]);
}

// (offset) A B ? ...
int solve(int offset, int A, int B) {
  // cout << "solve(" << offset <<", " << A <<", " << B <<")" << '\n';
  // x | x x ...
  if (offset >= 1) {
    int prev = table[offset-1];
    // fail
    if (isConst(offset-1) && prev == A && A == B) {
      cout << "fail?!" << endl;
      return 0;
    }
  }
  // | x y ? [wall]
  if (offset + 2 >= N) return 1; // goal
  if (offset >= 0 && (invalid(A) || invalid(B))) {
    cout << A << " " << B << endl;
    cout << "?!" << endl;
    exit(-1);
  }

  // A B C ...
  // shift
  if (isConst(offset+2)) {
    int next = table[offset+2];
    // check
    if (A == B && B == next) return 0;
    return solve(offset+1, B, table[offset+2]) % 10000;
  }

  // A B ? ...
  else {
    // x x [^x] ...
    if (A == B) {
      int a, b = -1;
      if (A == 0) { a = 1; b = 2; }
      if (A == 1) { a = 0; b = 2; }
      if (A == 2) { a = 0; b = 1; }
      return (solve(offset+1, B, a) + solve(offset+1, B, b)) % 10000;
    }
    // x y (any) ...
    else {
      return (solve(offset+1, B, 0) + solve(offset+1, B, 1) + solve(offset+1, B, 2)) % 10000;
    }
  }
}



int main() {
  cin >> N >> K;
  // initialize (-1 means ????¢????)
  rep(i, MAX_N) table[i] = -1;
  // input
  rep(i, K) {
    int a, b;
    cin >> a >> b;
    table[a] = b-1;
  }
  // rep(i, N) cout << "#" << i << ": " << table[i] << '\n'; // DEBUG
  // main
  cout << (solve(-2, -11, -12) % 10000) << '\n';
  return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
using namespace std;
//K[1-]
	int n,k;
	int K[101] ={};
	int ans=0;

	void sol(int now,int yest,int dob){

		if(dob == 2){
			return;
		}

		if(now == n+1){
			ans++;
			ans%=10000;
			return;
		}

		if(K[now] != -1){
			if(yest == K[now]){
				sol(now+1,K[now],dob+1);
				return;
			}
			else{
				sol(now+1,K[now],0);
				return;
			}
		}

		else{
			if(now!=1){
			sol(now+1,yest%3,dob+1);
			}
			else{
			sol(now+1,yest%3,0);
			}

			sol(now+1,(yest+1)%3,0);
			sol(now+1,(yest+2)%3,0);
			return;
		}
	}

int main(){

	cin >> n >> k;

	for(int i=0; i<n+1; i++){
		K[i] = -1;
	}


	for(int i=0; i<k; i++){
		int a,b;
		cin >> a >> b;
		K[a] = b%3;
	}

	sol(1,-1,0);

	cout << ans%10000 << endl;

	return 0;

}

C++: #include <bits/stdc++.h>
using namespace std;
#define Rep(i,N) for(int i = 0; i < N; i++)
#define INF 1 << 28
#define S second
#define F first
typedef pair<int,int> Pi;

inline void chmax(int &a, int b) { a = max(a,b); }
inline void chmin(int &a, int b) { a = min(a,b); }


int N, K;
int data[105];
int memo[105][4][2];

int dfs(int it, int ch, int cnt)
{
  int ret = 0;
  if(it == N)return 1;
  if(~memo[it][ch][cnt])return memo[it][ch][cnt];
  Rep(i,3) {
    if(data[it + 1] != INF && i != data[it + 1])continue;
    int nc;
    if(ch == i) nc = cnt + 1;
    else nc = 0;
    if(nc < 2) (ret += dfs(it + 1,i,nc)) %= 10000;
  }
  return memo[it][ch][cnt] = ret;
}

int main()
{
  memset(memo,-1,sizeof(memo));
  cin >> N >> K;
  int A, B;
  fill(data,data + 105,INF);
  Rep(i,K) {
    cin >> A >> B; --B;
    data[A] = B;
  }
  cout << dfs(0,3,0) << endl;
  return(0);
}