迷宫(一)(DFS & BFS)

//新生训练

#include <bits/stdc++.h>
using namespace std;
int n, m;
bool f;
char mp[15][15];
int vis[15][15];
int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
bool in(int x, int y)
{
	return 0 <= x && x < n && 0 <= y && y < m;
}
void dfs(int x, int y)
{
	if (mp[x][y] == 'T')
	{
		f = true;
		return;
	}
	if (!in(x, y) || mp[x][y] == '*' || vis[x][y])
	{
		return;
	}
	vis[x][y] = 1;
	for (int i = 0; i < 4; i++)
	{
		int tx = x + dir[i][0];
		int ty = y + dir[i][1];
		dfs(tx, ty);
	}
	return;
}
int main()
{
	cin >> n >> m ;
	for (int i = 0; i < n; i++)
	{
		cin >> mp[i];
	}
	int x, y;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			if (mp[i][j] == 'S')
			{
				x = i;
				y = j;
			}
		}
	}
	dfs(x, y);
	if (f)
	{
		cout << "yes" << endl;
	}
	else
	{
		cout << "no" << endl;
	}
	return 0;
}

 //笔者新学的DFS和BFS,练习了一些入门题目;

~~~//仅当笔者个人备忘录使用。

相关推荐

  1. CCF-走迷宫(bfs)

    2024-03-24 07:08:04       57 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-24 07:08:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-24 07:08:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-24 07:08:04       82 阅读
  4. Python语言-面向对象

    2024-03-24 07:08:04       91 阅读

热门阅读

  1. ORACLE 知识整理

    2024-03-24 07:08:04       35 阅读
  2. TensorFlow 的基本概念和使用场景

    2024-03-24 07:08:04       36 阅读
  3. C语言中的static关键字

    2024-03-24 07:08:04       44 阅读
  4. 二进制源码部署mysql8.0.35

    2024-03-24 07:08:04       31 阅读
  5. node.js中常用的命令及示例

    2024-03-24 07:08:04       36 阅读
  6. Node.js 常用命令

    2024-03-24 07:08:04       41 阅读
  7. 算法提高篇基础算法第一章 - 贪心算法

    2024-03-24 07:08:04       34 阅读