Language: C++

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#define calc __builtin_popcount
using namespace std;
int n,G[5][5],ans;
map<int,int>vis;
vector<int>move[30];
int H(int sta,int k){
	return (calc(sta)+k-1)/k;	
}
int Q[50];
void show(int sta){
	for(int r=0;r<n;r++){
		for(int c=0;c<n;c++)
			putchar((sta&(1<<(r*n+c)))?'1':'0');
		puts("");
	}
	puts("----");
}
void ws(int dep,int sta,int k,int last){
	if(dep==3||k==0)return;
	if(vis.find(sta)==vis.end()){
		vis[sta]=dep;
	}else if(vis[sta]>dep){
		vis[sta]=dep;
	}
	for(int i=last;i<move[k].size();i++){
		ws(dep+1,sta^move[k][i],k,i);
	}
	ws(dep,sta,k-1,0);
}
bool succ;
void DFS(int dep,int k,int sta,int last){
	if(k==0)return;
	if(dep+H(sta,k)>=ans)return;
	if(sta==0){
		ans=dep;
		/*printf("ans:%d\n",ans);
		for(int i=0;i<dep;i++){
			show(Q[i]);
		}*/
		return;
	}
	for(int i=last;i<move[k].size();i++){
		DFS(dep+1,k,sta^move[k][i],i);
	}
	DFS(dep,k-1,sta,0);
}
int lim;
bool DFS5(int dep,int k,int sta,int last){
	if(dep+H(sta,k)>lim)return false;
	if(vis.find(sta)!=vis.end()){
		ans=dep+vis[sta];
		return true;
	}
	for(int i=last;i<move[k].size();i++){
		if(DFS5(dep+1,k,sta^move[k][i],i))return true;
	}
	if(DFS5(dep,k-1,sta,0))return true;
	return false;
}
void init(){
	for(int r=1;r<=n;r++)
	for(int c=1;c<=n;c++){
		for(int i=0;i+r-1<n;i++)
		for(int j=0;j+c-1<n;j++){
			int sta=0;
			for(int y=i;y<i+r;y++)
			for(int x=j;x<j+c;x++)
				sta^=(1<<(y*n+x));
			move[r*c].push_back(sta);
		}
	}
}
map<int,int> aa;

int chan(){
	int sta=0;
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			if(G[i][j])sta^=(1<<(i*n+j));
	return sta;
}			

void init2(){
	aa.clear();
	memset(G,0,sizeof(G));
	G[0][0]=1;G[0][2]=1;G[0][4]=1;
	G[2][0]=1;G[2][2]=1;G[2][4]=1;
	G[4][0]=1;G[4][2]=1;G[4][4]=1;
	aa[chan()]=9;
	
	int dx[9]={0,0,0,2,2,2,4,4,4};
	int dy[9]={0,2,4,0,2,4,0,2,4};
	for (int i=0;i<9;++i){
		G[dx[i]][dy[i]]=0;
		aa[chan()]=8;
		G[dx[i]][dy[i]]=1;
	}
	
	for (int i=0;i<8;++i)
	 for (int j=i+1;j<9;++j){
		G[dx[i]][dy[i]]=0;
		G[dx[j]][dy[j]]=0;
		aa[chan()]=7;
		G[dx[i]][dy[i]]=1;
		G[dx[j]][dy[j]]=1;
	 }
}

int main(){
		
	n=5;
	init();
	ws(0,0,25,0);
	int maxans=0;
	//printf("%d\n",vis.size());
	
	init2();
	
	while(cin>>n){
		if(n==0)break;
		for(int i=0;i<=n*n;i++)move[i].clear();
		init();
		int sta=0;
		for(int i=0;i<n;i++)
			for(int j=0;j<n;j++){
				scanf("%d",&G[i][j]);
				if(G[i][j])sta^=(1<<(i*n+j));
			}
//		cout << sta << endl;
		if (aa.find(sta)!=aa.end()){
			printf("%d\n",aa[sta]);
			continue;
		}
		succ=false;
		ans=10;
		if(n<5){
			DFS(0,n*n,sta,0);
		}else{
			for(lim=1;;lim++){
				if(DFS5(0,n*n,sta,0))
					break;
			}
		}
		/*
		if(ans>maxans){
			cout<<ans<<endl;
			for (int i=0;i<5;++i){
				for (int j=0;j<5;++j) printf("%d ",G[i][j]);
				puts("");
			}
			maxans=ans;
		}*/
	}
}

Compiler output:
a.cc: In function 'void ws(int, int, int, int)':
a.cc:32:26: error: reference to 'move' is ambiguous
   32 |         for(int i=last;i<move[k].size();i++){
      |                          ^~~~
In file included from /usr/include/c++/14/algorithm:86,
                 from a.cc:6:
/usr/include/c++/14/pstl/glue_algorithm_defs.h:353:1: note: candidates are: 'template<class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> std::move(_ExecutionPolicy&&, _ForwardIterator1, _ForwardIterator1, _ForwardIterator2)'
  353 | move(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __d_first);
      | ^~~~
In file included from /usr/include/c++/14/bits/specfun.h:43,
                 from /usr/include/c++/14/cmath:3906,
                 from /usr/include/c++/14/math.h:36,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_algobase.h:675:5: note:                 'template<class _II, class _OI> _OI std::move(_II, _II, _OI)'
  675 |     move(_II __first, _II __last, _OI __result)
      |     ^~~~
In file included from /usr/include/c++/14/bits/stl_pair.h:61,
                 from /usr/include/c++/14/bits/stl_algobase.h:64:
/usr/include/c++/14/bits/move.h:137:5: note:                 'template<class _Tp> constexpr typename std::remove_reference<_Tp>::type&& std::move(_Tp&&)'
  137 |     move(_Tp&& __t) noexcept
      |     ^~~~
a.cc:12:12: note:                 'std::vector<int> move [30]'
   12 | vector<int>move[30];
      |            ^~~~
a.cc:33:30: error: reference to 'move' is ambiguous
   33 |                 ws(dep+1,sta^move[k][i],k,i);
      |                              ^~~~
/usr/include/c++/14/pstl/glue_algorithm_defs.h:353:1: note: candidates are: 'template<class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> std::move(_ExecutionPolicy&&, _ForwardIterator1, _ForwardIterator1, _ForwardIterator2)'
  353 | move(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __d_first);
      | ^~~~
/usr/include/c++/14/bits/stl_algobase.h:675:5: note:                 'template<class _II, class _OI> _OI std::move(_II, _II, _OI)'
  675 |     move(_II __first, _II __last, _OI __result)
      |     ^~~~
/usr/include/c++/14/bits/move.h:137:5: note:                 'template<class _Tp> constexpr typename std::remove_reference<_Tp>::type&& std::move(_Tp&&)'
  137 |     move(_Tp&& __t) noexcept
      |     ^~~~
a.cc:12:12: note:                 'std::vector<int> move [30]'
   12 | vector<int>move[30];
      |            ^~~~
a.cc: In function 'void DFS(int, int, int, int)':
a.cc:49:26: error: reference to 'move' is ambiguous
   49 |         for(int i=last;i<move[k].size();i++){
      |                          ^~~~
/usr/include/c++/14/pstl/glue_algorithm_defs.h:353:1: note: candidates are: 'template<class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> std::move(_ExecutionPolicy&&, _ForwardIterator1, _ForwardIterator1, _ForwardIterator2)'
  353 | move(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __d_first);
      | ^~~~
/usr/include/c++/14/bits/stl_algobase.h:675:5: note:                 'template<class _II, class _OI> _OI std::move(_II, _II, _OI)'
  675 |     move(_II __first, _II __last, _OI __result)
      |     ^~~~
/usr/include/c++/14/bits/move.h:137:5: note:                 'template<class _Tp> constexpr typename std::remove_reference<_Tp>::type&& std::move(_Tp&&)'
  137 |     move(_Tp&& __t) noexcept
      |     ^~~~
a.cc:12:12: note:                 'std::vector<int> move [30]'
   12 | vector<int>move[30];
      |            ^~~~
a.cc:50:33: error: reference to 'move' is ambiguous
   50 |                 DFS(dep+1,k,sta^move[k][i],i);
      |                                 ^~~~
/usr/include/c++/14/pstl/glue_algorithm_defs.h:353:1: note: candidates are: 'template<class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> std::move(_ExecutionPolicy&&, _ForwardIterator1, _ForwardIterator1, _ForwardIterator2)'
  353 | move(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __d_first);
      | ^~~~
/usr/include/c++/14/bits/stl_algobase.h:675:5: note:                 'template<class _II, class _OI> _OI std::move(_II, _II, _OI)'
  675 |     move(_II __first, _II __last, _OI __result)
      |     ^~~~
/usr/include/c++/14/bits/move.h:137:5: note:                 'template<class _Tp> constexpr typename std::remove_reference<_Tp>::type&& std::move(_Tp&&)'
  137 |     move(_Tp&& __t) noexcept
      |     ^~~~
a.cc:12:12: note:                 'std::vector<int> move [30]'
   12 | vector<int>move[30];
      |            ^~~~
a.cc: In function 'bool DFS5(int, int, int, int)':
a.cc:61:26: error: reference to 'move' is ambiguous
   61 |         for(int i=last;i<move[k].size();i++){
      |                          ^~~~
/usr/include/c++/14/pstl/glue_algorithm_defs.h:353:1: note: candidates are: 'template<class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> std::move(_ExecutionPolicy&&, _ForwardIterator1, _ForwardIterator1, _ForwardIterator2)'
  353 | move(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __d_first);
      | ^~~~
/usr/include/c++/14/bits/stl_algobase.h:675:5: note:                 'template<class _II, class _OI> _OI std::move(_II, _II, _OI)'
  675 |     move(_II __first, _II __last, _OI __result)
      |     ^~~~
/usr/include/c++/14/bits/move.h:137:5: note:                 'template<class _Tp> constexpr typename std::remove_reference<_Tp>::type&& std::move(_Tp&&)'
  137 |     move(_Tp&& __t) noexcept
      |     ^~~~
a.cc:12:12: note:                 'std::vector<int> move [30]'
   12 | vector<int>move[30];
      |            ^~~~
a.cc:62:37: error: reference to 'move' is ambiguous
   62 |                 if(DFS5(dep+1,k,sta^move[k][i],i))return true;
      |                                     ^~~~
/usr/include/c++/14/pstl/glue_algorithm_defs.h:353:1: note: candidates are: 'template<class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> std::move(_ExecutionPolicy&&, _ForwardIterator1, _ForwardIterator1, _ForwardIterator2)'
  353 | move(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __d_first);
      | ^~~~
/usr/include/c++/14/bits/stl_algobase.h:675:5: note:                 'template<class _II, class _OI> _OI std::move(_II, _II, _OI)'
  675 |     move(_II __first, _II __last, _OI __result)
      |     ^~~~
/usr/include/c++/14/bits/move.h:137:5: note:                 'template<class _Tp> constexpr typename std::remove_reference<_Tp>::type&& std::move(_Tp&&)'
  137 |     move(_Tp&& __t) noexcept
      |     ^~~~
a.cc:12:12: note:                 'std::vector<int> move [30]'
   12 | vector<int>move[30];
      |            ^~~~
a.cc: In function 'void init()':
a.cc:76:25: error: reference to 'move' is ambiguous
   76 |                         move[r*c].push_back(sta);
      |                         ^~~~
/usr/include/c++/14/pstl/glue_algorithm_defs.h:353:1: note: candidates are: 'template<class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> std::move(_ExecutionPolicy&&, _ForwardIterator1, _ForwardIterator1, _ForwardIterator2)'
  353 | move(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __d_first);
      | ^~~~
/usr/include/c++/14/bits/stl_algobase.h:675:5: note:                 'template<class _II, class _OI> _OI std::move(_II, _II, _OI)'
  675 |     move(_II __first, _II __last, _OI __result)
      |     ^~~~
/usr/include/c++/14/bits/move.h:137:5: note:                 'template<class _Tp> constexpr typename std::remove_reference<_Tp>::type&& std::move(_Tp&&)'
  137 |     move(_Tp&& __t) noexcept
      |     ^~~~
a.cc:12:12: note:                 'std::vector<int> move [30]'
   12 | vector<int>move[30];
      |            ^~~~
a.cc: In function 'void init2()':
a.cc:92:9: error: 'memset' was not declared in this scope
   92 |         memset(G,0,sizeof(G));
      |         ^~~~~~
a.cc:8:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    7 | #include<map>
  +++ |+#include <cstring>
    8 | #define calc __builtin_popcount
a.cc: In function 'int main()':
a.cc:128:40: error: reference to 'move' is ambiguous
  128 |                 for(int i=0;i<=n*n;i++)move[i].clear();
      |                                        ^~~~
/usr/include/c++/14/pstl/glue_algorithm_defs.h:353:1: note: candidates are: 'template<class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> std::move(_ExecutionPolicy&&, _ForwardIterator1, _ForwardIterator1, _ForwardIterator2)'
  353 | move(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __d_first);
      | ^~~~
/usr/include/c++/14/bits/stl_algobase.h:675:5: note:                 'template<class _II, class _OI> _OI std::move(_II, _II, _OI)'
  675 |     move(_II __first, _II __last, _OI __result)
      |     ^~~~
/usr/include/c++/14/bits/move.h:137:5: note:                 'template<class _Tp> constexpr typename std::remove_reference<_Tp>::type&& std::move(_Tp&&)'
  137 |     move(_Tp&& __t) noexcept
      |     ^~~~
a.cc:12:12: note:                 'std::vector<int> move [30]'
   12 | vector<int>move[30];
      |            ^~~~