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:今天又改了一下这道题,改成0ms了

Posted by TSERROF at 2012-10-29 23:08:32 on Problem 3083
In Reply To:做了1个多小时,1A啊啊,刚开始学搜索,拙代码供大家看看 Posted by:TSERROF at 2012-09-29 15:36:36
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
char map[45][45];
int dir[4][2]={{0,-1},{-1,0},{0,1},{1,0}};
bool lDFS(int i,int j,int orient,int step)
{
	if(map[i][j]=='E'){ cout<<step+1<<" ";return true;}
	if(orient<0)orient+=4;
	for(int k=orient;k<orient+4;++k)
	{
		int di=i+dir[k%4][0];
		int dj=j+dir[k%4][1];
		if(map[di][dj]!='#') if(lDFS(di,dj,(k-1)%4,step+1))return true;
	}
	return false;
}
bool rDFS(int i,int j,int orient,int step)
{
	if(map[i][j]=='E')
	{
		cout<<step+1<<" ";
		return true;
	}
	for(int k=orient+4;k>orient;--k)
	{
		int di=i+dir[k%4][0];
		int dj=j+dir[k%4][1];
		if(map[di][dj]!='#')if(rDFS(di,dj,(k+1)%4,step+1))return true;
	}
	return 0;
}
struct C
{
	int x,y,step;
};
int BFS(int i,int j)
{
	queue<C>c;
	C tempc;
	tempc.x=i,tempc.y=j,tempc.step=1;
	c.push(tempc);
	while(!c.empty())
	{
		tempc=c.front();
		c.pop();
		if(map[tempc.x][tempc.y]=='E')return tempc.step;
		map[tempc.x][tempc.y]='#';
		for(int i=0;i<4;++i)
		{
			C t;
			t.x=tempc.x+dir[i][0],t.y=tempc.y+dir[i][1],t.step=tempc.step+1;
			if(map[t.x][t.y]!='#')
			{ 
				c.push(t);
				if(map[t.x][t.y]!='E')map[t.x][t.y]='#';
			}
		}
	}
	return -1;
}
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		int row,col,sj,si;
		cin>>col>>row;
		memset(map,'#',sizeof(map));
		for(int i=1;i<=row;++i)
			for(int j=1;j<=col;++j)
			{
				cin>>map[i][j];
				if(map[i][j]=='S')si=i,sj=j;
			}
		lDFS(si,sj,0,0);
		rDFS(si,sj,0,0);
		cout<<BFS(si,sj)<<endl;
	}
	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