HDOJ题目3309 Roll The Cube(BFS) -电脑资料

电脑资料 时间:2019-01-01 我要投稿
【www.unjs.com - 电脑资料】

   

Roll The Cube

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

    Total Submission(s): 502 Accepted Submission(s): 181

    Problem Description This is a simple game.The goal of the game is to roll two balls to two holes each.

    'B' -- ball

    'H' -- hole

    '.' -- land

    '*' -- wall

    Remember when a ball rolls into a hole, they(the ball and the hole) disappeared, that is , 'H' + 'B' = '.'.

    Now you are controlling two balls at the same time.Up, down , left , right --- once one of these keys is pressed, balls exist roll to that direction, for example , you pressed up , two balls both roll up.

    A ball will stay where it is if its next point is a wall, and balls can't be overlap.

    Your code should give the minimun times you press the keys to achieve the goal.

    Input First there's an integer T(T<=100) indicating the case number.

    Then T blocks , each block has two integers n , m (n , m <= 22) indicating size of the map.

    Then n lines each with m characters.

    There'll always be two balls(B) and two holes(H) in a map.

    The boundary of the map is always walls(*).

    Output The minimum times you press to achieve the goal.

    Tell me "Sorry , sir , my poor program fails to get an answer." if you can never achieve the goal.

    Sample Input

46 3****B**B**H**H****4 4*****BB**HH*****4 4*****BH**HB*****5 6*******.BB***.H*H**..*.*******

    Sample Output

312Sorry , sir , my poor program fails to get an answer.

    Author MadFroG

    Source HDOJ Monthly Contest – 2010.02.06

    Recommend wxl | We have carefully selected several similar problems for you: 3308 3314 3307 3306 3310 ac代码

#include<stdio.h>#include<string.h>#include<iostream>#include<queue>using namespace std;int n,m,vis[25][25][25][25],sx[2],sy[2];char map[25][25];int dx[4]={0,1,0,-1};int dy[4]={1,0,-1,0};struct s{	int x[2],y[2],step,b[2],h[2];	//friend bool operator <(s a,s b)	//{	//	return a.step>b.step;	//}}a,temp;int bfs(){	memset(vis,0,sizeof(vis));	a.x[0]=sx[0],a.x[1]=sx[1];	a.y[0]=sy[0],a.y[1]=sy[1];	a.b[0]=a.b[1]=a.h[0]=a.h[1]=0;	vis[sx[0]][sy[0]][sx[1]][sy[1]]=1;	a.step=0;	//priority_queue<struct s="">q;	queue<struct s="">q;	q.push(a);	while(!q.empty())	{		int i,j;		//a=q.top();		a=q.front();		q.pop();		for(i=0;i<4;i++)		{			temp=a;			for(j=0;j<2;j++)			{				if(temp.b[j])					continue;				temp.x[j]=a.x[j]+dx[i];				temp.y[j]=a.y[j]+dy[i];				if(map[temp.x[j]][temp.y[j]]=='*')				{					temp.x[j]=a.x[j];					temp.y[j]=a.y[j];				}			}						if(vis[temp.x[0]][temp.y[0]][temp.x[1]][temp.y[1]])				continue;			if(temp.x[0]==temp.x[1]&&temp.y[0]==temp.y[1]&&temp.b[0]+temp.b[1]==0)				continue;			vis[temp.x[0]][temp.y[0]][temp.x[1]][temp.y[1]]=1;			temp.step=a.step+1;			int flag=1;			for(j=0;j<2;j++)			{				int now=map[temp.x[j]][temp.y[j]];				if(now<2&&!temp.h[now])				{					temp.h[now]=1;					temp.b[j]=1;				}				if(!temp.b[j])					flag=0;			}			if(flag)				return temp.step;			q.push(temp);		}	}	return -1;}int main(){	int t;	scanf("%d",&t);	while(t--)	{		int i,j;		scanf("%d%d",&n,&m);		int cnt=0,cot=0;		for(i=0;i<n;i++) an="" ans="=-1)" else="" fails="" get="" int="" j="0;j<m;j++)" my="" poor="" pre="" program="" sir="" sorry="" to=""></n;i++)></struct></struct></queue></iostream></string.h></stdio.h>

最新文章