CCF 201312-5 I’m stuck! (暴力,BFS)

问题描述

  给定一个R行C列的地图,地图的每一个方格可能是‘#‘, ‘+‘, ‘-‘, ‘|‘, ‘.‘, ‘S‘, ‘T‘七个字符中的一个,分别表示如下意思:
  ‘#‘: 任何时候玩家都不能移动到此方格;
  ‘+‘: 当玩家到达这一方格后,下一步可以向上下左右四个方向相邻的任意一个非‘#‘方格移动一格;
  ‘-‘: 当玩家到达这一方格后,下一步可以向左右两个方向相邻的一个非‘#‘方格移动一格;
  ‘|‘: 当玩家到达这一方格后,下一步可以向上下两个方向相邻的一个非‘#‘方格移动一格;
  ‘.‘: 当玩家到达这一方格后,下一步只能向下移动一格。如果下面相邻的方格为‘#‘,则玩家不能再移动;
  ‘S‘: 玩家的初始位置,地图中只会有一个初始位置。玩家到达这一方格后,下一步可以向上下左右四个方向相邻的任意一个非‘#‘方格移动一格;
  ‘T‘: 玩家的目标位置,地图中只会有一个目标位置。玩家到达这一方格后,可以选择完成任务,也可以选择不完成任务继续移动。如果继续移动下一步可以向上下左右四个方向相邻的任意一个非‘#‘方格移动一格。
  此外,玩家不能移动出地图。
  请找出满足下面两个性质的方格个数:
  1. 玩家可以从初始位置移动到此方格;
  2. 玩家不可以从此方格移动到目标位置。

输入格式

  输入的第一行包括两个整数R 和C,分别表示地图的行和列数。(1 ≤ R, C ≤ 50)。
  接下来的R行每行都包含C个字符。它们表示地图的格子。地图上恰好有一个‘S‘和一个‘T‘。

输出格式

  如果玩家在初始位置就已经不能到达终点了,就输出“I‘m stuck!”(不含双引号)。否则的话,输出满足性质的方格的个数。

样例输入

5 5
--+-+
..|#.
..|##
S-+-T
####.

样例输出

2

析:直接用BFS进行两次暴力即可,第一次是搜索S能到哪些点,第二次是在第一次的基础上再搜这些点能不能到T。很简单。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define frer freopen("in.txt", "r", stdin)
#define frew freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 50 + 5;
const int mod = 1e9 + 7;
const int dr[] = {-1, 1, 0, 0};
const int dc[] = {0, 0, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
char s[maxn][maxn];
int vis[maxn][maxn];
int viss[maxn][maxn];

void bfs1(int x, int y){
    queue<P> q;
    q.push(P(x, y));
    vis[x][y] = 1;
    while(!q.empty()){
        P u = q.front();   q.pop();
        int x = u.first, y = u.second;
        for(int i = 0; i < 4; ++i){
            int xx = x + dr[i];
            int yy = y + dc[i];
            if(!is_in(xx, yy) || vis[xx][yy] || s[xx][yy] == ‘#‘)  continue;
            if(s[x][y] == ‘-‘ && (0 == i || 1 == i))  continue;
            if(s[x][y] == ‘|‘ && (2 == i || 3 == i))  continue;
            if(s[x][y] == ‘.‘ && (0 == i || 2 == i || 3 == i))  continue;
            vis[xx][yy] = 1;
            q.push(P(xx, yy));
        }
    }
}

bool bfs(int x, int y){
    queue<P> q;
    q.push(P(x, y));
    memset(viss, 0, sizeof viss);
    viss[x][y] = 1;
    while(!q.empty()){
        P u = q.front();   q.pop();
        int x = u.first, y = u.second;
        if(s[x][y] == ‘T‘)  return false;
        for(int i = 0; i < 4; ++i){
            int xx = x + dr[i];
            int yy = y + dc[i];
            if(!is_in(xx, yy) || s[xx][yy] == ‘#‘ || viss[xx][yy])  continue;
            if(s[x][y] == ‘-‘ && (0 == i || 1 == i))  continue;
            if(s[x][y] == ‘|‘ && (2 == i || 3 == i))  continue;
            if(s[x][y] == ‘.‘ && (0 == i || 2 == i || 3 == i))  continue;
            viss[xx][yy] = 1;
            q.push(P(xx, yy));
        }
    }
    return true;
}

int main(){
    scanf("%d %d", &n, &m);
    for(int i = 0; i < n; ++i)  scanf("%s", s[i]);
    memset(vis, 0, sizeof vis);
    int sx, sy, tx, ty;
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j){
            if(s[i][j] == ‘S‘)  sx = i, sy = j;
            else if(s[i][j] == ‘T‘)   tx = i, ty = j;
        }
    }
    bfs1(sx, sy);
    if(!vis[tx][ty]) {  puts("I‘m stuck!");  return 0; }
    int ans = 0;
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j){
            if(vis[i][j] == 1 && bfs(i, j))  ++ans;
        }
    }

    printf("%d\n", ans);
    return 0;
}
时间: 2024-08-02 23:33:43

CCF 201312-5 I’m stuck! (暴力,BFS)的相关文章

【暴力+BFS】HDU 4474 Yet Another Multiple Problem

通道:http://acm.hdu.edu.cn/showproblem.php?pid=4474 题意:给出n和m个数位,求一个数X,这个数是n的最小倍数且他的每一位都不含有m个数位中的任意一个. 思路:反过来想,其实就是有非M的元素组成一个数,这个数是N的倍数.如果存在解,那么他的第一位便是非M组合中的任意一位,然后除N后,他的余数便是X-a*n,那么下一位除N的就是(X-a*n)*10+(枚举的下一个非M元素),要求最小这个数,那么BFS跑最短就可以了. 代码:https://github

NYOJ 21 三个水杯 【暴力+BFS】

题意:不解释. 策略:广搜. 解释一下为什么会是广搜,一个水杯只能向其他两个水杯倒水,不能向他自己倒水,这样一共有三个水杯也就是有6种情况,只需要一步一步的搜索就好了(数据没多大 <100), 我们将每一次出现的水杯中的水数(就是有多少水)都标记下来,如果是以前没有出现过,那么就进队列,同时将此时的水杯数标记下来,说明该种情况已出现,一直找到想要找的状态为止,如果都找不到,就返回-1. 难点:我在下面的代码中会有详细的解释. ps:网上有说这道题是隐式图,因为BFS原来是来搜索图的,我比较认可.

poj 3126 Prime Path 【暴力BFS】

题意:给你一个4位数,再给你一个4位数,如前一个数的每次只移动一位,问你能不能将第一个数变成第二个. 转移条件:1,只能通过素数作为中转,2,每次移动一位. 如果找到输出最少的转移次数(或步数), 如果找不到输出Impossible. 策略:如题. 直接上代码: #include<stdio.h> #include<string.h> #include<queue> #define M 10005 using std::queue; int vis[10000]; in

hdu 4771 13 杭州 现场 B - Stealing Harry Potter&#39;s Precious 暴力bfs

Description Harry Potter has some precious. For example, his invisible robe, his wand and his owl. When Hogwarts school is in holiday, Harry Potter has to go back to uncle Vernon's home. But he can't bring his precious with him. As you know, uncle Ve

hdu 1195:Open the Lock(暴力BFS广搜)

mediaxyz是一位研究ffmpeg有三年的高人了,这几天一直在折腾ffmpeg中的x264,就是不知道该如何控制码率,主要是参数太多,也不知道该如何设置,在 google上search了一下,这方面的介绍为0,那就找mediaxyz请教请教吧,这些可都是经验,非常宝贵! 以下是与mediaxyz在QQ上聊天的记录,只有一部分,因为QQ把之前的谈话删除了,但基本上精髓都可这里了. mediaxyz 23:40:26你说的qsable是c->global_quality吧 Leon 23:40:

UVa 10603 Fill (暴力BFS+优先队列)

题意:给定4个数,a,b,c,d,分别代表空杯子容积为a,b,一个盛满水的杯子容积为c,让你不断倒水,找一个dd,是不是存在某个时刻, 某个杯子里的水dd,和d相同,或者无限接近.让求最少的倒水量和dd(可能和d相同). 析:首先由于没有刻度,如果用数学方法计算,不好算,样例还好算一点,我们观察那个a,b,c都不大于200,挺小的,适合暴力求解. 就是把所有情况都倒一次,倒水就两种倒法,要么把一个杯子倒满,要么就是这个杯子空了,由于水量是固定的,那么确定两个杯子的水量, 那么第三个也就确定了,所

bzoj 1295: [SCOI2009]最长距离 暴力+bfs最短路

题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=1295 题解: 对每个点暴力跑一遍bfs,看能够到达的最远位置,这里如果有障碍物则距离为1,如果没有障碍物,则距离为0,用bfs跑距离<=t的所有点并更新答案. 代码: #include<iostream> #include<cstring> #include<cstdio> #include<utility> #include<queu

I’m stuck!(BFS)

I’m stuck! 给定一个R行C列的地图,地图的每一个方格可能是'#', '+', '-', '|', '.', 'S', 'T'七个字符中的一个,分别表示如下意思: '#': 任何时候玩家都不能移动到此方格: '+': 当玩家到达这一方格后,下一步可以向上下左右四个方向相邻的任意一个非'#'方格移动一格: '-': 当玩家到达这一方格后,下一步可以向左右两个方向相邻的一个非'#'方格移动一格: '|': 当玩家到达这一方格后,下一步可以向上下两个方向相邻的一个非'#'方格移动一格: '.'

POJ 3414 Pots 暴力,bfs 难度:1

http://poj.org/problem?id=3414 记录瓶子状态,广度优先搜索即可 #include <cstdio> #include <cstring> #include <queue> using namespace std; const int maxn=101; int n,m; typedef unsigned long long ull; int A,B,C; int vis[maxn][maxn]; int ans[maxn][maxn][ma