poj 2312:Battle City

坦克大战的最短路问题。E代表空地,B代表砖墙,一发炮弹可以解决,S代表铁墙,无法炸毁,R代表河流,无法通过,Y代表当前位置,T代表目标位置。每回合可以走一步,或者来一发,求最终到达目标位置的最少回合数。无法到达输出-1。

 

最短路嘛,spfa直接解决。bfs+优先队列也是可以做的,不过优先队列怎么用啊。。。各种头文件求指导。。。


#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
using namespace std;

typedef struct coordinate {
    int x,y;
    int turn;
}point;

const int inf = 1<<20;
const int MAX = 305;
int n,m;
int map[MAX][MAX];
int dis[MAX][MAX];
bool had[MAX][MAX];
queue <point> q;

void bfs(int sx,int sy){
    memset(had,0,sizeof(had));
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            dis[i][j]=inf;
        }
    }
    dis[sx][sy]=0;
    point temp;
    temp.x=sx; temp.y=sy;
    q.push(temp);
    had[sx][sy]=1;
    while(!q.empty()){
        point t=q.front(); q.pop();
        had[t.x][t.y]=0;
        if(t.x-1>=0 && dis[t.x-1][t.y]>dis[t.x][t.y]+map[t.x-1][t.y]) {
            dis[t.x-1][t.y]=dis[t.x][t.y]+map[t.x-1][t.y];
            if(!had[t.x-1][t.y]){
                had[t.x-1][t.y]=1;
                point tt=t;
                tt.x--;
                q.push(tt);
            }
        }

        if(t.x+1<n && dis[t.x+1][t.y]>dis[t.x][t.y]+map[t.x+1][t.y]) {
            dis[t.x+1][t.y]=dis[t.x][t.y]+map[t.x+1][t.y];
            if(!had[t.x+1][t.y]){
                had[t.x+1][t.y]=1;
                point tt=t;
                tt.x++;
                q.push(tt);
            }
        }

        if(t.y-1>=0 && dis[t.x][t.y-1]>dis[t.x][t.y]+map[t.x][t.y-1]) {
            dis[t.x][t.y-1]=dis[t.x][t.y]+map[t.x][t.y-1];
            if(!had[t.x][t.y-1]){
                had[t.x][t.y-1]=1;
                point tt=t;
                tt.y--;
                q.push(tt);
            }
        }

        if(t.y+1<m && dis[t.x][t.y+1]>dis[t.x][t.y]+map[t.x][t.y+1]) {
            dis[t.x][t.y+1]=dis[t.x][t.y]+map[t.x][t.y+1];
            if(!had[t.x][t.y+1]){
                had[t.x][t.y+1]=1;
                point tt=t;
                tt.y++;
                q.push(tt);
            }
        }
    }
}

int main(){
    //freopen("in.txt","r",stdin);
    while(cin >> n >> m){
        if(!n && !m) break;

        int sx=-1,sy=-1;
        int tx=-1,ty=-1;
        for(int i=0;i<n;i++){
            char ss[305];
            scanf("%s",ss);
            for(int j=0;j<m;j++){
                if(ss[j]=='Y') {
                    sx=i; sy=j;
                    map[i][j]=1;
                }
                else if(ss[j]=='T') {
                    tx=i; ty=j;
                    map[i][j]=1;
                }
                else if(ss[j]=='S' || ss[j]=='R') {
                    map[i][j]=inf;
                }
                else if(ss[j]=='B') {
                    map[i][j]=2;
                }
                else if(ss[j]=='E') {
                    map[i][j]=1;
                }
            }
        }
        bfs(sx,sy);
        if(tx==-1) cout<<-1<<endl;
        else if(dis[tx][ty]!=inf) cout<<dis[tx][ty]<<endl;
        else cout<<-1<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值