oh,my goddess

Oh, my goddess

时间限制:3000 ms  |  内存限制:65535 KB
难度:3
描述

Shining Knight is the embodiment of justice and he has a very sharp sword can even cleavewall. Many bad guys are dead on his sword.

One day, two evil sorcerer cgangee and Jackchess decided to give him some colorto see. So they kidnapped Shining Knight's beloved girl--Miss Ice! They built a M x Nmaze with magic and shut her up in it.

Shining Knight arrives at the maze entrance immediately. He can reach any adjacent emptysquare of four directions -- up, down, left, and right in 1 second. Or cleave one adjacent wall in 3

seconds, namely,turn it into empty square. It's the time to save his goddess! Notice: ShiningKnight won't leave the maze before he find Miss Ice.

输入
The input consists of blocks of lines. There is a blank line between two blocks.

The first line of each block contains two positive integers M <= 50 and N <= 50separated by one space. In each of the next M lines there is a string of length N contentsO and #.

O represents empty squares. # means a wall.

At last, the location of Miss Ice, ( x, y ). 1 <= x <= M, 1 <= y <= N.

(Shining Knight always starts at coordinate ( 1, 1 ). Both Shining and Ice's locationguarantee not to be a wall.)
输出
The least amount of time Shining Knight takes to save hisgoddess in one line.
样例输入
3 5
O####
#####
#O#O#
3 4
样例输出

14

个人理解:该题大意是输入有三部分,第一行有两个数字分别代表下面几行(除了最后一行)的行数和列数,最后一行代表的是公主的位置(座标) 中间的几行有"#"和"O"他们分别代表的意思是:"#"中间是一个屋子四面都是墙,"O"的意思是四面都是空的(没有墙)。王子手里的宝剑在三个单位时间内可以打破一堵墙,王子从一个房 跨越到另一个房间需要 1个单位时间。(王子只能 在上下左右 这四个方向上移动 )现在给你上述信息, 你需要求出来王子。在用时最短的情况下,几分钟可以将公主解救出来.该题通过设立一个函数将这些关系表示出来,并将各种情况下所需时间关系表示出来,从而求出总时间。第二个是复制百度上的代码,第一个超时了。

结果 时间 内存 语言
Accepted 36 324 C++
#include <stdio.h>
#include <string.h>
int min=1000000,a,b,v[55][55];
char s[55][55];
void d(int x,int y,int m,int n,int sum)
{
	if(x==m&&y==n)
	{
		if(sum<min)
			min=sum;
	}
	else{
		if(x-1>0&&(!v[x-1][y]))
		{
			v[x-1][y]=1;
			if(s[x-1][y]=='#')
                d(x-1,y,m,n,sum+4);
			else
			    d(x-1,y,m,n,sum+1);
			v[x-1][y]=0;
		}
		if(x+1<=a&&(!v[x+1][y]))
		{
			v[x+1][y]=1;
			if(s[x+1][y]=='#')
                d(x+1,y,m,n,sum+4);
			else
			    d(x+1,y,m,n,sum+1);
			v[x+1][y]=0;
		}
		if(y-1>0&&(!v[x][y-1]))
		{
			v[x][y-1]=1;
			if(s[x][y-1]=='#')
                d(x,y-1,m,n,sum+4);
			else
			    d(x,y-1,m,n,sum+1);
			v[x][y-1]=0;
		}
		if(y+1<=b&&(!v[x][y+1]))
		{
			v[x][y+1]=1;
			if(s[x][y+1]=='#')
                d(x,y+1,m,n,sum+4);
			else
			    d(x,y+1,m,n,sum+1);
			v[x][y+1]=0;
		}
	}
}

int main()
{
	int i,j,m,n;
	while(scanf("%d%d%*c",&a,&b)==2)
	{
		for(i=1;i<=a;i++){
			for(j=1;j<=b;j++)
				scanf("%c",&s[i][j]);
			getchar();
		}
		scanf("%d%d",&m,&n);
		memset(v,0,sizeof(v));
		v[1][1]=1;
		d(1,1,m,n,0);
		printf("%d\n",min);
	}
	return 0;
}
2.

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
char mp[55][55];
int vis[55][55];
int n,m;
int x,y,ex,ey;
int ans;
int dx[4]={0,1,-1,0};
int dy[4]={1,0,0,-1};
struct node 
{
	int x,y,step;
	bool friend operator < (node a,node b)
	{
		return a.step>b.step;
	} 
}a,temp;
int judge()
{
	if(temp.x<1||temp.x>n)	return 0;
	if(temp.y<1||temp.y>m)	return 0;
	if(vis[temp.x][temp.y]==1)	return 0;
	if(temp.step>=ans) return 0;
	return 1;
}
void bfs()
{
	a.x=x;
	a.y=y;
	a.step=0;
	priority_queue<node>q;
	q.push(a);
	memset(vis,0,sizeof(vis));
	vis[x][y]=1;
	while(!q.empty())
	{
		a=q.top();
		q.pop();
		for(int i=0;i<4;i++)
		{
			temp.x=a.x+dx[i];
			temp.y=a.y+dy[i];
			if(mp[temp.x][temp.y]=='#')
				temp.step=a.step+4;
			else
				temp.step=a.step+1;
			if(judge())
			{
				if(temp.x==ex&&temp.y==ey)
				{
					ans=temp.step;
//					printf("%d\n",temp.step);
					return;
				}
				q.push(temp);
				vis[temp.x][temp.y]=1;
			}
		}
	}
}
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		ans=INF;//我也真是够了,这个竟然忘记写了!错了好长时间! 
		for(int i=1;i<=n;i++)
		{
			scanf("%s",mp[i]+1);	
		}
		x=1;y=1;
		scanf("%d%d",&ex,&ey);
		if(ex==x&&ey==y)
		{
			printf("0\n");
			continue;
		}
		bfs();
		printf("%d\n",ans);
	}
	return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章