KamaCoder 110. 字符串接龙
solution:
class Program
{
// BFS方法
public static int LadderLength(string beginWord, string endWord, List<string> wordList)
{
// 使用HashSet作为查询容器,效率更高
HashSet<string> set = new HashSet<string>(wordList);
// 声明一个Queue存储每次变更一个字符得到的且存在于容器中的新字符串
Queue<string> queue = new Queue<string>();
// 声明一个Dictionary存储遍历到的字符串以及所走过的路径path
Dictionary<string, int> visitMap = new Dictionary<string, int>();
queue.Enqueue(beginWord);
visitMap[beginWord] = 1;
while (queue.Count > 0)
{
string curWord = queue.Dequeue();
int path = visitMap[curWord];
for (int i = 0; i < curWord.Length; i++)
{
char[] ch = curWord.ToCharArray();
// 每个位置尝试26个字母
for (char k = 'a'; k <= 'z'; k++)
{
ch[i] = k;
string newWord = new string(ch);
if (newWord == endWord) return path + 1;
// 如果这个新字符串存在于容器且之前未被访问到
if (set.Contains(newWord) && !visitMap.ContainsKey(newWord))
{
visitMap[newWord] = path + 1;
queue.Enqueue(newWord);
}
}
}
}
return 0;
}
static void Main(string[] args)
{
// 接收输入
string[] dimensions = Console.ReadLine().Split();
int N = int.Parse(dimensions[0]);
string[] strs = Console.ReadLine().Split(' ');
List<string> wordList = new List<string>();
for (int i = 0; i < N; i++)
{
wordList.Add(Console.ReadLine());
}
// 打印结果
int result = LadderLength(strs[0], strs[1], wordList);
Console.WriteLine(result);
}
}
KamaCoder 105. 有向图的完全可达性
题目:105. 有向图的完全可达性 (kamacoder.com)
题解:代码随想录 (programmercarl.com)
solution:
class Program
{
// 深度优先搜索 (DFS) 方法
static void Dfs(List<List<int>> graph, int key, bool[] visited)
{
if (visited[key])
{
return;
}
visited[key] = true;
foreach (int adjacent in graph[key])
{
// 深度优先搜索遍历
Dfs(graph, adjacent, visited);
}
}
static void Main()
{
// 读取输入
string[] input = Console.ReadLine().Split();
int n = int.Parse(input[0]);
int m = int.Parse(input[1]);
// 节点编号从0到n-1,所以申请 n 这么大的数组
List<List<int>> graph = new List<List<int>>(n);
for (int i = 0; i < n; i++)
{
graph.Add(new List<int>());
}
// 读取边并构建邻接表
for (int i = 0; i < m; i++)
{
input = Console.ReadLine().Split();
int s = int.Parse(input[0]);
int t = int.Parse(input[1]);
graph[s].Add(t);
}
bool[] visited = new bool[n];
Dfs(graph, 0, visited);
// 检查是否都访问到了
int count = 0;
for (int i = 0; i < n; i++)
{
if (visited[i])
{
count++;
}
}
Console.WriteLine(count);
}
}
KamaCoder 106. 岛屿的周长
solution:
class Program
{
static void Main()
{
// 读取输入
string[] firstLine = Console.ReadLine().Split();
int n = int.Parse(firstLine[0]);
int m = int.Parse(firstLine[1]);
// 创建并填充矩阵
int[,] grid = new int[n, m];
for (int i = 0; i < n; i++)
{
string[] row = Console.ReadLine().Split();
for (int j = 0; j < m; j++)
{
grid[i, j] = int.Parse(row[j]);
}
}
// 方向数组,上下左右
int[,] direction = { { 0, 1 }, { 1, 0 }, { -1, 0 }, { 0, -1 } };
int result = 0;
// 遍历每个点
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (grid[i, j] == 1)
{
// 检查四个方向
for (int k = 0; k < 4; k++)
{
int x = i + direction[k, 0];
int y = j + direction[k, 1];
if (x < 0 || x >= n || y < 0 || y >= m || grid[x, y] == 0)
{
result++;
}
}
}
}
}
// 输出结果
Console.WriteLine(result);
}
}