首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【POJ】2312-Battle City (bfs,优先队列)

【POJ】2312-Battle City (bfs,优先队列)

作者头像
FishWang
发布2025-08-26 19:03:03
发布2025-08-26 19:03:03
9500
代码可运行
举报
运行总次数:0
代码可运行

Battle City

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 7805

Accepted: 2613

Description

Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now.

What we are discussing is a simple edition of this game. Given a map that consists of empty spaces, rivers, steel walls and brick walls only. Your task is to get a bonus as soon as possible suppose that no enemies will disturb you (See the following picture).

Your tank can't move through rivers or walls, but it can destroy brick walls by shooting. A brick wall will be turned into empty spaces when you hit it, however, if your shot hit a steel wall, there will be no damage to the wall. In each of your turns, you can choose to move to a neighboring (4 directions, not 8) empty space, or shoot in one of the four directions without a move. The shot will go ahead in that direction, until it go out of the map or hit a wall. If the shot hits a brick wall, the wall will disappear (i.e., in this turn). Well, given the description of a map, the positions of your tank and the target, how many turns will you take at least to arrive there?

Input

The input consists of several test cases. The first line of each test case contains two integers M and N (2 <= M, N <= 300). Each of the following M lines contains N uppercase letters, each of which is one of 'Y' (you), 'T' (target), 'S' (steel wall), 'B' (brick wall), 'R' (river) and 'E' (empty space). Both 'Y' and 'T' appear only once. A test case of M = N = 0 indicates the end of input, and should not be processed.

Output

For each test case, please output the turns you take at least in a separate line. If you can't arrive at the target, output "-1" instead.

Sample Input

代码语言:javascript
代码运行次数:0
运行
复制
3 4
YBEB
EERE
SSTE
0 0

Sample Output

代码语言:javascript
代码运行次数:0
运行
复制
8

Source

POJ Monthly,鲁小石

第一次学bfs和优先队列,意料之中的十连跪。

一开始的思路是不断地更新扫描过的地图,连续TLE了4次,后来才采用标记地图的方法,但是在优先队列的重载的地方又4连WA。

此题记下以后反思吧。

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int st_x,st_y,end_x,end_y;		//起点、终点 
char map[311][311];		//原地图 
bool vis_map[311][311];		//扫描地图 
int move_x[4]={0,0,1,-1};
int move_y[4]={1,-1,0,0};		//移动 
int H,W;

int check(int x,int y)		//检查该点是否合法 
{
	if (x<0 || y<0 || x>=H || y>=W || vis_map[x][y] || map[x][y]=='S' || map[x][y]=='R')
		return 1;		//不能移动到此
	else
		return 0; 
}

struct tank
{
	int x,y,stp;
	bool friend operator<(tank num1,tank num2)
	{
		return num1.stp>num2.stp;
	}
}a,next;

int bfs()		//搜索
{
	priority_queue<tank> move;
	a.x=st_x;
	a.y=st_y;
	a.stp=0;
	move.push(a);
	vis_map[st_x][st_y]=1;
	while (!move.empty())
	{
		a=move.top();
		move.pop();
		if (a.x==end_x && a.y==end_y)
			return a.stp;
		for (int i=0;i<4;i++)
		{
			next.x=a.x+move_x[i];
			next.y=a.y+move_y[i];
			next.stp=a.stp;
			if (check(next.x,next.y))		//移动是否合法 
				continue;
			if (map[next.x][next.y]=='B')		//是否为软墙,是则多花一步 
				next.stp+=2;
			else
				next.stp++;
			vis_map[next.x][next.y]=1;
			move.push(next);
		}
	}
	return -1;
}

int main()
{
	while (~scanf ("%d %d",&H,&W) && (H||W))
	{
		for (int i=0;i<H;i++)		//输入并寻找起点终点 
		{
			scanf ("%s",map[i]);
			for (int j=0;j<W;j++)
			{
				if (map[i][j]=='Y')
				{
					st_x=i;
					st_y=j;
				}
				else if (map[i][j]=='T')
				{
					end_x=i;
					end_y=j;
				}
			}
		}
		memset(vis_map,0,sizeof(vis_map));
		int ans;
		ans=bfs();
		printf ("%d\n",ans);
	}
}

纠结了三天的题,AC了还是蛮开心的。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档