题意:

从左上角跳到右下角最少需要多少步,跳的规则为:可以向四个方向的任意一个方向跳当前格子中的步数,若跳不到右下角输出IMPOSSIBLE。

题解:

BFS搜索,注意判断边界,标记。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <stdio.h>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#include <cstdlib>
#include <utility>
using namespace std;
#define is_lower(c) (c>='a' && c<='z')
#define is_upper(c) (c>='A' && c<='Z')
#define is_alpha(c) (is_lower(c) || is_upper(c))
#define is_digit(c) (c>='0' && c<='9')
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define IO ios::sync_with_stdio(0);\
    cin.tie();\
    cout.tie();
#define For(i,a,b) for(int i = a; i <= b; i++)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<int,int > pll;
typedef vector<int> vi;
const ll inf=0x3f3f3f3f;
;
const ll inf_ll=(ll)1e18;
const ll maxn=100005LL;
const ll mod=1000000007LL;
+;
pair <int,int> p;
queue <pii> q;
int mp[N][N];
int xx[N][N];
bool vis[N][N];
int main() {
    int m,n;
    cin >> m >> n;
    For(i,,m)
        For(j,,n)
            scanf("%1d",&mp[i][j]);
    p.first = ;p.second = ;
    q.push(p);
    vis[][] = ;
    ;
    while(!q.empty()){
        pii x = q.front();
        q.pop();
        int step = mp[x.first][x.second];
        pii x1;
        &&(x.first+step)<=m&&!vis[x.first+step][x.second]){
            x1.first = x.first+step,x1.second = x.second;
            q.push(x1);
            xx[x1.first][x1.second] = xx[x.first][x.second]+;
            vis[x1.first][x1.second] = ;
        }//向下
        &&(x.first-step)<=m&&!vis[x.first-step][x.second]){
            x1.first = x.first-step,x1.second = x.second;
            q.push(x1);
            xx[x1.first][x1.second] = xx[x.first][x.second]+;
            vis[x1.first][x1.second] = ;
        }// up
        &&(x.second+step)<=n&&!vis[x.first][x.second+step]){
            x1.first = x.first,x1.second = x.second+step;
            q.push(x1);
            xx[x1.first][x1.second] = xx[x.first][x.second]+;
            vis[x1.first][x1.second] = ;
        }
        &&(x.second-step)<=n&&!vis[x.first][x.second-step]){
            x1.first = x.first,x1.second = x.second-step;
            q.push(x1);
            xx[x1.first][x1.second] = xx[x.first][x.second]+;
            vis[x1.first][x1.second] = ;
        }
        )
            break;
    }
    )
        cout << xx[m][n] <<endl;
    else
        cout <<"IMPOSSIBLE" << endl;
    ;
}

CF 某套题 O :Grid (简单BFS)的更多相关文章

  1. 第46套题【STL】【贪心】【递推】【BFS 图】

    已经有四套题没有写博客了.今天改的比较快,就有时间写.今天这套题是用的图片的形式,传上来不好看,就自己描述吧. 第一题:单词分类 题目大意:有n个单词(n<=10000),如果两个单词中每个字母 ...

  2. Educational Codeforces Round 15 套题

    这套题最后一题不会,然后先放一下,最后一题应该是大数据结构题 A:求连续最长严格递增的的串,O(n)简单dp #include <cstdio> #include <cstdlib& ...

  3. 【POJ 3669 Meteor Shower】简单BFS

    流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...

  4. hdu1312 Red and Black 简单BFS

    简单BFS模版题 不多说了..... 直接晒代码哦.... #include<cstdlib> #include<iostream> #include<cstdio> ...

  5. 【套题】qbxt国庆刷题班D1

    Day1 事实上D1的题目还是比较简单的= =然而D1T2爆炸了就十分尴尬--错失一波键盘 看题 T1 传送门 Description 现在你手里有一个计算器,上面显示了一个数\(S\),这个计算器十 ...

  6. LightOJ 1012 简单bfs,水

    1.LightOJ 1012  Guilty Prince  简单bfs 2.总结:水 题意:迷宫,求有多少位置可去 #include<iostream> #include<cstr ...

  7. CF 628C --- Bear and String Distance --- 简单贪心

    CF 628C 题目大意:给定一个长度为n(n < 10^5)的只含小写字母的字符串,以及一个数d,定义字符的dis--dis(ch1, ch2)为两个字符之差, 两个串的dis为各个位置上字符 ...

  8. POJ3185(简单BFS,主要做测试使用)

    没事做水了一道POJ的简单BFS的题目 这道题的数据范围是20,所以状态总数就是(1<<20) 第一次提交使用STL的queue,并且是在队首判断是否达到终点,达到终点就退出,超时:(其实 ...

  9. Moscow Pre-Finals Workshop 2016. Japanese School OI Team Selection. 套题详细解题报告

    写在前面 谨以此篇题解致敬出题人! 真的期盼国内也能多出现一些这样质量的比赛啊.9道题中,没有一道凑数的题目,更没有码农题,任何一题拿出来都是为数不多的好题.可以说是这一年打过的题目质量最棒的五场比赛 ...

随机推荐

  1. HDU1522 稳定婚姻匹配 模板

    Marriage is Stable Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  2. iPhoneX页面安全区域与内容重叠问题

    转载自:https://www.cnblogs.com/lolDragon/p/7795174.html 1.  iPhoneX的介绍 屏幕尺寸 我们熟知的iPhone系列开发尺寸概要如下: △ iP ...

  3. 数据结构&字符串:字典树

    前缀树里面可以存一堆字符串,也可以说是一堆单词,存完之后我们可以轻松判断一个指定的字符串是否出现过 下面我来详细解释一下实现细节 *+; //单词个数*每一个单词的字符数 ; struct Trie ...

  4. java -classpath or -cp 的设置和解释

    classpath is a parameter—set either on the command-line, or through an environment variable—that tel ...

  5. 【BZOJ3998】弦论 [SAM]

    弦论 Time Limit: 10 Sec  Memory Limit: 256 MB[Submit][Status][Discuss] Description 对于一个给定长度为N的字符串,求它的第 ...

  6. performSelector支持多参数

    默认的performSelector支持最多传递两个参数,要想传递超过两个的参数,需要使用NSInvocation来模拟performSelector的行为,如下: - (id)performSele ...

  7. Spring boot 集成Dubbox(山东数漫江湖)

    前言 因为工作原因,需要在项目中集成dubbo,所以去查询dubbo相关文档,发现dubbo目前已经不更新了,所以把目光投向了dubbox,dubbox是当当网基于dubbo二次开发的一个项目,dub ...

  8. el-date-picker 日期格式化 yyyy-MM-dd

    <el-date-picker format="yyyy-MM-dd" v-model="dateValue" type="date" ...

  9. poj 1797

    2013-09-08 09:48 最大生成树,输出生成树中最短的边儿即可 或者对边儿排序,二份答案+BFS判断是否1连通N 时间复杂度都是O(NlogN)的 附最大生成树pascal代码 //By B ...

  10. tornado 学习之GET POST方法 -- (转)

    import torndb import tornado.web import tornado.ioloop from tornado.options import define,options,pa ...