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;
}



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