1. 程式人生 > >Battle City BFS+優先隊列

Battle City BFS+優先隊列

man hot mem size nsis exc pic chess failed

Battle City

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+優先隊列的題,和之前的WAR CHESS類似,但判斷條件較少。這次主要是想作個比較,用暴力DFS跑一邊,果斷TLE。。BFS+優先隊列只遍歷最少步數之內的點一次,時間效率快很多

dfs length 861 time 1s+

bfs length 1469 time 0.016s memory 1m

Status Time Limit Exceeded
Length 861
Lang G++
Submitted
Shared
RemoteRunId 17284030
Status Accepted
Time 16ms
Memory 1028kB
Length 1469
Lang G++
Submitted
Shared
RemoteRunId 17284040

#include<stdio.h> 
#include
<string.h> char a[305][305]; int b[305][305]; int n,m,f,bx,by,min; void dfs(int x,int y,int s) { if(x<0||y<0||x>=n||y>=m) return; if(b[x][y]==1) return; if(a[x][y]==T){ f=1; if(s<min) min=s; return; } else if(a[x][y]==S){ b[x][y]=1; return; } else if(a[x][y]==R){ b[x][y]=1; return; } else if(a[x][y]==B){ s++; } b[x][y]=1;dfs(x+1,y,s+1);b[x][y]=0; b[x][y]=1;dfs(x,y+1,s+1);b[x][y]=0; b[x][y]=1;dfs(x-1,y,s+1);b[x][y]=0; b[x][y]=1;dfs(x,y-1,s+1);b[x][y]=0; } int main() { int i,j; while(scanf("%d%d",&n,&m)&&!(n==0&&m==0)){ memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); for(i=0;i<n;i++){ scanf("%s",a[i]); for(j=0;j<m;j++){ if(a[i][j]==Y){ bx=i;by=j; } } } f=0; min=200000; dfs(bx,by,0); if(f==1) printf("%d\n",min); else printf("-1\n"); } return 0; }
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;

char a[305][305];
int b[305][305];
int t[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int n,m,bx,by;

struct Node{
    int x,y,s;
    friend bool operator<(Node a,Node b)
    {
        return a.s>b.s;
    }
}node;

void bfs()
{
    int i,f=0,tx,ty;
    priority_queue<Node> q;
    node.x=bx;
    node.y=by;
    node.s=0;
    b[bx][by]=1;
    q.push(node);
    while(q.size()){
        for(i=0;i<4;i++){
            tx=q.top().x+t[i][0];
            ty=q.top().y+t[i][1];
            if(tx<0||ty<0||tx>=n||ty>=m) continue;
            if(b[tx][ty]==1) continue;
            b[tx][ty]=1;
            if(a[tx][ty]==T){
                printf("%d\n",q.top().s+1);
                f=1;
                return;
            }
            else if(a[tx][ty]==S){
                b[tx][ty]=1;
                continue;
            }
            else if(a[tx][ty]==R){
                b[tx][ty]=1;
                continue;
            }
            else if(a[tx][ty]==B){
                node.x=tx;
                node.y=ty;
                node.s=q.top().s+2;
                b[tx][ty]=1;
                q.push(node);
            }
            else if(a[tx][ty]==E){
                node.x=tx;
                node.y=ty;
                node.s=q.top().s+1;
                b[tx][ty]=1;
                q.push(node);
            }
        }
        q.pop();
    }
    if(f==0) printf("-1\n");
}
int main()
{
    int i,j;
    while(scanf("%d%d",&n,&m)&&!(n==0&&m==0)){
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        for(i=0;i<n;i++){
            scanf("%s",a[i]);
            for(j=0;j<m;j++){
                if(a[i][j]==Y){
                    bx=i;by=j;
                }
            }
        }
        bfs();
    }
    return 0;
}

Battle City BFS+優先隊列