答案:答:vara:array[1..10000] of integer;b:array[1..10000] of longint;i,j,k,n,m,open,closed,ans:longint;procedure swap(var x,y:integer);var t:integer;begint:=x;x:=y;y:=t;end;procedure qsort(s,t:longint);var i,j,mid:longint;begini:=s;j:=t;mid:=a[(s t) div 2];while i<=j do begin while a[i]mid do dec(j); if i<=j then begin swap(a[i],a[j]); inc(i);dec(j); end; end;if is then qsort(s,j);end;beginreadln(n,m);for i:=1 to n do read(a[i]);qsort(1,n);closed:=1;open:=1;fillchar(b,sizeof(b),0);k:=n;while k>m do begin k:=k div m k mod m; end; for i:=1 to k do b[1]:=b[1] a[i];k:=k 1;if k<=n thenrepeat inc(closed); for i:=1 to m do begin if ((a[k]<=b[open])or(open=closed))and(k<=n) then begin inc(b[closed],a[k]); inc(k); end else if ((a[k]>b[open])or(k>n))and(openn);ans:=0;for i:=1 to closed do inc(ans,b[i]);writeln(ans);end.for(i=0,j=0;j<=cols 1;j ) maze[i][j]='1';for(i=rows 1,j=0;j<=cols 1;j ) maze[i][j]='1';*startpos=start;*endpos=end;return OK;}int canPass(Position curpos){if(maze[curpos.x][curpos.y]=='0')return TRUE;return FALSE;}void markPos(Position curpos,MarkTag tag){ //为已走过的位置标记switch(tag){case YES: maze[curpos.x][curpos.y]='.'; break; //路径标记case NO: maze[curpos.x][curpos.y]='#'; break; //死胡同标记}}Position nextPos(Position curpos,Direction dir){//根据当前的位置坐标和下一步要探索的方向dir求下一步要走的位置坐标Position nextpos;switch(dir){case RIGHT: nextpos.x=curpos.x; nextpos.y=curpos.y 1; break;case DOWN: nextpos.x=curpos.x 1; nextpos.y=curpos.y; break;case LEFT: nextpos.x=curpos.x; nextpos.y=curpos.y-1; break;case UP: nextpos.x=curpos.x-1; nextpos.y=curpos.y; break;}return nextpos;}Direction nextDir(Direction dir){switch(dir){ //按照RIGHT DOWN LEFT UP的次序进行路径探索case RIGHT: return DOWN;case DOWN: return LEFT;case LEFT: return UP;}}/*若迷宫中存在从入口start到出口end的通道,则求得一条存放在栈S中,并返回TRUE,若不存在则返回FALSE*/int Solve(Stack *S,Position start,Position end){Position curpos;SElemType e;int curstep=1;if(InitStack(S)==ERROR)return FALSE;curpos=start;do{if(canPass(curpos)){ //当前位置可以通过markPos(curpos,YES); //留下足迹e.order=curstep;e.seat=curpos;e.di=RIGHT;Push(S,e);if(curpos.x==end.x && curpos.y=end.y)return TRUE; //找到从入口到出口的通道curpos=nextPos(curpos,RIGHT);curstep ;}else{if(!Empty(*S)){ //当前位置不能通过if(Pos(S,&e)==ERROR)return FALSE;while(e.di==UP && !Empty(*S)){//4个方向都找不到通路,则回溯curpos=e.seat;markPos(curpos,NO);if(Pop(S,&e)==ERROR)return FALSE;}if(e.di!=UP){ //4个方向还没有探索完e.di=nextDir(e.di);Push(S,e); //换下一个方向探索curpos=nextPos(e.seat,e.di);}}}while(!Empty(*S));return FALSE;}void main(void){Position startPos,endPos;Stack path;SElemType e;char *fname="in.txt";if(createMaze(fname,&startPos,&endPos)==ERROR) return;Solve(&path,startPos,endPos);while(!Empty(path)){ //输出出口到入口的路径Pop(&path,&e);printf("(%d,%d)\n",e.seat.x,e.seat.y);}}