POJ 2435Navigating the City(bfs)

题意:给你一个地图,’+’代表十字路口,‘-’‘|’表示街道,‘.’表示建筑物,‘s’,’E’ 起点和终点。输出从起点到终点的的 最短路径(包括方向和沿该方向的经过的十字路口数)

分析:ans[i][j],起点到(i,j)点的最短路径,bfs求出所有,再从终点回推到起点得最短路径。

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll  INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod =  1000000007;
struct point{
    int x,y;
};
int ans[100][100],n,m,mapc[100][100];
int sx,sy,ex,ey;
int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int dirc[10010],dept[10010],index=0;
void bfs(){
    point a,b;
    memset(ans,0x3f,sizeof(ans));
    a.x=sx;a.y=sy;
    ans[sx][sy]=0;
    queue<point>q;
    q.push(a);
    while(!q.empty()){
        b=q.front();
        q.pop();
        if(b.x==ex&&b.y==ey){
            break;
        }
        for(int i=0;i<4;++i){
            int xx=b.x+dir[i][0];
            int yy=b.y+dir[i][1];
            if(xx<0||xx>=n||yy<0||yy>=m||!mapc[xx][yy]||ans[xx][yy]<=ans[b.x][b.y]+1)
                continue;
            ans[xx][yy]=ans[b.x][b.y]+1;
            a.x=xx;
            a.y=yy;
            q.push(a);
        }
    }
    //逆向求路径
    int tx=ex,ty=ey,ncase=-1,num=0;
    while(tx!=sx||ty!=sy){
        for(int i=0;i<4;++i){
            int xx=tx+dir[i][0];
            int yy=ty+dir[i][1];
           if(xx<0||xx>=n||yy<0||yy>=m||!mapc[xx][yy]||ans[xx][yy]!=ans[tx][ty]-1)continue;
           if(mapc[xx][yy]==1)
                num++;
           if(ncase!=i&&ncase!=-1){
                dirc[++index]=ncase;
                dept[index]=num;
                num=0;
           }
           ncase=i;
           tx=xx;
           ty=yy;
        }
    }
    dirc[++index]=ncase;
    dept[index]=num+1;
}
int main()
{
    char dic[4]={‘W‘,‘N‘,‘E‘,‘S‘};
    scanf("%d%d",&n,&m);
    n=2*n-1;
    m=2*m-1;
    char ch;
    for(int i=0;i<n;++i)
    for(int j=0;j<m;++j)
    {
        cin>>ch;
        if(ch==‘.‘)mapc[i][j]=0;
        else if(ch==‘+‘)mapc[i][j]=1;
        else if(ch==‘S‘){mapc[i][j]=2;sx=i;sy=j;}
        else if(ch==‘E‘){mapc[i][j]=3;ex=i;ey=j;}
        else mapc[i][j]=4;
    }
    /*for(int i=0;i<n;++i){
        for(int j=0;j<m;++j)
        cout<<mapc[i][j]<<" ";
    cout<<endl;
    }*/
     bfs();
     for(int i=index;i>=1;--i)
        printf("%c %d\n",dic[dirc[i]],dept[i]);
return 0;
}
时间: 2024-11-09 17:27:38

POJ 2435Navigating the City(bfs)的相关文章

POJ 题目2312 Battle City(BFS)

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 now. What we are d

poj 3669 Meteor Shower(bfs)

Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hit. Anxious for her safety, she vows to find her way to a safe location (one that is never destroy

poj 2251 Dungeon Master(bfs)

题目链接  http://poj.org/problem?id=2251 题意:一个立体空间, 输入三个数,L,R,C,代表有L个平面,R行,C列,.代表可以走,#代表不能走,S代表开始点,E代表结束点,问从S开始走,对每个位置,有六个走法,即空间的六个方向的走法(上下东南西北),一分钟可以走一个点,问从S走到E点,最少可以经过多少分钟,若不能到达,则输出Trapped! 简单的三维bfs随便做一下就可以了. #include <iostream> #include <cstring&g

poj2312--Battle City(Bfs)

Battle City Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7712 Accepted: 2582 Description 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 discu

POJ 3126 Prime Path (BFS)

[题目链接]click here~~ [题目大意]给你n,m各自是素数,求由n到m变化的步骤数,规定每一步仅仅能改变个十百千一位的数,且变化得到的每个数也为素数 [解题思路]和poj 3278类似.bfs+queue,分别枚举个十百千的每一位就能够了,只是注意个位仅仅能为奇数,且千位从1開始 代码: #ifndef _GLIBCXX_NO_ASSERT #include <cassert> #endif #include <cctype> #include <cerrno&g

POJ 2251 Dungeon Master (bfs)

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<queue> using namespace std; char mat[50][50][50]; int vis[50][50][50]; int op[6][3]={0,-1,0, 0,1,0, 1,0,0, -1,0,0 ,0,0,1, 0,0,-1 }; int ok; in

POJ 3126 Prime Path(BFS)

Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12060   Accepted: 6843 Description The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-dig

POJ 3216 Prime Path (BFS)

The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices. — It is a matter of security to change such things every now and then, to

POJ 1753 Flip Game (bfs)

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<queue> using namespace std; int ch[20]; int op[4][2]={1,0, -1,0, 0,1, 0,-1}; int mat[200000]; int vis[200000]; void init_ch() { int i,j,k; int