#include #include #include #include clock_t pocetno, krajnje; using namespace std; char smer[4]={'N','E','S','W'}; struct slog{ int r,c; } roboti[25]; struct slogresenja{ int sc,bk,level; slog roboti[25]; char komande[200005]; } temp,fsol, bestsol; int R,C,T,K,minr,minc,maxr,maxc,glob,dokle;/// temporary, final za level, najbolji od svih. char M[155][155],MC[155][155]; int Levels[12][22][2]; bool imapromene, vreme, veliki; void ispisiresenje(){ cout << bestsol.level << endl; for (int i=1; i<=bestsol.bk; i++) cout << bestsol.komande[i]; cout << endl; } void ispisitemp(){ printf("Level==%d\n",temp.level); printf("cs ==%d\n",temp.sc); printf("bk ==%d\n",temp.bk); for (int i=1; i<=temp.bk; i++) cout << temp.komande[i]; cout << endl; } void ispisifsol(){ cout << fsol.level << endl; for (int i=1; i<=fsol.bk; i++) cout << fsol.komande[i]; cout << endl; } int scoring(){ minr=150; maxr=0; minc=150; maxc=0; int sc=max(1,temp.bk); for (int i=1; i<=K; i++){ minr=min(minr,temp.roboti[i].r); minc=min(minc,temp.roboti[i].c); maxr=max(maxr,temp.roboti[i].r); maxc=max(maxc,temp.roboti[i].c); } // cout << minr << " " << minc << " " << maxr << " " << maxc << endl; return sc+(maxr-minr+maxc-minc)*(R+C); } void checklevel(){ temp.sc=scoring(); // if (glob==2) printf("bk==%d score==%d\n",temp.bk,temp.sc); // printf("score je %d\n",temp.sc); if (temp.sc2500) return; if (krajnje-pocetno>4500) {vreme=true; return;} temp=fsol; } } void idilevo(int koliko){ for (int i=1; i<=koliko; i++){ temp.bk++; temp.komande[temp.bk]='E'; pomerirobote('E'); checklevel(); } } void ididesno(int koliko){ for (int i=1; i<=koliko; i++){ temp.bk++; temp.komande[temp.bk]='W'; pomerirobote('W'); checklevel(); } } void idigore(int koliko){ for (int i=1; i<=koliko; i++){ temp.bk++; temp.komande[temp.bk]='N'; pomerirobote('N'); checklevel(); } } void ididole(int koliko){ for (int i=1; i<=koliko; i++){ temp.bk++; temp.komande[temp.bk]='S'; pomerirobote('S'); checklevel(); } } void V(){ for (int x=1; x<=T; x++) { memcpy(M,MC,sizeof(M)); for (int y=1; y<=K; y++){ roboti[y].r=Levels[x][y][0]; roboti[y].c=Levels[x][y][1]; } temp.level=x; for (int i=1; i<=K; i++) temp.roboti[i]=roboti[i]; temp.bk=0; temp.sc=scoring(); fsol.sc=2000000000; for (int i=1; i<=20; i++){ idilevo(C); temp=fsol; ididesno(C); temp=fsol; idigore(R); temp=fsol; ididole(R); } checkall(); } } int main(){ pocetno=clock(); freopen("robots.in","r",stdin); bestsol.sc=2000000000; memset(M,'#',sizeof(M)); cin >> R >> C; veliki=R*C>8000; if (R<=10 and R<=50) dokle=8; if (R<=50 and R<=100) dokle=8; if (veliki) dokle=6; if (K==2) { dokle=8; veliki=false;} for (int i=1; i<=R; i++) for (int j=1; j<=C; j++){ cin >> M[i][j]; } memcpy(MC,M,sizeof(MC)); cin >> T >> K; for (int i=1; i<=T; i++) for (int j=1; j<=K; j++) cin >> Levels[i][j][0] >> Levels[i][j][1]; // if (veliki) V(); // else for (int x=1; x<=T; x++) { memcpy(M,MC,sizeof(M)); for (int y=1; y<=K; y++){ roboti[y].r=Levels[x][y][0]; roboti[y].c=Levels[x][y][1]; } temp.level=x; for (int i=1; i<=K; i++) temp.roboti[i]=roboti[i]; temp.bk=0; temp.sc=scoring(); fsol.sc=2000000000; algo(); checklevel(); if (vreme) break; temp=fsol; idilevo(C); temp=fsol; ididesno(C); temp=fsol; idigore(R); temp=fsol; ididole(R); checkall(); } checkall(); cout << bestsol.sc << endl; freopen("robots.out","w",stdout); ispisiresenje(); return 0; }