zoukankan      html  css  js  c++  java
  • POJ 2312 Battle City (bfs+优先队列)

    http://acm.pku.edu.cn/JudgeOnline/problem?id=2312

    题意:著名的90坦克大战游戏,小时后在小霸王上整天玩,哈哈,给定一个地图,问坦克是否能够达到目的点,输出最少的步数(动作)

    思路:因为坦克在打烂砖墙brick时,需要耗掉一步,我们可以这样想,如果我们想要打坏一个brick,必然我们需要经过这个brick,至于这个brick什么时候打就无关紧要了,所以要经过一个brick时,步数为2,而经过空地是1,这样我们必须用到优先队列了,STL优先队列第一次用,果然狠强大!

    #include <iostream>
    #include
    <cstdio>
    #include
    <algorithm>
    #include
    <memory.h>
    #include
    <cmath>
    #include
    <set>
    #include
    <queue>
    #include
    <vector>
    using namespace std;

    const int BORDER = (1<<20)-1;
    #define MAXN 305
    #define INF 0x7ffffff
    #define CLR(x,y) memset(x,y,sizeof(x))
    #define ADD(x) x=(++x)&BORDER
    #define IN(x) scanf("%d",&x)
    #define OUT(x) printf("%d\n",x)
    #define MIN(m,v) (m)<(v)?(m):(v)
    #define MAX(m,v) (m)>(v)?(m):(v)
    #define ABS(x) (x>0?x:-x)

    struct NODE{
    int x,y;
    int step;
    }node,tmp_node;
    bool operator<(const NODE& a,const NODE& b)
    {
    return a.step>b.step;
    }
    priority_queue
    <NODE> que;
    int direct[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
    int n,m,sx,sy,ex,ey;
    int arr[MAXN][MAXN],visit[MAXN][MAXN];
    bool _is(const int& x,const int& y)
    {
    if(x<0 || x>=n)
    return false;
    if(y<0 || y>=m)
    return false;
    return true;
    }
    int init()
    {
    CLR(visit,
    0);
    CLR(arr,
    0);
    return 0;
    }
    int input()
    {
    int i,j;
    char str[MAXN];
    for(i = 0; i < n; ++i)
    {
    scanf(
    "%s",str);
    for(j = 0; j < m; ++j)
    {
    switch(str[j])
    {
    case 'B': arr[i][j] = 1; break;
    case 'S': arr[i][j] = 2; break;
    case 'R': arr[i][j] = 2; break;
    case 'Y': sx=i;sy=j; break;
    case 'T': ex=i;ey=j; break;
    }
    }
    }
    return 0;
    }
    int work()
    {
    int i,j,tmp,x,y;
    int mmin = INF;
    node.x
    = sx;
    node.y
    = sy;
    node.step
    = 0;
    /* empty que */
    while(!que.empty())
    que.pop();
    /* init the que */
    que.push(node);
    visit[sx][sy]
    = 1;
    /* while loop */
    while(!que.empty())
    {
    node
    = que.top();
    que.pop();
    if(node.x == ex && node.y == ey)
    break;
    for( i = 0; i < 4; ++i)
    {
    x
    = node.x + direct[i][0];
    y
    = node.y + direct[i][1];
    if(_is(x,y) && !visit[x][y])
    {
    if(arr[x][y]&2)
    continue;
    tmp_node.x
    = x;
    tmp_node.y
    = y;
    visit[x][y]
    = 1;
    if(arr[x][y]&1)
    {
    tmp_node.step
    = node.step + 2;
    que.push(tmp_node);
    }
    else
    {
    tmp_node.step
    = node.step + 1;
    que.push(tmp_node);
    }
    }
    }
    }
    if(node.x == ex && node.y == ey)
    mmin
    = node.step;
    else
    mmin
    = -1;
    OUT(mmin);
    return 0;
    }
    int main()
    {
    while(scanf("%d%d",&n,&m))
    {
    if(!n && !m)
    break;
    init();
    input();
    work();
    }
    return 0;
    }
  • 相关阅读:
    【.NET】Web Service
    【Coding】C# 操作文件(一)
    【设计模式】设计模式概述
    TCP/IP协议
    【.NET】SOAP Web Service
    简单读写xml
    利用winform来承载WCF服务
    在panel里面显示一个窗体
    asp.net 角色管理 MSDN帮助路径
    asp.net ajax MSDN帮助
  • 原文地址:https://www.cnblogs.com/lvpengms/p/1704661.html
Copyright © 2011-2022 走看看