zoukankan      html  css  js  c++  java
  • Battle City

    Battle City
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 7208   Accepted: 2427

    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

    3 4
    YBEB
    EERE
    SSTE
    0 0
    

    Sample Output

    8

    Source

    POJ Monthly,鲁小石

    #include<stdio.h>
    #include<string.h>
    #include<queue>
    #include<algorithm>
    using namespace std;
    char map[1010][1010];
    int vis[1010][1010],m,n,x,y;
    int dx[4]={0,1,-1,0};
    int dy[4]={1,0,0,-1};
    struct node 
    {
    	int x,y;
    	int step;
    	friend bool operator < (node n1,node n2)
    	{
    		return n1.step>n2.step;
    	}
    }p,temp;
    int judge(node s)
    {
    	if(s.x<0||s.x>=m||s.y<0||s.y>=n)
    	return 1;
    	if(map[s.x][s.y]=='S'||map[s.x][s.y]=='R')
    	return 1;
    	if(vis[s.x][s.y])
    	return 1;
    	return 0;
    }
    int bfs()
    {
    	priority_queue<node>q;
    	p.x=x;p.y=y;
    	p.step=0;
    	memset(vis,0,sizeof(map));
    	vis[x][y]=1;
    	q.push(p);
    	while(!q.empty())
    	{
    		p=q.top();
    		q.pop();
    		for(int i=0;i<4;i++)
    		{
    			temp.x=p.x+dx[i];
    			temp.y=p.y+dy[i];
    			if(judge(temp)) continue;
    			if(map[temp.x][temp.y]=='B')
    			temp.step=p.step+2;
    			else temp.step=p.step+1;
    			if(map[temp.x][temp.y]=='T')
    			return temp.step;
    			vis[temp.x][temp.y]=1;
    			q.push(temp);
    		}
    	}
    	return -1;
    }
    int main()
    {
    	while(scanf("%d%d",&m,&n),m||n)
    	{
    		int i,j;
    		for(i=0;i<m;i++)
    		{
    			scanf("%s",map[i]);
    			for(j=0;j<n;j++)
    			{
    				if(map[i][j]=='Y')
    				{
    					x=i;y=j;
    				}
    			}
    		}
    		printf("%d
    ",bfs());
    	}
    	return 0;
    }


  • 相关阅读:
    [复变函数]第07堂课 2.2 初等解析函数
    [家里蹲大学数学杂志]第237期Euler公式的美
    [家里蹲大学数学杂志]第287期复变函数讲义
    [家里蹲大学数学杂志]第253期实变函数讲义
    模仿王者荣耀的实时阴影
    Android 识别身份证号码(图片识别)
    基于swiper的移动端H5页面,丰富的动画效果
    基于skitter的轮播图炫酷效果,幻灯片的体验
    基于canvas的原生JS时钟效果
    .net core 实现简单爬虫—抓取博客园的博文列表
  • 原文地址:https://www.cnblogs.com/playboy307/p/5273829.html
Copyright © 2011-2022 走看看