Cod sursa(job #12217)

Utilizator mastermageSchneider Stefan mastermage Data 3 februarie 2007 12:02:19
Problema A+B Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.09 kb
#include <stdio.h>
#include <queue>
using namespace std;

#define maxN 205
#define maxP 105

int n,m,k,p,mp[maxN][maxN];
int stl,stc,enl,enc;

int dinf[maxN][maxN],dinh[maxN][maxN];
int hisf[maxN][maxN],hish[maxN][maxN];
int nexf[maxN][maxN],nexh[maxN][maxN];

int fx[]={0 ,1 ,1 ,1 ,0 ,-1,-1,-1};
int fy[]={1 ,1 ,0 ,-1,-1,-1, 0,1 };

int hx[]={1 ,2 ,-1,2 ,1 ,-2,-1,-2};
int hy[]={2 ,1 ,2 ,-1,-2,1 ,-2,-1};

struct po{
	po(){}
	po(const int&x,const int&y){l=x,c=y;}
	int l,c;
};
queue<po> qf,qh;
po hans[maxP];
po stk[10000];

int dis[maxP][maxP];


void fill(int ha){
	memset(dinh,-1,sizeof dinh);memset(hish,0,sizeof hish);memset(nexh,0,sizeof nexh);
	while(!qh.empty())qh.pop();
	dinh[hans[ha].l][hans[ha].c]=0;
	qh.push(po(hans[ha].l, hans[ha].c));
	

	while(!qh.empty()){
		po cur=qh.front();qh.pop();
		int cl=cur.l,cc=cur.c,cd=dinh[cl][cc];;
		nexh[cl][cc]=0;
		
		if(cd==k)continue;
		
		for(int i=0; i<8; i++){
			int nl=cl+hx[i],nc=cc+hy[i];
			if(nl>=0 && nc>=0 && nl<n && nc<m){
				if(cd+1<dinh[nl][nc] || dinh[nl][nc]==-1){
					if(!nexh[nl][nc])qh.push(po(nl,nc));
					dinh[nl][nc]=cd+1;
					nexh[nl][nc]=1;hish[nl][nc]=i;
				}
			}
			
		}
		
	}
	
	
}

void inputFunc(){
	FILE*fi=fopen("arthur.in","r");
	fscanf(fi,"%d %d %d %d",&n,&m,&k,&p);
	fscanf(fi,"%d %d",&stl,&stc);stl--;stc--;
	fscanf(fi,"%d %d",&enl,&enc);enl--;enc--;
	memset(mp,-1,sizeof mp);
	mp[enl][enc]=0;hans[0]=po(enl,enc);
	for(int i=0;i<p;i++){
		int a,b;fscanf(fi,"%d %d",&a,&b);a--;b--;
		mp[a][b]=i+1;hans[i+1]=po(a,b);
	}
	
	fclose(fi);
}

void outputFunc(){
	FILE*fi=fopen("arthur.out","w");
	fprintf(fi,"%d\n",dinf[enl][enc]);
	
	int k=0;stk[0]=po(enl,enc);
	while(1){
		int cl=stk[k].l, cc=stk[k].c, w=hisf[cl][cc];if(cl==stl && cc==stc)break;
		if(w>=0){
			k++; stk[k]=po(cl - fx[w],cc - fy[w]);
		}else{
			w=-(w+1);
			fill(w);
			int hal=hans[w].l,hac=hans[w].c;
			
			while(1){
				cl=stk[k].l, cc=stk[k].c, w=hish[cl][cc];if(cl==hal && cc==hac)break;
				k++; stk[k]=po(cl - hx[w],cc - hy[w]);
			}
			
		}
		
	}
	
	while(k>=0){
		fprintf(fi,"%d %d\n",stk[k].l+1,stk[k].c+1);
		k--;
	}
	fclose(fi);
}

int main(){
	inputFunc();
	
	for(int i=1;i<=p;i++){
		fill(i);
		for(int j=0;j<=p;j++){
			dis[i][j]=dinh[hans[j].l][hans[j].c];
		}
	}
	
	

	qf.push(po(stl,stc));
	
	while(!qf.empty()){
		po cur=qf.front();qf.pop();
		int cl=cur.l,cc=cur.c,cd=dinf[cl][cc];
		nexf[cl][cc]=0;
		
		for(int i=0; i<8; i++){
			int nl=cl+fx[i],nc=cc+fy[i];
			if(nl>=0 && nc>=0 && nl<n && nc<m){
				if(cd+1<dinf[nl][nc] || !dinf[nl][nc]){
					if(!nexf[nl][nc])qf.push(po(nl,nc));
					dinf[nl][nc]=cd+1;
					nexf[nl][nc]=1;hisf[nl][nc]=i;
				}
			}
		}
		
		if(mp[cl][cc]>0){
			int curh=mp[cl][cc];
			for(int i=0;i<=p;i++)if(dis[curh][i]>0){
				int nl=hans[i].l,nc=hans[i].c, nextdis=cd+dis[curh][i];
				if(nextdis<dinf[nl][nc] || !dinf[nl][nc]){
					if(!nexf[nl][nc])qf.push(po(nl,nc));
					dinf[nl][nc]=nextdis;
					nexf[nl][nc]=1;hisf[nl][nc]=-(curh+1);
				}
			}
			
		}
		
	}

	
	outputFunc();
	return 0;
}