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