Borg mazetime limit:1000msmemory limit:65536kbthis problem would be judged onPKU. Original id:3026
64-bit integer IO format: %lld Java class name: Main The Borg is a immensely powerful race of enhanced humanoids from the Delta quadrant of the Galaxy. The Borg Collective is the term used to describe the group consciousness of the Borg civilization. Each Borg individual are linked to the collective by a sophisticated subspace network that insures each member are given con Stant supervision and guidance.
Your task is to help the Borg (yes, really) by developing a program which helps the Borg-estimate the minimal cost of s Canning a maze for the assimilation of aliens hiding in the maze, by moving in north, west, east, and south steps. The tricky thing is, the beginning of the search is conducted by a large group of over individuals. Whenever an alien was assimilated, or at the beginning of the search, the group could split in and more groups (but their Consciousness is still collective.). The cost of searching a maze are definied as the total distance covered by all the groups involved in the search together. That's, if the original group walks five steps, then splits into both groups each walking three steps, the total distance Is 11=5+3+3.InputOn the first line of input there are one integer, N <=, giving the number of test cases in the input. Each test case starts with a line containg the integers x, y such that 1 <= x, y <= 50. After this, y lines follow, each which x characters. For each character, a space "' stands for an open space, a hash mark ' # '" stands for an obstructing wall, the capital Letter "A" stand for a alien, and the capital letter "S" stands for the start of the search. The perimeter of the maze is always closed, i.e., and there is the no-to-get out from the coordinate of the ' S '. At most of aliens is present in the maze, and everyone is reachable.OutputFor every test case, output one line containing the minimal cost of a succesful search of the maze leaving no aliens alive .Sample Input
5##### #A #a### # a# #S ####### 7 7##### #AAA # # # # # # # # # # # # #AAA a##
Sample Output
811
SourceSvenskt m?sterskap I Programmering/norgesmesterskapet 2001 problem solving: Data a bit pit.
1#include <iostream>2#include <cstdio>3#include <queue>4#include <cstring>5 #defineINF 0x3f3f3f3f6 #definePII pair<int,int>7 using namespacestd;8 Const intMAXN = the;9 Const intMX =1000000;Ten intE[maxn][maxn],d[maxn][maxn],hs[maxn][maxn],ds[mx],n,m,tot; One BOOLDONE[MX]; A CharMP[MAXN][MAXN]; - BOOLIsIn (intXinty) { - returnX >=0&& x < n && y >=0&& y <m; the } - voidBFsintXinty) { -queue< PII >Q; - Q.push (Make_pair (x, y)); +memset (d,-1,sizeof(d)); -D[x][y] =0; + Static Const intdir[4][2] = {-1,0,1,0,0,-1,0,1}; A while(!Q.empty ()) { atPII now =Q.front (); - Q.pop (); - for(inti =0; I <4; ++i) { - inttx = Now.first + dir[i][0]; - intty = Now.second + dir[i][1]; - if(IsIn (Tx,ty) && mp[tx][ty]! ='#'&& D[tx][ty] = =-1) { inD[tx][ty] = D[now.first][now.second] +1; - Q.push (Make_pair (Tx,ty)); to if(Mp[tx][ty] = ='A'|| Mp[tx][ty] = ='S') { +E[hs[x][y]][hs[tx][ty]] =D[tx][ty]; - } the } * } $ }Panax Notoginseng } - the intPrim () { + intAns =0, cnt =0; A for(inti =0; i < tot; ++i) { theDone[i] =false; +Ds[i] =INF; - } $ds[0] =0; $ while(true) { - intMINV = Inf,idx =-1; - for(inti =0; i < tot; ++i) the if(Ds[i] < MINV &&!done[i]) MINV = Ds[idx =i]; - if(MINV = = INF | | idx = =-1) Break;WuyiAns + =MINV; theDONE[IDX] =true; - for(inti =1; i < tot; ++i) Wu if(!done[i] && e[idx][i] < ds[i]) ds[i] =E[idx][i]; - } About returnans; $ } - - intMain () { - intKase; Ascanf"%d",&Kase); + while(kase--) { thescanf"%d%d",&m,&n); -Gets (mp[0]); $ for(inti = tot =0; I < n; ++i) { the gets (Mp[i]); the for(intj =0; J < M; ++j) { the if(Mp[i][j] = ='A'|| MP[I][J] = ='S') theHS[I][J] = tot++; - } in } the for(inti =0; I < n; ++i) { the for(intj =0; J < M; ++j) About if(Mp[i][j] = ='A'|| MP[I][J] = ='S') BFS (i,j); the } theprintf"%d\n", Prim ()); the } + return 0; -}
View Code
POJ 3026 Borg Maze