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 a654889339 at 2013-02-20 16:17:53 on Problem 1101
#include "iostream"
#include "queue"
using namespace std;

int h, w, step[1000][1000], sx, sy, ex, ey;
int direct[1000][1000];
bool map[1000][1000],inq[1000][1000];
int a[5][2]={-1,-1,0,-1,0,1,-1,0,1,0};  //上下左右 
queue<int> qx;
queue<int> qy;
void hua(){
	for(int i=0;i<=h+1;i++){
		for(int j=0;j<=1+w;j++)
			printf("%d",direct[i][j]);
		cout<<endl;
	}
}
void bfs(){
	while(!qx.empty()){
		int x=qx.front(), y=qy.front();
		qx.pop();		qy.pop();	inq[y][x]=0;
		for(int i=1;i<=4;i++)
		{
			int nowx=x+a[i][0],nowy=y+a[i][1];
	//		printf("nowx=%d  nowy=%d  终点=%d\n",nowx,nowy,step[ey][ex]);

			if(nowx<=w+1 && nowx>=0 && nowy<=h+1  && nowy>=0)
				if( (nowx==ex && nowy==ey) || map[nowy][nowx]==0)
			{
				int same=1;	if(i==direct[y][x])same--;  //如果同向  same=0  i要和xy同向 不是now
				if(same+step[y][x]<step[nowy][nowx])
				{
					step[nowy][nowx]=same+step[y][x];
					direct[nowy][nowx]=i;
		//			hua();
					if(inq[nowy][nowx]==0)
						qx.push(nowx),qy.push(nowy),inq[nowy][nowx]=1;
				}
			}
		}
	}
	return ;
}
int main(){
	int n=1;
	while( ~scanf("%d %d",&w,&h), h||w ){
		
		memset(map,0,sizeof(map));
		for(int i=1;i<=h;i++){
			getchar();
			for(int j=1;j<=w;j++)
			{
				char u=getchar();
				if(u=='X')map[i][j]=1;
			}
		}
		/*		for( i=0;i<=h+1;i++){
		for( int j=0;j<=w+1;j++)
		printf("%d",map[i][j]);
		cout<<endl;
		}
		*/
		printf("Board #%d:\n",n++);
		int m=1;
		while( ~scanf("%d %d %d %d", &sx, &sy, &ex, &ey), sx||sy||ex||ey)
		{
			
			memset(step,100,sizeof(step));
			memset(direct,0,sizeof(direct));	memset(inq,0,sizeof(inq));
			
			
			while(!qx.empty())qx.pop();	while(!qy.empty())qy.pop();	
			inq[sy][sx]=1;	step[sy][sx]=0;		qx.push(sx);	qy.push(sy);
			//	for(int i=1;i<=4;i++)
			//		step[sy+a[i][0]][sx+a[i][1]]=1;
			
			bfs();
			if(step[ey][ex]<99999)
				printf("Pair %d: %d segments.\n",m++,step[ey][ex]);
			else printf("Pair %d: impossible.\n",m++);
		}
		cout<<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