zoukankan      html  css  js  c++  java
  • C

    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

    原题链接:点击打开链接

    思路:简单的bfs,但是因为点的权值不同,所以要用到优先队列,顺便对struct <重新定义一下

    ac代码:

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<queue>
    using namespace std;
    const int maxn = 310;
    char map[maxn][maxn];
    int vis[maxn][maxn];
    int n,m,x1,y1,x2,y2;
    int k[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
    struct node{
    	int x,y,cost;
    	node(int _x,int _y,int _cost)
    	{
    		x = _x;
    		y = _y;
    		cost = _cost;
    	}
    	friend bool operator < (node x,node y)
    	{
    		return x.cost>y.cost;
    	}
    };
    int bfs()
    {
    	priority_queue<node>que;
    	que.push(node(x1,y1,0));
    	vis[x1][y1] = 1;
    	while(!que.empty())
    	{
    		node q = que.top();
    		que.pop();
    		for(int i = 0;i<4;i++)
    		{
    			node v = q;
    			v.x += k[i][0];
    			v.y += k[i][1];
    			if(map[v.x][v.y] == 'B') v.cost += 2;
    			else if(map[v.x][v.y] == 'E') v.cost += 1;
    			else if(map[v.x][v.y] == 'T') v.cost += 1;
    			if(v.x == x2 &&v.y == y2) return v.cost;
    			if(v.x>=0 && v.x<n && v.y>=0 && v.y < m && !vis[v.x][v.y] && (map[v.x][v.y] == 'B'||map[v.x][v.y] == 'E'||map[v.x][v.y] == 'T'))
    			{
    				que.push(node(v.x,v.y,v.cost));
    				vis[v.x][v.y] = 1;
    			}
    		}
    	}
    	return -1;
    }
    int main()
    {
    	while(cin>>n>>m && (n+m))
    	{
    		memset(vis,0,sizeof(vis));
    		getchar();
    		for(int i = 0;i<n;i++)
    		{
    			for(int j = 0;j<m;j++)
    			{
    				scanf("%c",&map[i][j]);
    				if(map[i][j] == 'Y')
    				{
    					x1 = i;
    					y1 = j;
    				}
    				else if(map[i][j] == 'T')
    				{
    					x2 = i;
    					y2 = j;
    				}
    			}
    			getchar();
    		}
    		
    		cout<<bfs()<<endl;
    	}
    	return 0;
    }


  • 相关阅读:
    洛谷 P1414 又是毕业季II Label:None
    洛谷 P1372 又是毕业季I Label:None
    洛谷 P1111 修复公路 Label:并查集
    高精度特别策划 加减乘除余~~~
    洛谷 P1967 货车运输 Label: 倍增LCA && 最小瓶颈路
    数组指针和指针数组的区别
    堆和栈的区别
    JAVA8 十大新特性详解
    自己在菜单栏中加了一项打开文件的菜单后窗口不刷新 单击才刷新
    Windows 7 OpenGL配置
  • 原文地址:https://www.cnblogs.com/Nlifea/p/11746056.html
Copyright © 2011-2022 走看看