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

双向广搜+康托展开 代码-47ms

Posted by 15211160230 at 2016-12-15 15:01:25 on Problem 1077
#include <stdio.h>
#include <memory.h>
#include <queue>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;
typedef struct{
	int pos;
	char data[10];
	int hash;
}Node;
const int fac[]={1,1,2,6,24,120,720,5040,40320,362880};
const int dir[4][2]={0,1,0,-1,1,0,-1,0};
const int MAXN=363000;
const char str[5]="rldu";
const char revstr[5]="lrud";
short vis[MAXN];
int pre[MAXN];
char path[MAXN];
vector<char>road1,road2;
vector<char>::iterator it;
int num1,num2,middle;
int GetCantorHash(const Node &p)
{   int i,j,num=0,sum=0;
    for(i=0;i<9;++i)
    {    num=0;
         for(j=i+1;j<9;++j)
            if(p.data[j]<p.data[i])	num++;
         sum+=(num*fac[9-1-i]);
    }
    return sum;
}
queue<Node>q1,q2;
void Initial()
{	while(!q1.empty())	q1.pop();
	while(!q2.empty())	q2.pop();
	memset(vis,0,sizeof(vis));
	memset(pre,-1,sizeof(pre));
	num1=num2=0;
	road1.clear(),road2.clear();
}
void RemmberPath1(int hash)
{	while(pre[hash]!=-1)
	{	road1.push_back(path[hash]);
		hash=pre[hash];
	}
	hash=middle;
	while(pre[hash]!=-1)
	{	road2.push_back(path[hash]);
		hash=pre[hash];
	}
	reverse(road1.begin(),road1.end());
}
void RemmberPath2(int hash)
{	while(pre[hash]!=-1)
	{	road2.push_back(path[hash]);
		hash=pre[hash];
	}
	hash=middle;
	while(pre[hash]!=-1)
	{	road1.push_back(path[hash]);
		hash=pre[hash];
	}
	reverse(road1.begin(),road1.end());
}
bool EightDFS(bool flag)
{	Node now,temp;
	int x,y,i;
	if(flag)	now=q1.front(),q1.pop();
	else		now=q2.front(),q2.pop();
	for(i=0;i<4;++i)
	{	x=now.pos/3+dir[i][0];
		y=now.pos%3+dir[i][1];
		if(x<0||x>2||y<0||y>2)	continue;
		temp=now;
		temp.pos=x*3+y;
		swap(temp.data[now.pos],temp.data[temp.pos]);
		temp.hash=GetCantorHash(temp);
		if(flag)
		{	if(vis[temp.hash]==0)
			{	vis[temp.hash]=1;
				pre[temp.hash]=now.hash;
				path[temp.hash]=str[i];
				q1.push(temp),num1++;
			}
			else if(vis[temp.hash]==2)
			{	middle=temp.hash;
				RemmberPath1(now.hash);
				path[middle]=str[i];
				return 1;
			}
		}
		else
		{	if(vis[temp.hash]==0)
			{	vis[temp.hash]=2;
				pre[temp.hash]=now.hash;
				path[temp.hash]=revstr[i];
				q2.push(temp),num2++;
			}
			else if(vis[temp.hash]==1)
			{	middle=temp.hash;
				RemmberPath2(now.hash);
				path[middle]=revstr[i];
				return 1;
			}
		}
	}
	return 0;	
}
bool BothDFS(char *s,int pos)
{	Node temp;
	Initial();
	strcpy(temp.data,s);
	temp.pos=pos;
	temp.hash=GetCantorHash(temp);
	vis[temp.hash]=1,num1++;
	q1.push(temp);
	
	strcpy(temp.data,"123456780");
	temp.pos=8;
	temp.hash=GetCantorHash(temp);
	vis[temp.hash]=2,num2++;
	q2.push(temp);
	while(!q1.empty()||!q2.empty())
	{	if(num1<=num2)
		{	if(EightDFS(1))		
				return 1;
		}	
		else if(EightDFS(0))	return 1;
	}
	return 0;
}
void PrintOut()
{	for(it=road1.begin();it!=road1.end();it++)
		printf("%c",*it);
	printf("%c",path[middle]);
	for(it=road2.begin();it!=road2.end();it++)
		printf("%c",*it);
	puts("");
}
int main()
{	int i,len,pos;
	char s[101],t[10];
	while(gets(s)!='\0')
	{	len=0;
		for(i=0;s[i]!='\0';++i)
		{	if(s[i]=='x')
			{	pos=len;
				t[len++]='0';				
				continue;
			}
			if('1'<=s[i]&&s[i]<='8')
				t[len++]=s[i];
		}
		t[len]='\0';
		if(BothDFS(t,pos))		PrintOut();
		else					puts("unsolvable");
	}
	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