Oh,mygoddess

很早的时候就看了这一道题目 , 当时不会做 , 现在 边听歌边写无压力 ........

 

题意 : 光辉骑士 一直都在 迷宫的右上角 , 第一行给你迷宫的规格 , 下面是迷宫 "O" 代表空地需要花一个单位时间跨越 , "#" 代表

墙 ,需要三个单位的时间把墙破开 , 也就是 "O"为一个时间  "#"为4个时间 .

 

 下面附上代码 .

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<map>
#include<cctype>
using namespace std;
struct node
{
    int x,y,step;
    friend bool operator <(node s1,node s2)
    {
        return s1.step>s2.step;
    }
};
char a[52][52];
int n,m,tx,ty,b[4][2]={0,-1,0,1,-1,0,1,0},visited[52][52];
priority_queue<node>Q;
int BFS(int x,int y)
{
    node q={x,y,0};
    Q.push(q);
    while(!Q.empty())
    {
        node e=Q.top();                         //  怎么会把 这里  忘了呢    ?
        Q.pop();
        for(int i=0;i<4;i++)
        {
            q.x=e.x+b[i][0],q.y=e.y+b[i][1];   //怎么 整天 都  弄错    ? 
            if(q.x>=0&&q.x<m&&q.y>=0&&q.y<n&&!visited[q.y][q.x])
            {
                if(a[q.y][q.x]=='O')
                    q.step=e.step+1;
                if(a[q.y][q.x]=='#')
                    q.step=e.step+4;
                visited[q.y][q.x]=1;
                Q.push(q);
                if(q.x==tx&&q.y==ty)   //注意  安放的位置
                {
                    return q.step;
                }
            }
        }
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                scanf(" %c",&a[i][j]);
            }
        }
        scanf("%d%d",&ty,&tx);
        tx--;
        ty--;
        memset(visited,0,sizeof(visited));
        visited[0][0]=1;
        while(!Q.empty())   //  注意  清空
            Q.pop();
        int mark=BFS(0,0);
        printf("%d\n",mark);
    }
}

 

 

 

 

posted @ 2016-04-04 17:20  X-POWER  阅读(465)  评论(0编辑  收藏  举报