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

操作符重载版代码,比较美观。

Posted by Codefresher at 2016-06-27 18:40:10 on Problem 2251
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x) memset(x,0,sizeof(x))
#define MMINF(x) memset(x,INF,sizeof(x))
typedef long long LL;
const double PI=acos(-1.0);
int l,n,m;
const int N=50;
struct info
{
	int x;
	int y;
	int z;
	int step;
};
info S,E;
info direct[6]={{1,0,0,1},{-1,0,0,1},{0,1,0,1},{0,-1,0,1},{0,0,1,1},{0,0,-1,1}};//·½ÏòÊý×é 
char pos[N][N][N];
int vis[N][N][N];
info operator+(const info &a,const info &b)
{
	info c;
	c.x=a.x+b.x;
	c.y=a.y+b.y;
	c.z=a.z+b.z;
	c.step=a.step+b.step;
	return c;
}
bool operator==(const info &a,const info &b)
{
	return (a.x==b.x&&a.y==b.y&&a.z==b.z);
}
inline bool check(const info &Q)
{
	if((pos[Q.x][Q.y][Q.z]=='.'||pos[Q.x][Q.y][Q.z]=='E')&&(!vis[Q.x][Q.y][Q.z]))//ÕâÀïÒª¼ÇµÃËãÉÏÖÕµã 
		return true;
	return false;
}
int main(void)
{
	int i,j,k;
	while (~scanf("%d%d%d",&l,&n,&m)&&(l||n||m))
	{
		S.step=0;
		MM(vis);
		MM(pos);
		for (i=0; i<l; i++)
		{
			for (j=0; j<n; j++)
			{
				for (k=0; k<m; k++)
				{
					cin>>pos[i][j][k];//cin»á×Ô¶¯Ê¡ÂÔ¿Õ¸ñ£¬ÕâÑù±È½Ï·½±ãÅжÏSºÍEµÄλÖᣠ
					if(pos[i][j][k]=='S')
					{
						S.x=i;
						S.y=j;
						S.z=k;
					}
					else if(pos[i][j][k]=='E')
					{
						E.x=i;
						E.y=j;
						E.z=k;
					}
				}
			}
		}
		int r=-1;
		queue<info>Q;
		Q.push(S);
		vis[S.x][S.y][S.z]=1;
		while (!Q.empty())
		{
			info now=Q.front();
			Q.pop();
			if(now==E)
			{
				r=now.step;
				break;
			}
			for (i=0; i<6; i++)
			{
				info v=now+direct[i];
				if(check(v))
				{
					vis[v.x][v.y][v.z]=1;
					Q.push(v);
				}
			}			
		}
		r==-1?puts("Trapped!"):printf("Escaped in %d minute(s).\n",r);
	}
	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