跟普通迷宫不同的是,它这个,每个点有四个方向,所以每个方向也对应一个状态,然后按要求走下去就行
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <string>
using namespace std;
#define ll long long
#define maxn 4005
const int dx[4] = { -1, 0, 1, 0 };
const int dy[4] = { 0, 1, 0, -1 };
char grid[12][12];
bool vis[12][12][4];
bool walk[12][12];
int N, M;
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
scanf("%d%d", &N, &M);
int dir = -1, ax = -1, ay = -1;
for (int i = 0; i < N; ++i)
{
scanf("%s", grid[i]);
for (int j = 0; j < M; ++j)
{
if (grid[i][j] != '*'&&grid[i][j] != '.')
{
if (grid[i][j] == 'U')
{
dir = 0;
}
else if (grid[i][j] == 'R')
{
dir = 1;
}
else if (grid[i][j] == 'D')
{
dir = 2;
}
else
{
dir = 3;
}
ax = i; ay = j; grid[i][j] = '.';
}
}
}
int ans = 0;
int tx, ty, tdir;
int cnt;
bool cannot = false;
while (1)
{
if (walk[ax][ay] == false)
{
++ans;
walk[ax][ay] = true;
}
//printf("%d %d %d\n", ax, ay, dir);
//vis[ax][ay][dir] = true;
cnt = 0;
tdir = dir;
while (1)
{
tx = ax + dx[tdir]; ty = ay + dy[tdir];
if (tx >= 0 && tx < N&&ty >= 0 && ty < M&&grid[tx][ty] != '*')
{
break;
}
vis[ax][ay][tdir] = true;
cnt++;
if (cnt >= 4)
{
cannot = true;
break;
}
tdir++;
if (tdir >= 4)
tdir -= 4;
}
if (cannot || vis[tx][ty][tdir])
break;
ax = tx; ay = ty; dir = tdir;
}
printf("%d\n", ans);
//system("pause");
//while (1);
return 0;
}