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

我就用从0123456开始搜索的,用的C语言程序如下

Posted by mingruoyuan at 2009-05-15 20:12:56 on Problem 3221
In Reply To:bfs tle-_- Posted by:qywyh_scut at 2007-04-28 17:06:13
#include<stdio.h>
#include<string.h>
#define MAX 5040
struct
{
	int deep;
	char s[8];
}a[MAX];
int head=0,tail=1;
char ch,ss[8];
int you(char str[])
{
	int i;
	for(i=0;i<tail;i++)
		if(strcmp(a[i].s,str)==0) return 1;
	return 0;
}
void work(int x,int y)
{
	strcpy(ss,a[head].s);
	ch=ss[x];
	ss[x]=ss[y];
	ss[y]=ch;
	if(you(ss)) return;
	strcpy(a[tail].s,ss);
	a[tail++].deep=a[head].deep+1;
}
int main()
{
	int i,j,n;
	char sss[8];
	strcpy(a[0].s,"0123456");
	a[0].deep=0;
	while(tail>head)
	{
		for(i=0;;i++)
			if(a[head].s[i]=='0') break;
		if(i==0)
		{
			work(0,2);
			work(0,4);
			work(0,6);
		}
		if(i==1) {work(1,6);work(1,2);}
		if(i==3||i==5) {work(i,i+1);work(i,i-1);}
		if(i==2||i==4) {work(0,i);work(i,i+1);work(i,i-1);}
		if(i==6) {work(1,6);work(5,6);work(0,6);}
		head++;
	}
	scanf("%d",&n);
	while(n--)
	{
		int flag=1;
		scanf("%s",sss);
		for(i=0;i<tail;i++)
			if(strcmp(a[i].s,sss)==0)
			{
				printf("%d\n",a[i].deep);
				flag=0;
				break;
			}
		if(flag) printf("-1\n");
	}
	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