直接模拟一波,看到数据比较小就不用记忆化搜索了,每次搜索直接暴力,然后判断是否兜圈子,用一个sum记录步数,sum>200还没走完则为兜圈子。
Code:
#include<iostream>
#include<map>
#define pii pair<int,int>
typedef long long ll;
using namespace std;
map<char, pii> ma;
ll sum = 0, s = 0;
char lst[105][105]=
{
{'U','D','D','L','U','U','L','R','U','L'},
{'U','U','R','L','L','L','R','R','R','U'},
{'R','R','U','U','R','L','D','L','R','D'},
{'R','U','D','D','D','D','U','U','U','U'},
{'U','R','U','D','L','L','R','R','U','U'},
{'D','U','R','L','R','L','D','L','R','L'},
{'U','L','L','U','R','L','L','R','D','U'},
{'R','D','L','U','L','L','R','D','D','D'},
{'U','U','D','D','U','D','U','D','L','L'},
{'U','L','R','D','L','U','U','R','R','R'},
};
ll ans = 0;
void dfs(int x,int y)
{
if (x < 0 || x>9 || y < 0 || y>9) {
ans++;return;
}
sum++;
if (sum >= 200)return;
x += ma[lst[x][y]].first;
y += ma[lst[x][y]].second;
dfs(x, y);
}
int main()
{
ma['U'] = make_pair(-1, 0);
ma['D'] = make_pair(1, 0);
ma['L'] = make_pair(0, -1);
ma['R'] = make_pair(0, 1);
for(int i=0;i<=9;i++)
for (int j = 0;j <= 9;j++)
{
sum = 0;
dfs(i, j);
}
cout << ans;
}