using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
namespace CodeS
{
class Program_3
{
static List<List<int>> graph = new List<List<int>>();
static List<List<List<int>>> dp = new List<List<List<int>>>();
// graph 图数据
// dp 动态规划
// t 步数
// x,y 警察 小偷位置
static int Search(ref List<List<int>> graph, ref List<List<List<int>>> dp, int t, int x, int y)
{
if (t == graph.Count * 2)
return 0;
if (x == y)
return dp[t][x][y] = 2;
if (x == 0)
return dp[t][x][y] = 1;
if (dp[t][x][y] != -1)
return dp[t][x][y];
if (t % 2 == 0)
{
bool police = true;
for (int i = 0; i < graph[x].Count; i ++)
{
int ret = Search(ref graph, ref dp, t + 1, graph[x][i], y);
if (ret == 1)
return dp[t][x][y] = 1;
else if (ret != 2)
police = false;
}
if (police)
return dp[t][x][y] = 2;
else
return dp[t][x][y] = 0;
}
else
{
bool thief = true;
for (int i = 0; i < graph[y].Count; i ++)
{
if (graph[y][i] == 0)
continue;
int ret = Search(ref graph, ref dp, t + 1, x, graph[y][i]);
if (ret == 2)
return dp[t][x][y] = 2;
else if (ret != 1)
thief = false;
}
if (thief)
return dp[t][x][y] = 1;
else
return dp[t][x][y] = 0;
}
}
static void Main(string[] args)
{
List<int> list;
list = new List<int>(){4};
graph.Add(list);
list = new List<int>(){4,5};
graph.Add(list);
list = new List<int>(){3};
graph.Add(list);
list = new List<int>(){2,4,5};
graph.Add(list);
list = new List<int>(){0,1,3};
graph.Add(list);
list = new List<int>(){1,3};
graph.Add(list);
int count = graph.Count;
for(int i = 0; i < count*2; i ++)
{
List<List<int>> _list = new List<List<int>>();
for(int j = 0; j < count; j ++)
{
list = new List<int>();
for(int m = 0; m < count; m ++)
{
list.Add(-1);
}
_list.Add(list);
}
dp.Add(_list);
}
int ret = Search(ref graph, ref dp, 0, 1, 2);
Console.WriteLine(ret);
}
}
}
LeetCode 913
最新推荐文章于 2022-01-04 18:04:41 发布