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

用朴素的方法也能过,用标准库的queue也能过滴

Posted by tcet030840zxp at 2012-03-11 23:20:42 on Problem 1324
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
const int MAXN=21;
int map[MAXN][MAXN];
int snake[MAXN][MAXN][20000];
const int dir1[4][2]={-1,0,0,1,1,0,0,-1};
const int dir2[4]={0,3,2,1};
struct Node{
	int x,y,c,state;
	bool operator < (Node other)const{
		return c+x+y>other.c+other.x+other.y;
	}
};
queue<Node> q;
int n,m,k;
int code(int a[])
{
	int state=0;
	for(int i=0;i<k-1;i++)
		state=state*4+a[i];
	return state;
}
void encode(int state,int s[])
{
	for(int i=k-2;i>=0;i--)
	{
		s[i]=state%4;
		state/=4;
	}
}
int bfs()
{
	Node now;
	int state[8];
	int i,j,x,y,tmpx,tmpy,c,s;
	while(!q.empty())
	{
		tmpx=x=q.front().x;tmpy=y=q.front().y;
		c=q.front().c;s=q.front().state;
		q.pop();
		if(x==1&&y==1)return c;
		encode(s,state);
		for(j=0;j<k-1;j++)
		{
			switch(state[j])
			{
			case 0:{map[x+1][y]=1;x+=1;break;}
			case 1:{map[x][y+1]=1;y+=1;break;}
			case 2:{map[x-1][y]=1;x-=1;break;}
			case 3:{map[x][y-1]=1;y-=1;break;}
			}
		}
		for(j=k-2;j>=1;j--)state[j]=state[j-1];
		x=tmpx;y=tmpy;
		for(i=0;i<4;i++)
		{
			now.x=x+dir1[i][0];
			now.y=y+dir1[i][1];
			now.c=c+1;
			if(now.x<1||now.x>n||now.y<1||now.y>m)continue;
	        if(map[now.x][now.y]==1)continue;
			
			state[0]=dir2[i];
			now.state=code(state);

			if(snake[now.x][now.y][now.state]==-1||now.c<snake[now.x][now.y][now.state])
			{
				snake[now.x][now.y][now.state]=now.c;
				q.push(now);
			}
		}
		encode(s,state);
		for(j=0;j<k-1;j++)
		{
			switch(state[j])
			{
			case 0:{map[x+1][y]=0;x+=1;break;}
			case 1:{map[x][y+1]=0;y+=1;break;}
			case 2:{map[x-1][y]=0;x-=1;break;}
			case 3:{map[x][y-1]=0;y-=1;break;}
			}
		}
	}
	return -1;
}
int main()//0下1右2上3左
{
	int i,j,stone,x,y,t=1;
	Node init[8], now;
	int first[8];
	while(scanf("%d%d%d",&n,&m,&k)==3,n&&m&&k)
	{
		memset(snake,-1,sizeof(snake));
		memset(map,0,sizeof(map));
		for(i=0;i<k;i++)scanf("%d%d",&init[i].x,&init[i].y);
		scanf("%d",&stone);
		for(i=0;i<stone;i++)
		{
			scanf("%d%d",&x,&y);
			map[x][y]=1;
		}
		for(i=1;i<k;i++)
		{
			if(init[i].x==init[i-1].x&&init[i].y==init[i-1].y+1)
				first[i-1]=1;
			else if(init[i].x==init[i-1].x&&init[i].y==init[i-1].y-1)
				first[i-1]=3;
			else if(init[i].x==init[i-1].x+1&&init[i].y==init[i-1].y)
				first[i-1]=0;
			else if(init[i].x==init[i-1].x-1&&init[i].y==init[i-1].y)
				first[i-1]=2;
		}
		int fstate=code(first);
		while(!q.empty())q.pop();
		now.x=init[0].x;now.y=init[0].y;now.c=0;now.state=fstate;
		snake[now.x][now.y][fstate]=0;
		q.push(now);
		printf("Case %d: %d\n",t++,bfs());
	}
	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