Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)
var (
	table, ans = [12][12]int{}, [12][12]int{}
)

func main() {
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 1; j < 11; j++ {
			scanner.Scan()
			input := strings.Split(scanner.Text(), " ")
			tmp := [10]int{}
			for k := range input {
				tmp[k], _ = strconv.Atoi(input[k])
			}
			for l := 1; l < 11; l++ {
				table[j][l] = tmp[l-1]
			}
		}
		check(table, 1, ans)
	}
}

func check(table [12][12]int, i int, ans [12][12]int) {
	if i == 11 {
		solve(table, 2, 1, ans)
		return
	}
	ans[1][i] = 0
	check(table, i+1, ans)
	ans[1][i] = 1
	attack(table, 1, i)
	check(table, i+1, ans)
	attack(table, 1, i)
}

func solve(table [12][12]int, i, j int, ans [12][12]int) {
	if i == 11 {
		flag := true
		for k := 1; k < 11; k++ {
			if table[10][k] == 1 {
				flag = false
				break
			}
		}
		if flag {
			printans(ans)
		}
		return
	}
	if table[i-1][j] == 1 {
		ans[i][j] = 1
		attack(table, i, j)
		if j == 10 {
			solve(table, i+1, 1, ans)
		} else {
			solve(table, i, j+1, ans)
		}
		attack(table, i, j)
		ans[i][j] = 0
	} else {
		ans[i][j] = 0
		if j == 10 {
			solve(table, i+1, 1, ans)
		} else {
			solve(table, i, j+1, ans)
		}
	}
}

func printans(ans [12][12]int) {
	for i := 1; i < 11; i++ {
		for j := 1; j < 11; j++ {
			fmt.Print(ans[i][j])
			if j < 10 {
				fmt.Print(" ")
			}
		}
		fmt.Println()
	}
}

func attack(table [12][12]int, i, j int) {
	table[i][j] = 1 - table[i][j]
	table[i-1][j] = 1 - table[i-1][j]
	table[i+1][j] = 1 - table[i+1][j]
	table[i][j-1] = 1 - table[i][j-1]
	table[i][j+1] = 1 - table[i][j+1]
}

C++: #include <iostream>
#include <algorithm>
#define SIZE 10
using namespace std;

class State{
public:
  int data[SIZE][SIZE];
  int put[SIZE][SIZE];
  State() {
    fill((int*)put, (int*)put+SIZE*SIZE, 0);
  }
  void puts(int x, int y) {
    static int d[5][2] = {{0,-1},{1,0},{0,1},{-1,0},{0,0}};
    put[y][x] = 1;
    for(int k = 0; k < 5; k++) {
      int nx = x + d[k][0];
      int ny = y + d[k][1];
      if(nx < 0 || nx >= SIZE) continue;
      if(ny < 0 || ny >= SIZE) continue;
      data[ny][nx] = -(data[ny][nx] - 1);
    }
  }
  bool isAll0() {
    for(int i = 0; i < SIZE; i++) {
      for(int j = 0; j < SIZE; j++) {
	if(data[i][j] != 0) return false;
      }
    }
    return true;
  }
  void show() {
    for(int i = 0; i < SIZE; i++) {
      for(int j = 0; j < SIZE; j++) {
	cout << put[i][j];
      }
      cout << endl;
    }
  }
};

bool rec(State s, int x, int y) {
  static int d[5][2] = {{0,-1},{1,0},{0,1},{-1,0},{0,0}};
  if(s.isAll0()) {
    s.show();
    return true;
  }

  for(int i = y; i < SIZE; i++) {
    int j;
    if(i == y) j = x;
    else       j = 0;
    for(; j < SIZE; j++) {
      bool flag = false;
      /*for(int k = 0; k < 5; k++) {
	int nx = j + d[k][0];
	int ny = i + d[k][1];
	if(nx < 0 || nx >= SIZE) continue;
	if(ny < 0 || ny >= SIZE) continue;
	if(s.data[ny][nx] == 1) {
	  flag = true;
	  break;
	}
	}*/
      flag = true;
      if(flag) {
	if(i > 0 && s.data[i-1][j] == 0) continue;
	State tmp = s;
	tmp.puts(j, i);
	int nx, ny;
	nx = j + 1;
	ny = i;
	if(nx >= SIZE) {
	  nx = 0;
	  ny++;
	}
	if(rec(tmp, nx, ny)) {
	  return true;
	}
	if(i > 0 && s.data[i-1][j] == 1) return false;
      }
    }
  }
  return false;
}

main() {
  int n;
  cin >> n;
  for(int t = 0; t < n; t++) {
    State s;
    for(int i = 0; i < SIZE; i++) {
      for(int j = 0; j < SIZE; j++) {
	cin >> s.data[i][j];
      }
    }
    rec(s, 0, 0);
  }
}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>

using namespace std;

typedef long long ll;
typedef pair<char,int> P;

void flip(int x,int y,int stage[10][10]){
	const int t[][2] = {{0,0},{-1,0},{1,0},{0,1},{0,-1}};
	for(int i=0;i<5;i++){
		int dx = x+t[i][0];
		int dy = y+t[i][1];

		if(dx < 0 || dx >= 10 || dy < 0 || dy >= 10) continue;

		stage[dy][dx]*=-1;
	}
}

bool checkStage(int store[10][10],int x,int y){
	return store[y][x] == -1 ? true : false;
}

bool isClear(int store[10][10]){
	for(int y=0;y<10;y++){
		for(int x=0;x<10;x++){
			if(store[y][x]==1) return false;
		}
	}

	return true;
}

void writeResult(int result[10][10]){
	for(int y=0;y<10;y++){
		for(int x=0;x<10;x++){
			printf("%d%s",result[y][x],x==9 ? "\n" : " ");
		}
	}
}

int main(){

	int n;
	while(~scanf("%d",&n)){
		for(int i=0;i<n;i++){
			int stage[10][10];
			
			for(int y=0;y<10;y++){
				for(int x=0;x<10;x++){
					int num;
					scanf("%d",&num);
					stage[y][x] = (num == 0 ? -1 : 1);
				}
			}
			
			for(int S=0;S<(1 << 10)-1;S++){
				int store[10][10];
				int result[10][10];
				memset(result,0,sizeof(result));
				//memset(store,0,sizeof(store));
				memcpy(store,stage,sizeof(stage));

				for(int j=0;j<10;j++){
					if((S>>j) & 1){
						flip(j,0,store);
						result[0][j]=1;
					}
				}

				for(int y=1;y<10;y++){
					for(int x=0;x<10;x++){
						flip(x,y,store);
						if(!checkStage(store,x,y-1)){
							flip(x,y,store);
						}
						else{
							result[y][x] = 1;
						}
					}
				}

				if(isClear(store)){
					writeResult(result);
					break;
				}
			}

		}
	}
}

C++: #include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < j; ++i)
#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define P pair<int, int>
const int H = 10;
const int W = 10;
typedef vector< vector<int> > ivv;
typedef vector<int> iv;

int my[5] = {1, -1, 0, 0, 0};
int mx[5] = {0, 0, 1, -1, 0};

class C{
  public:
    ivv u;
    int t;
    C(){}
    C(ivv _u, int _t){ u = _u; t = _t; }
};

queue<C> open;
set<ivv> S;

void rev(ivv &v, int y, int x){
  REP(i, 5){
    int ny = y + my[i], nx = x + mx[i];
    if(ny >= 0 && nx >= 0 && ny < H && nx < W) v[ny][nx] = (v[ny][nx] + 1) % 2;
  }
}

bool next(ivv v, ivv &u, int y, int x){
  REP(i, H) REP(j, W) if(u[i][j]) rev(v, i, j);
  int a = 0, b = 0;
  REP(i, 5){
    int ny = y + my[i], nx = x + mx[i];
    if(ny >= 0 && nx >= 0 && ny < H && nx < W){
      if(v[ny][nx]) ++a;
      else ++b;
    }
  }
  return a > b;
}


bool end(ivv v, ivv &u){
  REP(i, H) REP(j, W) if(u[i][j]) rev(v, i, j);
  REP(i, H) REP(j, W) if(v[i][j] == 1) return false;
  return true;
}

ivv bfs(ivv &v){
  open = queue<C>();
  S = set<ivv>();
  ivv _u(H, iv(W, 0));
  open.push(C(_u, 0));
  S.insert(_u);
  while(!open.empty()){
    C tmp = open.front(); open.pop();
    ivv u = tmp.u; int t = tmp.t;
    //cout <<"---------------" <<endl;
    //cout <<open.size() <<endl;
    //REP(i, H){
    //  REP(j, W) cout <<u[i][j] <<" ";
    //  cout <<endl;
    //}
    if(end(v, u)) return u;
    REP(y, H){
      REP(x, W){
        if(!u[y][x] && next(v, u, y, x)){
          rev(v, y, x);
          u[y][x] = true;
          if(S.find(u) == S.end()){
            S.insert(u);
            open.push(C(u, t + 1));
          }
          u[y][x] = false;
          rev(v, y, x);
        }
      }
    }
  }
  return ivv();
}

int main() {
  int T; cin >>T;
  REP(t, T){
    ivv v(H, iv(W));
    REP(i, H) REP(j, W) cin >>v[i][j];
    ivv ans = bfs(v);
    REP(i, H){
      REP(j, W) cout <<ans[i][j] <<(j + 1 == W ? "" : " ");
      cout <<endl;
    }
  }
  return 0;
}