#include using namespace std; typedef unsigned int uint; typedef long long int ll; typedef unsigned long long int ull; #define debugv(v) printf("L%d %s => ",__LINE__,#v);for(auto e:v){cout< ",__LINE__,#m);for(int x=0;x<(w);x++){cout<<(m)[x]<<" ";}cout<>=1,k++)s=(s<<1)|(u&1);for(;0>=1)cout<<(s&1);}} #define TIME chrono::system_clock::now() #define MILLISEC(t) (chrono::duration_cast(t).count()) template ostream& operator <<(ostream &o,const pair p){o<<"("<>,int> dp; void flipx(vector>& field){ int x,y; for (y=0;y>& field){ int x,y; for (y=0;y> height >> width; vector> first; // cww vector> final; // zero for (y=0;y> first[y][x]; } } // all zero queue>> > q; q.push(make_pair(1,final)); dp[final] = 1; q.push(make_pair(10001,first)); dp[first] = 10001; auto tim = TIME; while (!q.empty()){ if (MILLISEC(TIME-tim)>1800) abort(); pair>> p = q.front(); q.pop(); //if (dp.size()>100000){ // cout << dp.size() << endl;return 0; //} if (p.first<10000 && p.second == first){ cout << (p.first-1) << endl; return 0; } if (p.first>=10000 && p.second == final){ cout << (p.first-10001) << endl; return 0; } for (y=0;y> tmp = p.second; for (i=-1+(x==0);i<=1-(x==width-1);i++){ for (j=-1+(y==0);j<=1-(y==height-1);j++){ tmp[y+j][x+i]^=1; } } //vector> tmp2 = tmp; bool flg = true; for (i=0;i<4;i++){ if (i%2==1){ flipx(tmp); } if ((i/2)%2==1){ flipy(tmp); } if (dp.count(tmp)){ flg = false; int &val = dp[tmp]; if ((p.first<=10000)^(val<=10000)){ //printf("%d %d\n",p.first,val); //cout << "!"; cout << (p.first+val-10001) << endl; return 0; } }else{ //val = p.first+1; } } if (flg){ dp[tmp] = p.first+1; q.push(make_pair(p.first+1,tmp)); } } } } //cout << dp.size() << endl; cout << "Impossible" << endl; return 0; }