#include #include #include #include #include #include using namespace std; const int xMax=128; struct MyStruct { int n, m, bonus, maxPoints; char table[xMax][xMax]; }; int places, dogs; char ch; clock_t tstart; //(clock()-tstart)/(CLOCKS_PER_SEC*1.0) MyStruct a[xMax]; void f1(int iii, int x, int y, int cnt, pair used[][xMax], vector > comp[xMax]) { comp[cnt][0].first+=(a[iii].table[x][y]-'0'); used[x][y].first=cnt; comp[cnt].push_back({x, y}); if(x-1>=1) { if(a[iii].table[x-1][y]!='#') { if(used[x-1][y].first==0) { used[x][y].second++; used[x-1][y].second++; f1(iii, x-1, y, cnt, used, comp); } } } if(x+1<=a[iii].n) { if(a[iii].table[x+1][y]!='#') { if(used[x+1][y].first==0) { used[x][y].second++; used[x+1][y].second++; f1(iii, x+1, y, cnt, used, comp); } } } if(y-1>=1) { if(a[iii].table[x][y-1]!='#') { if(used[x][y-1].first==0) { used[x][y].second++; used[x][y-1].second++; f1(iii, x, y-1, cnt, used, comp); } } } if(y+1<=a[iii].m) { if(a[iii].table[x][y+1]!='#') { if(used[x][y+1].first==0) { used[x][y].second++; used[x][y+1].second++; f1(iii, x, y+1, cnt, used, comp); } } } } bool cmp1(pair aa, pair bb) { return aa.second>bb.second; } void f11(int xx, int yy, int compNow, int limit, int curr, string s, string &smax, pair used[][xMax], char chh) { if(curr==limit) { if(smax=="") { smax=s; } else if(s.size()1) { if(!(s[s.size()-2]=='U' && s[s.size()-1]=='D')) { f11(xx-1, yy, compNow, limit, (chh=='D'?curr:curr+1), s+"U", smax, used, 'U'); } } else { f11(xx-1, yy, compNow, limit, (chh=='D'?curr:curr+1), s+"U", smax, used, 'U'); } } if(used[xx+1][yy].first==compNow) { if(s.size()>1) { if(!(s[s.size()-2]=='D' && s[s.size()-1]=='U')) { f11(xx+1, yy, compNow, limit, (chh=='U'?curr:curr+1), s+"D", smax, used, 'D'); } } else { f11(xx+1, yy, compNow, limit, (chh=='U'?curr:curr+1), s+"D", smax, used, 'D'); } } if(used[xx][yy-1].first==compNow) { if(s.size()>1) { if(!(s[s.size()-2]=='L' && s[s.size()-1]=='R')) { f11(xx, yy-1, compNow, limit, (chh=='R'?curr:curr+1), s+"L", smax, used, 'L'); } } else { f11(xx, yy-1, compNow, limit, (chh=='R'?curr:curr+1), s+"L", smax, used, 'L'); } } if(used[xx][yy+1].first==compNow) { if(s.size()>1) { if(!(s[s.size()-2]=='R' && s[s.size()-1]=='L')) { f11(xx, yy+1, compNow, limit, (chh=='L'?curr:curr+1), s+"R", smax, used, 'R'); } } else { f11(xx, yy+1, compNow, limit, (chh=='L'?curr:curr+1), s+"R", smax, used, 'R'); } } //RLR //LRL //UDU //DUD } void task1(int iii) { pair used[xMax][xMax]; int cnt=1; vector > Queue; vector > comp[10001]; for(int i=0; i