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 <bits/stdc++.h>
using namespace std;
 
int main() {
  
  vector<vector<int>> sp(4, vector<int>(3, 1));
  sp.at(1) = {0, 1, 1};
  sp.at(2) = {1, 0, 1};
  sp.at(3) = {1, 1, 0};

  int N, K;  
  cin >> N >> K;
  
  vector<int> A(N + 1);
  for (int i = 0 ; i < K; i++) {
    int a,b;
    cin >> a >> b;
    A.at(a) = b;
  }
  
 vector<vector<vector<int64_t>>> dp(N + 1,
         vector<vector<int64_t>>   (4, 
                 vector<int64_t>   (4, 0)));

  
  if (A.at(1) > 0) dp.at(1).at(0).at(A.at(1)) = 1;
  else {
    dp.at(1).at(0).at(1) = 1;
    dp.at(1).at(0).at(2) = 1;
    dp.at(1).at(0).at(3) = 1;
  }
  
  for (int i = 1; i < N; i++) {
    for (int j = 0; j < 4; j++) {
      for (int k = 1; k < 4; k++) {
        
        if (A.at(i+1) > 0 ) {
          if(j == k && k == A.at(i+1)) continue;
          dp.at(i+1).at(k).at(A.at(i+1)) += dp.at(i).at(j).at(k) % 10000;;
        } else {
          for (int l = 1; l < 4; l++) {
            if(j == k && k == l) continue;
            dp.at(i+1).at(k).at(l) += dp.at(i).at(j).at(k) % 10000;;
          }
        }
      }
    }
  }
  
  int ans = 0;
  for (int i = 1; i < 4; i++) {
    for (int j = 1; j < 4; j++) {
      ans += dp.at(N).at(i).at(j) ;
    }
  }

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

C++: #include<bits/stdc++.h>
using namespace std;
int dfs(int day,int tmt,int cream,int bazil);
int dfsmem[105][3][3][3];
int n,k;
int mem[105];
main(){
  cin>>n>>k;
  memset(mem,-1,sizeof(mem));
  memset(dfsmem,-1,sizeof(dfsmem));
  for(int i=0;i<k;i++){
    int a,b;
    cin>>a>>b;
    mem[a]=b;
  }
  cout<<dfs(1,0,0,0)<<endl;
}
int dfs(int day,int tmt,int cream,int bazil){
  if(day>n){
    return 1;
  }
  else if(tmt>=3||cream>=3||bazil>=3){
    return 0;
  }
  else if(dfsmem[day][tmt][cream][bazil]!=-1){
    return dfsmem[day][tmt][cream][bazil];
  }
  else if(mem[day]!=-1){
    if(mem[day]==1){
      return dfsmem[day][tmt][cream][bazil]=dfs(day+1,tmt+1,0,0)%10000;
    }
    else if(mem[day]==2){
      return dfsmem[day][tmt][cream][bazil]=dfs(day+1,0,cream+1,0)%10000;
    }
    else if(mem[day]==3){
      return dfsmem[day][tmt][cream][bazil]=dfs(day+1,0,0,bazil+1)%10000;
    }
  }
  else{
    int paturn=0;
    paturn+=dfs(day+1,tmt+1,0,0)%10000;
    paturn+=dfs(day+1,0,cream+1,0)%10000;
    paturn+=dfs(day+1,0,0,bazil+1)%10000;
    return dfsmem[day][tmt][cream][bazil]=paturn%10000;
  }
}

C++: #include<bits/stdc++.h>
#define rep(a,b) for(int a=0;a<b;a++)
#define int long long int

using namespace std;

const int INF = 1000000009;

int day,al;
int almost[200];
int ans=1;
int used[200];

int pasu(int p){
    if(p==3)return 1;
    else if(p==2)return 3;
    else if(p==1)return 2;
}

int pasu2(int p){
    if(p==3)return 2;
    else if(p==2)return 1;
    else if(p==1)return 3;
}

int kaburi1(int date){
    if(almost[date+1]!=0){
        return almost[date+1];
    }else return 0;
}

int kaburi2(int date){
    if(almost[date+2]!=0){
        return almost[date+2];
    }else return 0;
}

int pasuta(int d2, int d1, int dd1, int dd2,int date){
    if(date>day)return 1;
    else if(almost[date]!=0){
        return pasuta(d1,almost[date],kaburi1(date+1),kaburi2(date+1),date+1)%10000;
    }else if((d2==d1||d1==dd1)&&d1!=0){
       return (pasuta(d1,pasu(d1),kaburi1(date+1),kaburi2(date+1),date+1)+pasuta(d1,pasu2(d1),kaburi1(date+1),kaburi2(date+1),date+1))%10000;
    }else{
        return (pasuta(d1,1,kaburi1(date+1),kaburi2(date+1),date+1)+
        pasuta(d1,2,kaburi1(date+1),kaburi2(date+1),date+1)+
        pasuta(d1,3,kaburi1(date+1),kaburi2(date+1),date+1))%10000;
    }
}

signed main(){
    fill(almost,almost+200,0);
    fill(used,used+200,0);
    cin >> day >> al;
    rep(a,al){
        int da,pa;
        cin >> da >> pa;
        almost[da]=pa;
    }
    ans = pasuta(0,0,almost[2],almost[3],1);
    cout << ans%10000 << endl;
return 0;
}