#include #include #include using namespace std; char map[8][8]; char mapto[8][8]; int w,h; char dist[128][8][8]; int distscore[128]; int checklist[64]; int numcheck; void makechecklist(int ex,int ey){ for (int i =32; i<128; i++) { bool f = false; for (int y = 0; y <= h+1 ; y++ ) { for (int x = 0; x <= w+1 ; x++ ) { if (mapto[y][x] == i) { dist[i][y][x] = 0; f = true; } else { dist[i][y][x] = 99; } } } while (f) { f = false; for (int y = 1; y <= h ; y++ ) { for (int x = 1; x <= w ; x++ ) { int min = 99; if (dist[i][y][x+1]>=0 && dist[i][y][x+1] < min) min = dist[i][y][x+1]; if (dist[i][y][x-1]>=0 && dist[i][y][x-1] < min) min = dist[i][y][x-1]; if (dist[i][y+1][x]>=0 && dist[i][y+1][x] < min) min = dist[i][y+1][x]; if (dist[i][y-1][x]>=0 && dist[i][y-1][x] < min) min = dist[i][y-1][x]; if (dist[i][y][x] > min+1) { dist[i][y][x] = min+1; f = true; } } } } } char tmp[8][8]; for (int y = 0; y <= h+1 ; y++ ) { for (int x = 0; x <= w+1 ; x++ ) { tmp[y][x] = -1; } } for (int y = 1; y <= h ; y++ ) { for (int x = 1; x <= w ; x++ ) { if (map[y][x]!='=') { tmp[y][x] = dist['@'][y][x]; } } } // fix bool f = true; while (f) { f = false; for (int y = 1; y <= h ; y++ ) { for (int x = 1; x <= w ; x++ ) { if (tmp[y][x]<=0) continue; int v1 = 99, v2 = 99; // v1 > v2 if (tmp[y][x+1]>=0 && tmp[y][x+1] < v1) { v1 = tmp[y][x+1]; if (v1=0 && tmp[y][x-1] < v1) { v1 = tmp[y][x-1]; if (v1=0 && tmp[y+1][x] < v1) { v1 = tmp[y+1][x]; if (v1=0 && tmp[y-1][x] < v1) { v1 = tmp[y-1][x]; if (v10;i--) { for (int y = 1; y <= h ; y++ ) { for (int x = 1; x <= w ; x++ ) { if (tmp[y][x] == i) { checklist[numcheck] = y*8+x; numcheck++; } distscore[mapto[y][x]] = tmp[y][x]; } } } } int check() { for (int i = 0; i < numcheck ; i++ ) { int x = checklist[i]%8; int y = checklist[i]/8; if (mapto[y][x] != map[y][x]) return numcheck-i; } return 0; } int calc_cost() { int c=0; for (int y = 1; y <= h ; y++ ) { for (int x = 1; x <= w ; x++ ) { c += dist[ map[y][x] ] [y][x] * distscore[ map[y][x] ] ; } } return c; } void putmap(char map[8][8]){ cerr << " ------" << endl; for (int y = 0; y < h ;y++) { cerr << " " << map[y+1] +1 << endl; } cerr << " ------" << endl; } int maxdepth = 21; int dd[4][2] = { {1, 0}, {-1,0}, {0,1}, {0,-1}}; char *dc = "RLDU"; int minsc = 0; char ans[200]; int solv(int depth,int restcost,int ccount,int x,int y, int d) { ans[depth] = '\0'; if (depth > maxdepth) {return check();} int c = calc_cost(); if (c == 0) return 0; if (c < restcost) { restcost = c; ccount = 0; } else { ccount ++; if (ccount > 14 || (c-restcost) > 40) { return 9999; } } int sc = check(); if (maxdepth-depth < sc-minsc) { return 9999; } if (sc < minsc) { minsc = sc; ccount = 0; } if (sc == 0) {exit(0);return sc;} char tmpans[40]; tmpans[0] = 0; int min = sc; int minlen = 0; for (int kt=d+1;kt strlen(ans+depth)) { ans[depth]=dc[k]; min = sc; strcpy(tmpans,ans+depth); minlen = strlen(ans+depth);} } strcpy(ans+depth,tmpans); return min; } int px,py; void move(const char *cmd) { while (*cmd!='\0') { int d = 0; if (*cmd == 'R') d = 0; if (*cmd == 'L') d = 1; if (*cmd == 'D') d = 2; if (*cmd == 'U') d = 3; int dx = dd[d][0] ,dy = dd[d][1]; map[py][px] = map[py+dy][px+dx]; px+=dx; py+=dy; map[py][px] = '@'; cmd++; } } int main() { ifstream ifs = ifstream("problems.txt"); int lx,rx,ux,dx, n; ifs >> lx >> rx >> ux >> dx; ifs >> n; int score = 0; for (int i=0; i> s; w = s[0]-'0'; h = s[2]-'0'; for (int y = 0; y <= h+1 ; y++ ) { for (int x = 0; x <= w+1 ; x++ ) { map[y][x] = 0; mapto[y][x] = 0; } } int p = 4, a = '1'; int ex = 0,ey = 0; for (int y = 1; y <= h ; y++ ) { for (int x = 1; x <= w ; x++ ) { map[y][x] = s[p]; mapto[y][x] = a; if (a=='9') { a='A'; } else { a++; } if (s[p]== '=') { mapto[y][x] = '='; } else if (s[p] == '0') { px = x; py = y; map[y][x] = '@'; ex = x; ey = y; } else { ex = x; ey = y; } p++; } } mapto[ey][ex] = '@'; string answer; bool scoref = true; cerr << "** " << i << " ** " << endl; putmap(map); makechecklist(ex,ey); int tsc = check(); cerr << i << " rest:" << tsc << endl; //putmap(mapto); bool solved = false; maxdepth = 24; minsc = 999; for (int k=0;k<20;k++) { int sc = solv(0,9999,0,px,py,-1); answer += ans; if (sc == 0) { solved = true; cerr << i << " solved!" << endl; break; } else { move(ans); cerr << i << " rest:" << sc << ":" << ans << endl; } if (sc >= tsc) { maxdepth+=4; if (maxdepth > 34) {putmap(map);break;} } else { maxdepth = 24; } tsc = sc; } if (solved){ //putmap(map); for (int p=0; p