题目描述
Given a maze of size n×mn\times mn×m, where upper left corner is (1,1)(1, 1){}(1,1) and lower right corner is (n,m)(n, m){}(n,m). For each cell (x,y)(x, y)_{}(x,y), there is exactly one character c (c∈{W,A,S,D})c~(c\in {W, A, S, D})c (c∈{W,A,S,D}) on it, denoting the moving restriction:
-
If c=Wc = W_{}c=W, you can only go up from this cell, specifically go from (x,y)(x, y){}(x,y) to (x−1,y)(x-1, y){}(x−1,y)
-
If c=Ac = A_{}c=A, you can only go left from this cell, specifically go from (x,y)(x, y){}(x,y) to (x,y−1)(x, y-1){}(x,y−1)
-
If c=Sc = S_{}c=S, you can only go down from this cell, specifically go from (x,y)(x, y){}(x,y) to (x+1,y)(x+1, y){}(x+1,y)
-
If c=Dc = D_{}c=D, you can only go right from this cell, specifically go from (x,y)(x, y){}(x,y) to (x,y+1)(x, y+1){}(x,y+1)
We say one is out if x<1x < 1_{}x<1 or x>nx > n_{}x>n or y<1y < 1_{}y<1 or y>my > m_{}y>m holds, now you should determine the number of cells that one can be out if start walking on it.
输入描述:
The first line contains two integers n,m (1≤n,m≤1000)n,m~(1\le n,m \le 1000)n,m (1≤n,m≤1000), denoting the size of the maze.
Following nn_{}n lines each contains a string of length mm_{}m, where the jj_{}j-th character in ii_{}i-th line si,j (si,j∈{W,A,S,D})s_{i,j}~(s_{i,j} \in {W, A, S, D})si,j (si,j∈{W,A,S,D}) denotes the character on cell (i,j)(i, j)_{}(i,j).
输出描述:
Only one line containing one integer, denoting the number of cells that can make one out.
示例1
输入
复制
3 4
DDSD
AWAA
WASD
输出
复制
6
说明
The 6 cells are (1,4),(2,1),(3,1),(3,2),(3,3),(3,4)(1, 4), (2, 1), (3, 1), (3, 2), (3, 3), (3, 4)_{}(1,4),(2,1),(3,1),(3,2),(3,3),(3,4) respectively.
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 1e3 + 10;
struct point{
int x, y;
};
struct node{
int x, y;
int step;
vector<point>v;
};
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
char a[maxn][maxn];
bool vis[maxn][maxn];
bool suc[maxn][maxn];
int n, m;
int ans;
void bfs(int x, int y) {
queue<node>que;
node cur;
cur.x = x, cur.y = y;
cur.step = 1;
vis[x][y] = true;
point p;
p.x = x;
p.y = y;
cur.v.push_back(p);
que.push(cur);
while(!que.empty()) {
cur = que.front();
que.pop();
int dir = 0;
int nx = cur.x, ny = cur.y;
if(a[nx][ny] == 'W') dir = 3;
else if(a[nx][ny] == 'S') dir = 1;
else if(a[nx][ny] == 'A') dir = 2;
else dir = 0;
nx += dx[dir];
ny += dy[dir];
if(nx < 1 || ny < 1 || nx > n || ny > m || suc[nx][ny] == true) {
ans += cur.step;
for(int k = 0; k < cur.v.size(); k++) {
point pi = cur.v[k];
suc[pi.x][pi.y] = true;
}
continue;
}
if(vis[nx][ny] == true) continue;
vis[nx][ny] = true;
p.x = nx, p.y = ny;
node nex;
nex.x = nx;
nex.y = ny;
nex.v = cur.v;
nex.v.push_back(p);
nex.step = cur.step + 1;
que.push(nex);
}
}
int main() {
cin >> n >> m;
for(int i = 1; i <= n; i++) {
scanf("%s", a[i] + 1);
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(vis[i][j] == 0)
bfs(i, j);
}
}
printf("%d", ans);
return 0;
}