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 2641145181 at 2018-08-23 17:25:54 on Problem 1101
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
bool map[180][180];
int n,m;
int stx,sty,lsx,lsy;
int cas=0;
int ans;
int sum[4][180][180];
int t[4][2]={{-1,0},{1,0},{0,-1},{0,1}};

struct st
{
	int x,y;
	int f,n;
	friend bool operator < (st a,st b)
	{
		return a.n>b.n;
	}
};

priority_queue <st>q;

void bfs()
{
	while(!q.empty()) q.pop();
	int x,y,ti;
	memset(sum,0xf,sizeof(sum));
	st tmp,tp;
	tmp.x=stx;tmp.y=sty;tmp.n=1;
	tmp.f=0;
	q.push(tmp);
	tmp.f=1;
	q.push(tmp);
	tmp.f=2;
	q.push(tmp);
	tmp.f=3;
	q.push(tmp);
	sum[0][stx][sty]=sum[1][stx][sty]=sum[2][stx][sty]=sum[3][stx][sty]=1;
	while(!q.empty())
	{
		tmp=q.top();q.pop();
		if(tmp.x==lsx&&tmp.y==lsy)
		{
			ans=tmp.n;
			return;
		}
		if(tmp.n>sum[tmp.f][tmp.x][tmp.y]) continue;
		for(int i=0;i<4;i++)
		{
			x=tmp.x+t[i][0];
			y=tmp.y+t[i][1];
			if(i==tmp.f) ti=tmp.n;
			else ti=tmp.n+1;
			while(x>=0&&x<=n+1&&y>=0&&y<=m+1&&(!map[x][y]))
			{
				if(ti<sum[i][x][y])
				{
					sum[i][x][y]=ti;
					tp.x=x;tp.y=y;
					tp.f=i;tp.n=ti;
					q.push(tp);
				}
				x+=t[i][0];
				y+=t[i][1];
				if(map[x][y]) break;
			}
			if(x==lsx&&y==lsy)
			{
				if(ti<sum[i][x][y])
				{
					sum[i][x][y]=ti;
					tp.x=x;tp.y=y;
					tp.f=i;tp.n=ti;
					q.push(tp);
				}
			}
		}
	}
}

void init()
{
	memset(map,0,sizeof(map));
	char c;
	int j,op=0;
	while(scanf("%c",&c)==1)
	{
		if(c=='\n')break;
	}
	for(int i=1;i<=n;i++)
	{
		j=0;
		while(scanf("%c",&c)==1)
		{
			j++;
			if(c=='X') map[i][j]=1;
			if(c=='\n') break;
		}
	}
	printf("Board #%d:\n",++cas);
	while(scanf("%d%d%d%d",&sty,&stx,&lsy,&lsx)==4&&stx)
	{
		printf("Pair %d:",++op);
		ans=0;
		bfs();
		if(ans) printf(" %d segments.\n",ans);
		else printf(" impossible.\n");
	}
}

int main()
{
	//freopen("poj.in","r",stdin);
	while(scanf("%d%d",&m,&n)==2&&n&&m)
	{
		init();
		printf("\n");
	}
	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