迷宫(一)(DFS & BFS)

//新生训练

cpp 复制代码
#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,练习了一些入门题目;

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

相关推荐
lueluelue472 小时前
LeetCode:链表
算法·leetcode·链表
橘子汽水1685 小时前
Leetcode 23,543合并K个升序链表,二叉树的直径
算法·leetcode·链表
huameinan狮子5 小时前
Adaboost算法原理与计算实例
算法
Tri_Function5 小时前
动态规划DP1(c++)
c++
别怪我很水5 小时前
API中提供了VelocityTracker类用于计算触摸事件MotionEvent的速度,而其内部默认使用的方法就是最小二乘法,本 ...
算法·gitee·最小二乘法
清水迎朝阳8 小时前
客户端软件 — 用户统计方案
服务器·c++·客户端·用户统计
Re.不晚8 小时前
挑战做100道力扣算法- DAY1
算法·leetcode·职场和发展
蓝悦无人机8 小时前
《Planning algorithms》读书笔记——第1章 引言
算法·读书笔记·规划算法·lavalle
Sagittarius_A*9 小时前
分组密码基础(二):Feistel 结构与 DES 的设计思想
算法·信息安全·密码学·des·数论