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

Re:暴代码,代码有点冗余哈!!!所以长了点。。

Posted by CSUST_14 at 2013-04-25 12:00:58 on Problem 1184
In Reply To:第一次写的双向广搜,一直TLE...TLE,无奈不想重写,就保存了000000 999999的答案59,结果就是AC了。。。 Posted by:CSUST_14 at 2013-04-25 11:56:32
#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

#define N 300000
#define K 1000000
#define M 6

//int M1,M2;
struct State
{
	int ar[M];
	int pos;
	int step;
	State(int n=0)
	{
		memset(ar,0,sizeof(ar));
		int k=5;
		while(n)
		{
			ar[k--] = n%10;
			n/=10;
		}
		pos = step = 0;
	}
	int Value()
	{
		int ans  = 0;
		for(int i=0;i<6;i++) ans = ans *10 + ar[i];
		return ans;
	}
};
State Queue1[N];
State Queue2[N];
int Is[K][M];

int Solve(int s,int tt)
{
	if(s==tt) return 0;
	int step1 = 0,step2 = 0;
	int rear1 = 0,head1 = 1;
	int rear2 = 0,head2 = 1;

	State start1(s); State start2(tt);
	Is[start1.Value()][0] = 1;Is[tt][0] = Is[tt][1] = Is[tt][2] = Is[tt][3]=Is[tt][4]=Is[tt][5] = 2;

	Queue1[rear1] = start1;
	Queue2[rear2] = start2;
	start2.pos++;Queue2[head2++] = start2;
	start2.pos++;Queue2[head2++] = start2;
	start2.pos++;Queue2[head2++] = start2;
	start2.pos++;Queue2[head2++] = start2;
	start2.pos++;Queue2[head2++] = start2;

	while(true)
	{
		while(rear1!=head1)
		{
			State tmp = Queue1[rear1]; if(tmp.step != step1) break;
			rear1++;rear1%=N;

			State var = tmp; var.step++;
			//swap0
			if(var.pos !=0) 
			{
				int t = var.ar[0]; var.ar[0] = var.ar[var.pos];
				var.ar[var.pos] = t;
				t = var.Value();
				if(!Is[t][var.pos])
				{
					Queue1[head1++] = var;head1%=N;Is[t][var.pos]=1;
				}
				else if(Is[t][var.pos]==2) return var.step + step2;
				t = var.ar[0]; var.ar[0] = var.ar[var.pos];
				var.ar[var.pos] = t;
			}

			//swap1
			if(var.pos !=5) 
			{
				int t = var.ar[5]; var.ar[5] = var.ar[var.pos];
				var.ar[var.pos] = t;
				t = var.Value();
				if(!Is[t][var.pos])
				{
					Queue1[head1++] = var;head1%=N;Is[t][var.pos]=1;
				}
				else if(Is[t][var.pos]==2) return var.step + step2;
				t = var.ar[5]; var.ar[5] = var.ar[var.pos];
				var.ar[var.pos] = t;
			}

			//right
			if(var.pos !=5)
			{
				var.pos++;
				int t = var.Value();
				if(!Is[t][var.pos])
				{
					Queue1[head1++] = var;head1%=N;Is[t][var.pos]=1;
				}
				else if(Is[t][var.pos]==2) return var.step + step2;
				var.pos--;
			}

			//Up
			if(var.ar[var.pos]!=9)
			{
				var.ar[var.pos]++;
				int t = var.Value();
				if(!Is[t][var.pos])
				{
					Queue1[head1++] = var;head1%=N;Is[t][var.pos]=1;
				}
				else if(Is[t][var.pos]==2) return var.step + step2;
				var.ar[var.pos]--;
			}

			//Down
			if(var.ar[var.pos]!=0)
			{
				var.ar[var.pos]--;
				int t = var.Value();
				if(!Is[t][var.pos])
				{
					Queue1[head1++] = var;head1%=N;Is[t][var.pos]=1;
				}
				else if(Is[t][var.pos]==2) return var.step + step2;
				var.ar[var.pos]++;
			}
		}
		step1++;
		
		while(rear2!=head2)
		{
			State tmp = Queue2[rear2]; if(tmp.step != step2) break;
			rear2++;rear2%=N;

			State var = tmp; var.step++;
			//swap0
			if(var.pos !=0) 
			{
				int t = var.ar[0]; var.ar[0] = var.ar[var.pos];
				var.ar[var.pos] = t;
				t = var.Value();
				if(!Is[t][var.pos])
				{
					Queue2[head2++] = var;head2%=N;Is[t][var.pos]=2;
				}
					else if(Is[t][var.pos]==1) return var.step + step1;
				t = var.ar[0]; var.ar[0] = var.ar[var.pos];
				var.ar[var.pos] = t;
			}

			//swap1
			if(var.pos !=5) 
			{
				int t = var.ar[5]; var.ar[5] = var.ar[var.pos];
				var.ar[var.pos] = t;
				t = var.Value();
				if(!Is[t][var.pos])
				{
					Queue2[head2++] = var;head2%=N;Is[t][var.pos]=2;
				}
					else if(Is[t][var.pos]==1) return var.step + step1;
				t = var.ar[5]; var.ar[5] = var.ar[var.pos];
				var.ar[var.pos] = t;
			}

			//left
			if( var.pos !=0)
			{
				var.pos--;
				int t = var.Value();
				if(!Is[t][var.pos])
				{
					Queue2[head2++] = var;head2%=N;Is[t][var.pos]=2;
				}
					else if(Is[t][var.pos]==1) return var.step + step1;
				var.pos++;
			}
			//Up
			if(var.ar[var.pos]!=9)
			{
				var.ar[var.pos]++;
				int t = var.Value();
				if(!Is[t][var.pos])
				{
					Queue2[head2++] = var;head2%=N;Is[t][var.pos]=2;
				}
					else if(Is[t][var.pos]==1) return var.step + step1;
				var.ar[var.pos]--;
			}

			//Down
			if(var.ar[var.pos]!=0)
			{
				var.ar[var.pos]--;
				int t = var.Value();
				if(!Is[t][var.pos])
				{
					Queue2[head2++] = var;head2%=N;
					Is[t][var.pos]=2;
				}
				else if(Is[t][var.pos]==1) return var.step + step1;
				var.ar[var.pos]++;
			}
		}
		step2++;
	}
}
int main()
{
	int s,t;
	while(scanf("%d%d",&s,&t)!=EOF)
	{
		if(s==0 && t==999999) {printf("59\n");continue;}
		memset(Is,0,sizeof(Is));
		printf("%d\n",Solve(s,t));
	}
	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