3344 迷宫

 时间限制: 1 s
 空间限制: 32000 KB
 题目等级 : 黄金 Gold
 
 
 
题目描述 Description

小刚在迷宫内,他需要从A点出发,按顺序经过B,C,D……,到达最后一个点,再回到A点。迷宫内有些障碍,问至少走几步。

输入描述 Input Description

第一行有三个数n,m表示迷宫有n行,m列。

第2行到第n+1行,每行m个字符,可能是’A’..’Z’,’2’,’0’ 其中,2表示障碍,0表示可以走。’A’..’Z’也可以走。

输出描述 Output Description

至少走几步可以按规定走完,如果不行,输出“Impossible”

样例输入 Sample Input

5 5

A002B

022C0

000D0

00222

0000E

样例输出 Sample Output

26

数据范围及提示 Data Size & Hint

0%的数据满足:1<=n<=10 1<=m<=10 字母为“A”..“B”。

30%的数据满足:1<=n<=10 1<=m<=10 字母为“A”..“G”。

50%的数据满足:1<=n<=10 1<=m<=10 字母为“A”..“Z”。

10%的数据满足:1<=n<=100 1<=m<=100 字母为“A”..“B”。

30%的数据满足:1<=n<=100 1<=m<=100 字母为“A”..“G”。

100%的数据满足:1<=n<=100 1<=m<=100 字母为“A”..“Z”。

bfs

以A为起点以B为终点,以B为起点以C为终点、、、以此类推,跑sum遍bfs找出每一个点到它下一个点的最小值,然后累加就可以了。

不要忘了判断没有路的情况

#include<queue>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define N 210
using namespace std;
char ch;
bool flag;
int dis[N][N],map[N][N];
int n,m,sx,sy,ex,ey,sum,ans;
]={,,,-},yy[]={,-,,};
int read()
{
    ,f=; char ch=getchar();
    ') ch=getchar();
    +ch-'; ch=getchar();}
    return x*f;
}
struct Node
{
    int x,y;
}node[],que;
queue<Node>q;
void bfs(int sx,int sy)
{
    while(!q.empty()) q.pop();
     memset(dis,0x3f3f3f3f,sizeof(dis));
    dis[sx][sy]=;
    que.x=sx,que.y=sy,q.push(que);
    while(!q.empty())
    {
        Node p=q.front();q.pop();
        if(p.x==ex&&p.y==ey) {ans+=dis[ex][ey]; return ;}
        ;i<;i++)
        {
            int fx=p.x+xx[i],fy=p.y+yy[i];
            ||fy<||fx>n||fy>m||!map[fx][fy]) continue;
            ) continue;
            dis[fx][fy]=dis[p.x][p.y]+;
            que.x=fx,que.y=fy;
            q.push(que);
        }
    }
    flag=true;
}
int main()
{
    n=read(),m=read();
    ;i<=n;i++)
     ;j<=m;j++)
     {
         cin>>ch;
         ;
         ;
         if(ch>='A'&&ch<='Z')
         {
             sx=ch-;
             sum=max(sx,sum);
             node[sx].x=i;
             node[sx].y=j;
         }
      }
    ;i<=sum;i++)
    {
        sx=node[i].x,sy=node[i].y;
        if(i==sum)
         ex=node[].x,ey=node[].y;
        else
         ex=node[i+].x,ey=node[i+].y;
        bfs(sx,sy);
    }
    if(flag) printf("Impossible");
    else printf("%d",ans);
    ;
}

codevs——3344 迷宫的更多相关文章

  1. codevs 3344 迷宫

    时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold 题目描述 Description 小刚在迷宫内,他需要从A点出发,按顺序经过B,C,D……,到达最后一个点,再回到A ...

  2. codevs 1215 迷宫

    时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 已知 n 个整数 x1,x2,-,xn,以及一个整数 k(k<n).从 n ...

  3. BFS简单题套路_Codevs 1215 迷宫

    BFS 简单题套路 1. 遇到迷宫之类的简单题,有什么行走方向的,先写下面的 声明 ; struct Status { int r, c; Status(, ) : r(r), c(c) {} // ...

  4. Codevs 1337 银行里的迷宫

    1337 银行里的迷宫 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 传送门 题目描述 Description 楚楚每一次都在你的帮助下过了一关又一关(比如他开 ...

  5. Codevs 2855 游乐园的迷宫

    2855 游乐园的迷宫  时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 迷宫可是每个游乐园必不可少的项目,菜菜当然是要尝试一下啦. ...

  6. codevs——T1337 银行里的迷宫

     时间限制: 1 s  空间限制: 128000 KB  题目等级 : 白银 Silver 题解       题目描述 Description 楚楚每一次都在你的帮助下过了一关又一关(比如他开宴会). ...

  7. Codevs 1629 01迷宫

    1629 01迷宫 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 有一个由01组成的n*n格迷宫,若你位于一格0上,那么你可 ...

  8. 习题:codevs 2822 爱在心中 解题报告

    这次的解题报告是有关tarjan算法的一道思维量比较大的题目(真的是原创文章,希望管理员不要再把文章移出首页). 这道题蒟蒻以前做过,但是今天由于要复习tarjan算法,于是就看到codevs分类强联 ...

  9. codevs 2822 爱在心中

    codevs 2822 爱在心中  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description “每个人都拥有一个梦,即使彼此不相同, ...

随机推荐

  1. paper:synthesizable finite state machine design techniques using the new systemverilog 3.0 enhancements 之 FSM Coding Goals

    1.the fsm coding style should be easily modifiable to change state encoding and FSM styles. FSM 的的 状 ...

  2. 解决 viewer.js 动态更新图片导致无法预览的问题

    1.viewer.js 使用 Demo http://fengyuanchen.github.io/viewerjs/ 2.viewer.js 下载地址 https://github.com/feng ...

  3. LeetCode(225) Implement Stack using Queues

    题目 Implement the following operations of a stack using queues. push(x) – Push element x onto stack. ...

  4. POJ:1961-Period(寻找字符串循环节)

    Period Time Limit: 3000MS Memory Limit: 30000K Description For each prefix of a given string S with ...

  5. Leetcode 559. N叉树的最大深度

    题目链接 https://leetcode-cn.com/problems/maximum-depth-of-n-ary-tree/description/ 题目描述 给定一个N叉树,找到其最大深度. ...

  6. ubuntu检测到系统错误解决方法

    解决方案: 1.打开终端,输入 sudo gedit /etc/default/apport 2.把里面的enabled=1改成enabled=0,保存

  7. [转]how to inserting multiple rows in one step

    To insert multiple rows in the table use executemany() method of cursor object. Syntax: cursor_objec ...

  8. [转]廖雪峰:datetime用法

    datetime是Python处理日期和时间的标准库. 获取当前日期和时间 我们先看如何获取当前日期和时间: >>> from datetime import datetime &g ...

  9. maven项目打包jar,含有依赖jar

    在pom文件中添加一下插件 <plugin> <artifactId>maven-assembly-plugin</artifactId> <configur ...

  10. IDE介绍以及Eclipse的简单使用

    一.IDE介绍 ①   什么是IDE? 1.  IDE全称是”Integrated Development Environment”,中文称为“集成开发环境”; 2.是用于提供程序开发环境的应用程序, ...