首页 > 其他分享 >nyoj 坦克大战 284 (bfs) 模板

nyoj 坦克大战 284 (bfs) 模板

时间:2023-04-20 17:37:35浏览次数:41  
标签:f1 int 310 nyoj bfs wall will include 284


坦克大战


1000 ms  |           内存限制: 65535


3



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?


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.

输出 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.

样例输入

3 4YBEB EERE SSTE 0 0


样例输出

8


//唉,南阳又崩了,写了两个题,提交一直在判题。。。不知正确与否。。先贴上明天再提交试试。。。


//题意:


//Y是坦克所在位置


//B表示砖墙,要花费1分钟将其摧毁变为自由空间,(也就是说遇到砖墙,坦克要花费两分钟走一格)


//R是河,坦克不能过


//S是铁墙,坦克不能过


//E是自由空间,坦克可自由行走,每走一格花费一分钟


//T是目的地


//哦,这个程序今天提交一下WA。。。改了一下过了


<pre class="cpp" name="code">#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
int b[310][310];
char a[310][310];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int n,m;
struct zz
{
	int x;
	int y;
	int l;
	friend bool operator<(zz a,zz b)
	{
		return a.l>b.l;
	}
}f1,f2;
void bfs(int x,int y)
{
	priority_queue<zz>q;
	memset(b,0,sizeof(b));
	f1.x=x;f1.y=y;f1.l=0;
	q.push(f1);
	b[x][y]=1;
	while(!q.empty())
	{
		f1=q.top();
		q.pop();			
		for(int i=0;i<4;i++)
		{
			f2.x=f1.x+dx[i];
			f2.y=f1.y+dy[i];
			if(a[f2.x][f2.y]=='T')
			{
				printf("%d\n",f1.l+1); 
				return ;
			}
			if(f2.x>=1&&f2.x<=n&&f2.y>=1&&f2.y<=m&&!b[f2.x][f2.y]&&a[f2.x][f2.y]!='R'&&a[f2.x][f2.y]!='S')
			{					
				b[f2.x][f2.y]=1;
				if(a[f2.x][f2.y]=='B')
				{
					f2.l=f1.l+2;
					a[f2.x][f2.y]='E';
				}
				else
				{
					f2.l=f1.l+1;
					a[f2.x][f2.y]='E';
				}
				q.push(f2);
			}
		}
	}
	printf("-1\n");
}
int main()
{
	int i,j,sx,sy;
	while(scanf("%d%d",&n,&m),n|m)
	{
		for(i=1;i<=n;i++)
			scanf("%s",a[i]+1);
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=m;j++)
			{
				if(a[i][j]=='Y')
				{
					sx=i;
					sy=j;
				}
			}
		}
		bfs(sx,sy);
	}
	return 0;
}


//这个也是正确的

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
int b[310][310];
char a[310][310];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int n,m,flag,cnt;
struct zz
{
	int x;
	int y;
	int l;
	friend bool operator<(zz a,zz b)
	{
		return a.l>b.l;
	}
}f1,f2;
void bfs(int x,int y)
{
	priority_queue<zz>q;
	f1.x=x;f1.y=y;f1.l=0;
	b[x][y]=1;
	q.push(f1);	
	while(!q.empty())
	{
		f1=q.top();
		q.pop();			
		for(int i=0;i<4;i++)
		{
			f2.x=f1.x+dx[i];
			f2.y=f1.y+dy[i];
			if(a[f2.x][f2.y]=='T')
			{
				flag=1;
				cnt=f1.l+1;
				return ;
			}
			else if(f2.x>=1&&f2.x<=n&&f2.y>=1&&f2.y<=m&&!b[f2.x][f2.y]&&a[f2.x][f2.y]!='R'&&a[f2.x][f2.y]!='S')
			{					
				
				if(a[f2.x][f2.y]=='B')
					f2.l=f1.l+2;
				else
					f2.l=f1.l+1;
				b[f2.x][f2.y]=1;
				q.push(f2);
			}
		}
	}
}
int main()
{
	int i,j,sx,sy;
	while(scanf("%d%d",&n,&m),n&&m)
	{
		for(i=1;i<=n;i++)
			scanf("%s",a[i]+1);
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=m;j++)
			{
				if(a[i][j]=='Y')
				{
					sx=i;
					sy=j;
				}
			}
		}
		memset(b,0,sizeof(b));
		flag=0;
		bfs(sx,sy);
		if(flag)
			printf("%d\n",cnt);
		else
			printf("-1\n");
	}
	return 0;
}



标签:f1,int,310,nyoj,bfs,wall,will,include,284
From: https://blog.51cto.com/u_16079508/6210022

相关文章

  • 最短时间——BFS
    最短时间有若干个城市,它们之间有道路连通,可以互相到达,从一个城市到另一个城市时间为1。现在给出起点城市A,终点城市B,和N条道路。问从A到B最短时间。输入第一行A,B,N(A,B,N<=30),B为最大城市标号接下来N行,每行两个数x,y,表示城市x和城市y有道路相连。输出输出最短时间。样例输入......
  • LeetCode 双周赛 102,模拟 / BFS / Dijkstra / Floyd
    本文已收录到AndroidFamily,技术和职场问题,请关注公众号[彭旭锐]提问。大家好,欢迎来到小彭的LeetCode周赛解题报告。昨晚是LeetCode双周赛第102场,你参加了吗?这场比赛比较简单,拼的是板子手速,继上周掉大分后算是回了一口血......
  • HDU 2842 Chinese Rings(矩阵快速幂+递推)
    题目地址:HDU2842这个游戏是一个九连环的游戏。假设当前要卸下前n个环。由于要满足前n-2个都卸下,所以要先把前n-2个卸下,需要f(n-2)次。然后把第n个卸下需要1次,然后这时候要卸下第n-1个,然后此时前n-2个都已经被卸下了。这时候把前n-2个都卸下与都装上所需的次数是一样的,因为卸下与装......
  • Codeforces Round #284 (Div. 1) C. Array and Operations (最大流)
    题目地址:http://codeforces.com/contest/498/problem/C分别分解出每个数字的质因子,然后第奇数个数字的质因子在左边集合,偶数个数字的质因子在右边集合,建立源点和汇点,然后根据每个数字含有的质因子的个数建边,跑一遍最大流即可。代码如下:#include<iostream>#include<string.h>......
  • POJ 1753 Flip Game(bfs枚举+递推)
    题目地址:http://poj.org/problem?id=1753这题此前曾经做过,但当时是用的纯BFS做的,然后后来又做了一次组队赛,那是16*16的,果断超时超内存。。能超的都超了。。于是又找了个更好的方法,就是枚举第一行,然后后面的根据第一行的情况推下去。比如,你要让所有的都变成W,如果上一行的对应位置是B......
  • 天梯赛练习题 L3-008 喊山(bfs)
    https://pintia.cn/problem-sets/994805046380707840/exam/problems/994805050709229568输入样例:75412233145561457输出样例:2640#include<bits/stdc++.h>usingnamespacestd;typedeflonglongLL;typedefpair<LL,LL>PII;constLLMAX......
  • 天梯赛练习题 L3-004 肿瘤诊断(bfs)
    https://pintia.cn/problem-sets/994805046380707840/exam/problems/994805052626026496输入样例:3452111111111111001100110011101101000000101100000000000100011000输出样例:26LLdz[]={1,-1,0,0,0,0},dx......
  • spfa求最短路——BFS,数组实现邻接表,数组实现队列
    题目描述题目来源AcWing给定一个n个点m条边的有向图,图中可能存在重边和自环,边权可能为负数。请你求出1号点到n号点的最短距离,如果无法从1号点走到n号点,则输出impossible。数据保证不存在负权回路。输入格式第一行包含整数n和m。接下来m行每行包含三个......
  • Leetcode(剑指offer专项训练)——DFS/BFS专项(2)
    课程顺序题目现在总共有numCourses 门课需要选,记为 0 到 numCourses-1。给定一个数组 prerequisites,它的每一个元素 prerequisites[i] 表示两门课程之间的先修顺序。 例如 prerequisites[i]=[ai,bi] 表示想要学习课程ai ,需要先完成课程bi 。请根据给出的......
  • Leetcode(剑指offer专项训练)——DFS/BFS专项(1)
    计算除法题目给定一个变量对数组equations和一个实数值数组values作为已知条件,其中equations[i]=[Ai,Bi]和values[i]共同表示等式Ai/Bi=values[i]。每个Ai或Bi是一个表示单个变量的字符串。另有一些以数组queries表示的问题,其中queries[j]=[Cj,Dj]......