CCF 201312-5 I’m stuck!

 


问题描述
  给定一个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
样例说明
  如果把满足性质的方格在地图上用'X'标记出来的话,地图如下所示:
  --+-+
  ..|#X
  ..|##
  S-+-T
  ####X

#include <iostream>
#include <queue>
#include <vector>
///2004-2022
///2108(50)
///2115(90)-rc
///2120(100)-did not read problem description completely;
using namespace std;

int main() {
    int r,c;
    int dir[4][2]= {1,0,-1,0,0,1,0,-1};
    cin>>r>>c;
    char sco[r][c];
    vector<vector<int> >vst(r,vector<int>(c,0));
    vector<vector<int> >vst_b(r,vector<int>(c,0));
    int s_x,s_y;
    int t_x,t_y;
    for (int i=0; i<r; i++) {
        for (int j=0; j<c; j++) {
            cin>>sco[i][j];
            if (sco[i][j]=='S') {
                s_x=i;
                s_y=j;
            }
            if (sco[i][j]=='T') {
                t_x=i;
                t_y=j;
            }
        }
    }
    queue<int >qux;
    queue<int >quy;
    vst[s_x][s_y]=1;
    qux.push(s_x);
    quy.push(s_y);
    int step=1;
    while(!qux.empty()) {
        int x=qux.front();
        qux.pop();
        int y=quy.front();
        quy.pop();
        step++;
        if (sco[x][y]=='|') {
            for (int i=0; i<2; i++) {
                int t_x=x+dir[i][0];
                int t_y=y+dir[i][1];
                if (-1<t_x&&t_x<r) {
                    if (-1<t_y&&t_y<c) {
                        if (!vst[t_x][t_y]&&sco[t_x][t_y]!='#') {
                            vst[t_x][t_y]=step;
                            qux.push(t_x);
                            quy.push(t_y);
                        }
                    }
                }
            }

        } else if (sco[x][y]=='-') {
            for (int i=2; i<4; i++) {
                int t_x=x+dir[i][0];
                int t_y=y+dir[i][1];
                if (-1<t_x&&t_x<r) {
                    if (-1<t_y&&t_y<c) {
                        if (!vst[t_x][t_y]&&sco[t_x][t_y]!='#') {
                            vst[t_x][t_y]=step;
                            qux.push(t_x);
                            quy.push(t_y);
                        }
                    }
                }
            }

        } else if (sco[x][y]=='.') {
            for (int i=0; i<1; i++) {
                int t_x=x+dir[i][0];
                int t_y=y+dir[i][1];
                if (-1<t_x&&t_x<r) {
                    if (-1<t_y&&t_y<c) {
                        if (!vst[t_x][t_y]&&sco[t_x][t_y]!='#') {
                            vst[t_x][t_y]=step;
                            qux.push(t_x);
                            quy.push(t_y);
                        }
                    }
                }
            }

        } else  if (sco[x][y]!='#') {
            for (int i=0; i<4; i++) {
                int t_x=x+dir[i][0];
                int t_y=y+dir[i][1];
                if (-1<t_x&&t_x<r) {
                    if (-1<t_y&&t_y<c) {
                        if (!vst[t_x][t_y]&&sco[t_x][t_y]!='#') {
                            vst[t_x][t_y]=step;
                            qux.push(t_x);
                            quy.push(t_y);
                        }
                    }
                }
            }
        }

    }
    if (vst[t_x][t_y]==0)
    {
        cout<<"I'm stuck!"<<endl;
        return 0;
    }
//    for (int i=0; i<r; i++) {
//        for (int j=0; j<c; j++) {
//            cout<<vst[i][j]<<" ";
//        }
//        cout<<endl;
//    }

    vst_b[t_x][t_y]=1;
    qux.push(t_x);
    quy.push(t_y);
    step=1;
    while(!qux.empty()) {
        int x=qux.front();
        qux.pop();
        int y=quy.front();
        quy.pop();
        step++;

        for (int i=0; i<4; i++) {
            int t_x=x+dir[i][0];
            int t_y=y+dir[i][1];
            if (-1<t_x&&t_x<r &&-1<t_y&&t_y<c) {
                if (!vst_b[t_x][t_y]&&sco[t_x][t_y]!='#') {

                    if (sco[t_x][t_y]=='|') {
                        if (i==0||i==1) {
                            vst_b[t_x][t_y]=step;
                            qux.push(t_x);
                            quy.push(t_y);
                        }
                    } else if  (sco[t_x][t_y]=='-') {
                        if (i==2||i==3) {
                            vst_b[t_x][t_y]=step;
                            qux.push(t_x);
                            quy.push(t_y);
                        }

                    } else if  (sco[t_x][t_y]=='.') {
                        if (i==1) {
                            vst_b[t_x][t_y]=step;
                            qux.push(t_x);
                            quy.push(t_y);
                        }

                    } else {
                        vst_b[t_x][t_y]=step;
                        qux.push(t_x);
                        quy.push(t_y);
                    }

                }
            }

        }
    }
    int cnt=0;
    for (int i=0; i<r; i++) {
        for (int j=0; j<c; j++) {
            //cout<<vst_b[i][j]<<" ";
            if (vst[i][j]&&(!vst_b[i][j])) {
                cnt++;
            }
        }
     //   cout<<endl;
    }

 


    cout << cnt << endl;
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值