POJ topic 2312 Battle City (BFS)

Source: Internet
Author: User

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 no W.

What's 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 would 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'll be turned to empty spaces when you hit it, however, if your shot hit a steel wall, there'll be no Da Mage to the wall. In all of your turns, you can choose-to-move to a-neighboring (4 directions, not 8) empty space, or shoot in one of the F Our directions without a move. The shot would go ahead in that direction, until it go out of the the map or hit a wall. If the shot hits a brick wall, the wall would disappear (i.e., in this turn). Well, given the description of a maps, the positions of your tank and the target, how many turns would you take at least to Arrive there?

Input

The input consists of several test cases. The first line of all test case contains, integers m and n (2 <= m, n <= 300). Each of the following M lines contains N uppercase letters, each of the which are one of ' Y ' (You), ' T ' (target), ' S ' (Steel WA ll), ' 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 being processed.

Output

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

Sample Input

3 4ybebeeresste0 0

Sample Output

8

Source

POJ Monthly, Lu Xiao

AC Code

#include <stdio.h> #include <string.h> #include <queue> #include <iostream>using namespace std; Char map[1010][1010];struct s{int x,y,step;friend bool operator < (s a,s b) {return a.step>b.step;}} A,temp;int vis[1010][1010],n,m,sx,sy;int dx[4]={0,-1,0,1};int Dy[4]={1,0,-1,0};int Jud (struct s a) {if (a.x<0| | a.x>=n| | a.y<0| | A.Y&GT;=M) return 0;if (map[a.x][a.y]== ' S ' | | map[a.x][a.y]== ' R ') return 0;if (Vis[a.x][a.y]) return 0;return 1;} int BFs () {A.x=sx;a.y=sy;a.step=0;memset (vis,0,sizeof (Vis)); Vis[sx][sy]=1;priority_queue<struct S>q;q.push ( a); while (!q.empty ()) {a=q.top (); Q.pop (); for (int i=0;i<4;i++) {temp.x=a.x+dx[i];temp.y=a.y+dy[i];if (!jud (temp)) Continue;if (map[temp.x][temp.y]== ' B ') temp.step=a.step+2;else//if (map[temp.x][temp.y]== ' E ') temp.step=a.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", &n,&m)!=eof,n| | m) {int i,j;for (i=0;i<n;i++) {scanf ("%s", Map[i]); for (j=0;j<m;j++) {if (map[i][j]== ' Y ') {sx=i;sy=j;}}} printf ("%d\n", BFS ());}}


POJ topic 2312 Battle City (BFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.