---
title: "C++ and Langton's ant"
author: "易水侠客"
date: "2013-05-29T21:26:59Z"
tags: []
slug: "c-and-langton-s-ant"
draft: false
lastmod: "2013-05-29T22:06:36Z"
id: 1879679
url: "https://shadowwalker.iteye.com/blog/1879679"
---

```
// ant.cpp
#include <iostream>
using namespace std;
int main(void){
	int N=5;
	int **G= new int*[N];
	for(int i=0;i<N;i++) G[i] = new int[N];
	for(int j=0;j<N;j++){
		for(int k=0;k<N;k++){
			G[j][k]=1;
		}
	}
	int mold=3;int nold=2;int mnew=2;int nnew=2;
	int t=0;int T=40;
	while(t<T){
		if((mnew<mold)&&(nnew==nold)){
			G[mnew][nnew]=-G[mnew][nnew];
			mold=mnew;nold=nnew;nnew=(nnew+1)%N;goto L;
		}
		if((mnew==mold)&&(nnew<nold)){
			G[mnew][nnew]=-G[mnew][nnew];
			mold=mnew;
			nold=nnew;
			mnew=(mnew+1)%N;goto L;
		}
		if((mnew>mold)&&(nnew==nold)){
			G[mnew][nnew]=-G[mnew][nnew];
			mold=mnew;nold=nnew;nnew=(nnew-1+N)%N;goto L;
		}
		if((mnew==mold)&&(nnew>nold)){
			G[mnew][nnew]=-G[mnew][nnew];
			mold=mnew;nold=nnew;mnew=(mnew-1+N)%N;goto L;
		}
L:
		cout<<mnew<<" "<<nnew<<" "<<G[mnew][nnew]<<endl;
		t++;
	}
	for(int l=0;l<N;l++) delete[] G[1];delete[] G;
	return 0;
}
```