Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

蹭着时限过的感觉真不好

Posted by fanhqme at 2009-07-13 15:12:01 on Problem 3732
有人也想感受这种不爽的感觉吗?

int R,C,cc[CMax],cl;
char map[NMax][NMax];
int way[WMax][3],Dep;
char *S,*T,color,color2;
void floodfill(int x,int y){
	int x1[WMax],y1[WMax],top;top=1;x1[0]=x;y1[0]=y;
	while (top){top--;x=x1[top];y=y1[top];
		T[(x<<2)+y]=color2;
		if (x && S[((x-1)<<2)+y]==color && T[((x-1)<<2)+y]!=color2)
			x1[top]=x-1,y1[top]=y,top++;
		if (x+1<R && S[((x+1)<<2)+y]==color && T[((x+1)<<2)+y]!=color2)
			x1[top]=x+1,y1[top]=y,top++;
		if (y && S[((x)<<2)+y-1]==color && T[((x)<<2)+y-1]!=color2)
			x1[top]=x,y1[top]=y-1,top++;
		if (y+1<C && S[((x)<<2)+y+1]==color && T[((x)<<2)+y+1]!=color2)
			x1[top]=x,y1[top]=y+1,top++;
	}
}
int dfs(int id){
	char bak[NMax][NMax],vi[NMax][NMax],cc2[CMax];
	int q;
	if (id==Dep){
		for (int i=0;i<R;i++)for (int j=0;j<C;j++)if (map[i][j])return 0;
		return 1;
	}
	memset(vi,0,sizeof(vi));memset(cc2,0,sizeof(cc2));
	memcpy(bak,map,sizeof(map));
	q=0;
	for (int i=0;i<R;i++)for (int j=0;j<C;j++){
		if (!cc2[map[i][j]])q++;
		cc2[map[i][j]]++;
	}
	if (q+id-(cc2[0]&&1)>Dep)return 0;
	S=(char*)map;
	for (int i=0;i<R;i++)for (int j=0;j<C;j++)if (!vi[i][j]){
		T=(char*)vi;color=map[i][j];color2=1;
		floodfill(i,j);
		for (int k=0;k<cl;k++)if (!k || cc2[cc[k]]>=2){
			way[id][0]=i;way[id][1]=j;way[id][2]=cc[k];
			T=(char*)map;color=map[i][j];color2=cc[k];
			floodfill(i,j);
			if (dfs(id+1))return 1;
			memcpy(map,bak,sizeof(map));
		}
	}
	return 0;
}
int main()
{
	char bak[NMax][NMax];
	int x;
	while (scanf("%d %d",&R,&C)!=EOF){
		for (int i=0;i<CMax;i++)cc[i]=0;
		for (int i=0;i<R;i++)for (int j=0;j<C;j++){
			scanf("%d",&x);
			map[i][j]=x;
			cc[x]++;
		}
		memcpy(bak,map,sizeof(map));
		cc[0]=0;cl=1;
		for (int i=1;i<CMax;i++)if (cc[i]>=2)cc[cl++]=i;
		for (Dep=0;Dep<=R*C;Dep++){
			if (dfs(0))break;
		}
		printf("%d\n",Dep);
		memcpy(map,bak,sizeof(map));
		for (int i=0;i<Dep;i++){
			printf("%d %d %d\n",way[i][0]+1,way[i][1]+1,way[i][2]);
		}
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator