zoukankan      html  css  js  c++  java
  • B

    来源poj2312

    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

    搜索T的位置,输出要走的步数,用只要把bfs的队列变成优先队列就可以了

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include <iomanip>
    #include<cmath>
    #include<float.h> 
    #include<string.h>
    #include<algorithm>
    #define sf scanf
    #define pf printf
    #define scf(x) scanf("%d",&x)
    #define scff(x,y) scanf("%d%d",&x,&y)
    #define prf(x) printf("%d
    ",x) 
    #define mm(x,b) memset((x),(b),sizeof(x))
    #include<vector>
    #include<queue>
    #include<map>
    #define rep(i,a,n) for (int i=a;i<n;i++)
    #define per(i,a,n) for (int i=a;i>=n;i--)
    typedef long long ll;
    const ll mod=1e9+7;
    const double eps=1e-8;
    const int inf=0x3f3f3f3f;
    using namespace std;
    const double pi=acos(-1.0);
    const int N=3e2+10;
    char MAP[N][N];
    struct node
    {
    	int x,y,ans;
    	friend bool operator <(node a,node b){		return a.ans>b.ans;	}
    	node(int a=0,int b=0){	x=a;y=b;ans=0;	}
    };
    int a[4][2]={-1,0,1,0,0,1,0,-1};
    priority_queue<node>q;
    bool checkt(node a)
    {
    	return (MAP[a.x][a.y]=='T');
    }
    int visit[N][N];
    int bfs()
    {
    	node t,tt;
    	while(!q.empty())
    	{
    		t=q.top();
    		q.pop();
    		visit[t.x][t.y]=1;
    		rep(i,0,4)
    		{
    			tt.ans=t.ans+1;
    			tt.x=t.x+a[i][0];
    			tt.y=t.y+a[i][1];
    			if(!visit[tt.x][tt.y])
    			{
    				if(checkt(tt))
    				return tt.ans;
    				if(MAP[tt.x][tt.y]=='E')
    				q.push(tt);
    				else if(MAP[tt.x][tt.y]=='B')
    				{
    					tt.ans++;
    					q.push(tt); 
    				}
    				visit[tt.x][tt.y]=1;
    			}	
    		}
    	}
    	return -1;
    }
    void display(int n,int m)
    {
    	mm(visit,0);
    	while(!q.empty()) q.pop();
    	int x,y;
    	rep(i,1,n+1)
    	rep(j,1,m+1)
    	if(MAP[i][j]=='Y')
    	{ x=i;y=j;}
    	q.push(node(x,y)); 
    	prf(bfs());
    }
    int main()
    {
    	int n,m;
    	while(~scff(n,m)&&n&&m)
    	{
    		mm(MAP,'S');
    		rep(i,1,n+1)
    		{
    			sf("%s",MAP[i]+1);
    			MAP[i][m+1]='S';
    		}
    		display(n,m);
    	}
    	return 0;
    }
    
  • 相关阅读:
    安卓手机抓包
    探讨 yum 与 rpm 的安装包数量
    CentOS6.5 yum安装桌面环境
    CentOS6.5使用本地光盘做yum源 (参考:http://www.jb51.net/os/RedHat/43343.html)
    tar、zip 、unzip 打包与压缩 (参考:http://pengyl.blog.51cto.com/5591604/1191197)
    CentOS6.5使用本地光盘做yum源 (参考:http://www.jb51.net/os/RedHat/43343.html)
    mount 挂载光盘
    Shell编程
    Vim实现批量注释的方法
    Linux基本命令
  • 原文地址:https://www.cnblogs.com/wzl19981116/p/9497266.html
Copyright © 2011-2022 走看看