WOJ 1546 Maze


怎么简单的题目,比赛时看都没看。。。。。不知道当时在干什么。。。。
用SublimeText敲代码的手感就是好


Problem 1546 - J - Maze
Time Limit: 1000MS    Memory Limit: 65536KB   
Total Submit: 68   Accepted: 8   Special Judge: No
Description

Get out from the maze!

 

He looks around, finding that the maze has only one exit but it has been locked. He has not enough energy to break the door and he must move out to find extra bonuses of energy hidden in the maze. He can move toward 4 directions: left, right, up, and down, which will take 1 second in a single move.

 

Unfortunately, there is an evil machine which will detect whether he is still there or not every L seconds in the initial place where he is located. And if he is out of the maze, the machine will surely be out of service.

 

Right after he is in the exit grid, he will be considered to be outside the maze.

 

Input
The input consists of one or more test cases.

First line of each test case contains 5 integers W(1<=W<=50), H(1<=H<=50), L(1<=L<=1,000,000), M(1<=M<=10), and S(0<=S<=10,000,000), indicating:
W: the width of the maze.
H: the height of the maze.
L: time of the round of the machine.
M: the number of the bonuses of energy.
S: energy needed to break the lock.

The next line contains M integers indicating the list of the energy-bonus.

Then there comes the map of the maze:
#: wall
.: empty place
$: initial position
<: exit grid
@: energy bonuses

Energy bonuses are ordered from the top to bottom, left to the right.
Output
For each test case, output “YES” or “NO”
Sample Input
3
4 4 2 2 200
100 200
####
#$@#
#@<#
####

4 4 1 2 300
100 200
####
#$@#
#@<#
####

12 5 13 2 400
100 200
############
#@.........#
#.########.#
#$...@....<#
############
Sample Output
YES
NO
NO
Hint
Source

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>

using namespace std;

const int INF=0x3f3f3f3f;
const int dir_x[4]={-1,1,0,0};
const int dir_y[4]={0,0,-1,1};

typedef pair<int,int> pII;

char maze[60][60];
int w,h,l,m,s,egy[20],EG[60][60],step[60][60];
int qi[60][60],zhong[60][60];
vector<pII> egpoint;

void init()
{
	egpoint.clear();
	memset(maze,0,sizeof(maze));
	memset(egy,0,sizeof(egy));
 	memset(EG,0,sizeof(EG));
 	memset(step,0,sizeof(step));
 	memset(qi,63,sizeof(qi));
 	memset(zhong,63,sizeof(zhong));
}

int main()
{
	int T_T;
	scanf("%d",&T_T);
while(T_T--)
{
	int sum=0;
	init();
	scanf("%d%d%d%d%d",&w,&h,&l,&m,&s);	
	for(int i=0;i<m;i++) scanf("%d",egy+i),sum+=egy[i];
	for(int i=0;i<h;i++) scanf("%s",maze[i]); //for(int i=0;i<h;i++) cout<<maze[i]<<endl;
	if(sum<s)
	{
		puts("NO"); continue;///根本没有足够的能量
 	}
	pII IP,EXIT;int cur=0;
	for(int i=0;i<h;i++)
	{
		for(int j=0;j<w;j++)
		{
			if(maze[i][j]=='$')
			{
				IP=make_pair(i,j);
			}
			else if(maze[i][j]=='<')
			{
				EXIT=make_pair(i,j);
			}
			else if(maze[i][j]=='@')
			{
				EG[i][j]=egy[cur++];
				egpoint.push_back(make_pair(i,j));
			}
		}
	}
	queue<pII> q;
	q.push(IP);
	qi[IP.first][IP.second]=0;
	while(!q.empty())//从起点到各个点的距离
	{
		pII u=q.front(),v; q.pop();
		for(int i=0;i<4;i++)
		{
			v.first=u.first+dir_x[i];
			v.second=u.second+dir_y[i];
			if(maze[v.first][v.second]=='#'||qi[v.first][v.second]!=INF) continue;
			qi[v.first][v.second]=qi[u.first][u.second]+1;
			q.push(v);
		}
	}
	if(qi[EXIT.first][EXIT.second]>l)
	{
		puts("NO"); continue;  ///即使不要能量也跑不出去
	}
	while(!q.empty()) q.pop();
	q.push(EXIT);
	zhong[EXIT.first][EXIT.second]=0;
	while(!q.empty())///各个点到出口的距离
	{
 		pII u=q.front(),v; q.pop();
		for(int i=0;i<4;i++)
		{
			v.first=u.first+dir_x[i];
			v.second=u.second+dir_y[i];
			if(maze[v.first][v.second]=='#'||zhong[v.first][v.second]!=INF) continue;
			zhong[v.first][v.second]=zhong[u.first][u.second]+1;
			q.push(v);
		}
	}
	int CAN=0;
	for(int i=0,sz=egpoint.size();i<sz;i++)	
	{
		int X=egpoint[i].first;
		int Y=egpoint[i].second;

		if(qi[X][Y]*2<=l||zhong[X][Y]*2<=l||qi[X][Y]+zhong[X][Y]<=l)	
			CAN+=EG[X][Y];
	}
	if(CAN>=m) puts("YES");
	else puts("NO");
}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值