7.9广度优先搜索进阶总结
课堂练习
T628045 迷宫问题
题目描述
定义一个二维数组:
int maze\[5\]\[5\] = {
0, 1, 0, 0, 0,
0, 1, 0, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};
它表示一个迷宫,其中的
输入格式
一个
输出格式
左上角到右下角的最短路径。
正确思路
- 用一个pre数组来记录每个位置是从哪来的,只需要插入一句
pre[nx][ny] = {x, y}; - 如果到了终点,就递归输出
pre 数组,但要注意的是这个题目的下标是从0 开始的
代码如下:
void print(int x, int y) {
if (x == 1 && y == 1) {
cout << "(0, 0)" << endl;
return;
}
else {
print(pre[x][y].x, pre[x][y].y);
cout << "(" << x - 1 << ", " << y - 1 << ")" << endl;
}
}
正确代码
#include <bits/stdc++.h>
using namespace std;
int n, m, ans;
const int N = 1007;
char a[25][25];
bool vis[1007][1007];
struct node {
int x, y;
}pre[25][25];
int dx[] = {1, 0};
int dy[] = {0, 1};
void print(int x, int y) {
if (x == 1 && y == 1) {
cout << "(0, 0)" << endl;
return;
}
else {
print(pre[x][y].x, pre[x][y].y);
cout << "(" << x - 1 << ", " << y - 1 << ")" << endl;
}
}
void dfs(int x, int y) {
if (x == 5 && y == 5) print(x, y);
for (int i = 0; i < 2; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 1 && nx <= 5 && ny >= 1 && ny <= 5 && vis[nx][ny] == false && a[nx][ny] == '0') {
pre[nx][ny] = {x, y};
vis[nx][ny] = 1;
dfs(nx, ny);
}
}
}
int main() {
for (int i = 1; i <= 5; i++)
for (int j = 1; j <= 5; j++) cin >> a[i][j];
vis[1][1] = 1;
dfs(1, 1);
return 0;
}
P1379 八数码难题【普及+/提高】
题目描述
在
输入格式
输入初始状态,一行九个数字,空格用
输出格式
只有一行,该行只有一个数字,表示从初始状态到目标状态需要的最少移动次数。保证测试数据中无特殊无法到达目标状态数据。
输入输出样例 #1
输入 #1
283104765
输出 #1
4
说明/提示
样例解释
图中标有
并且可以证明,不存在更优的策略。
正确思路
- 可以把
3 * 3 的二维数组转换成一个长度为9 字符串 - 由于在
3 * 3 的棋盘里一个位置的上面那一个在字符串里就是这个位置的前三个,下面那一个在字符串里就是这个位置的后三个,左面那一个在字符串里就是这个位置的前一个,右面那一个在字符串里就是这个位置的后一个。因此方向数组的定义为int dx[] = {-3, 3, -1, 1}; - 注意!!!如果dx[i] == 1, 那原下标为2、5、8都无法达下一个。如果dx[i] == -1, 那原下标为3、6都无法达下一个。
所以bfs的伪代码如下:
string st, ed = "123804765";//起点和终点
struct node {
string s;
int x, step;//x:0所在的位置
};
map<string, bool> mp;
void bfs() {
queue<node> q;
//起点入队
//标记起点
while (q.size()) {
//取队首, 队首出队
//判断队首是不是ed
//找字符串中'0'在哪里
//四个方向
for (int i = 0; i < 4; i++) {
string ns = cur.s; int nx = xx + dx[i];
if (dx[i] == 1 && (xx == 2 || xx == 5 || xx == 8)) continue;
if (dx[i] == -1 && (xx == 0 || xx == 3 || xx == 6)) continue;
if (nx < 0 || nx >= 9) continue;
swap(ns[xx], ns[nx]);
if(vis[ns] == true) continue;
//标记,入队
}
}
}
正确代码
#include<bits/stdc++.h>
using namespace std;
const int N = 1007;
int dx[] = {-3, 3, -1, 1};
struct node {
string s;
int x, step;
};
string st, ed = "123804765";
queue<node> q;
map<string, bool> vis;
int bfs(string a) {
node tmp = {a, 0, 0};
vis[a] = true;
q.push(tmp);
while (!q.empty()) {
node cur = q.front();
q.pop();
if (cur.s == ed) return cur.step;
int xx; string ss = cur.s;
for (int i = 0; i < 9; i++) if (ss[i] == '0') xx = i;
for (int i = 0; i <= 3; i++) {
string ns = cur.s; int nx = xx + dx[i];
if (dx[i] == 1 && (xx == 2 || xx == 5 || xx == 8)) continue;
if (dx[i] == -1 && (xx == 0 || xx == 3 || xx == 6)) continue;
if (nx < 0 || nx >= 9) continue;
swap(ns[xx], ns[nx]);
if(vis[ns] == true) continue;
node nxt = {ns, nx, cur.step + 1};
vis[ns] = true;
q.push(nxt);
}
}
return 0;
}
int main() {
cin >> st;
cout << bfs(st);
return 0;
}
P2730 [USACO3.2] 魔板 Magic Squares【普及+/提高】
题目背景
在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板。这是一张有
题目描述
我们知道魔板的每一个方格都有一种颜色。这
这里提供三种基本操作,分别用大写字母
对于每种可能的状态,这三种基本操作都可以使用。
你要编程计算用最少的基本操作完成基本状态到目标状态的转换,输出基本操作序列。
输入格式
只有一行,包括
输出格式
第一行包括一个整数,表示最短操作序列的长度。
第二行在字典序中最早出现的操作序列,用字符串表示,除最后一行外,每行输出
输入输出样例 #1
输入 #1
2 6 8 4 5 7 3 1
输出 #1
7
BCABCCB
正确思路
-
注意!!!题目是以
S 形的路线给出的数字,假如给出的数字是2 6 8 4 5 7 3 1那么变成两行就是这样:第一行是2 6 8 4,第二行是1 3 7 5 -
注意!!!这个题目起点是固定的是
"12345678" ,而题目给出的是终点,不然你就会和我一样卡好久 -
直接用
swap 模拟A、B、C 三种操作
A操作代码如下:
swap(ns[0], ns[7]);
swap(ns[1], ns[6]);
swap(ns[2], ns[5]);
swap(ns[3], ns[4]);
B操作代码如下:
swap(ns[3], ns[0]);
swap(ns[3], ns[2]);
swap(ns[2], ns[1]);
swap(ns[7], ns[4]);
swap(ns[4], ns[5]);
swap(ns[5], ns[6]);
C操作代码如下:
swap(ns[1], ns[2]);
swap(ns[1], ns[6]);
swap(ns[6], ns[5]);
4.bfs伪代码如下:
int dx[] = {-3, 3, -1, 1};
struct node {
string s, ans;
int step;
};
int a[15];
string st = "12345678", ed = "";
queue<node> q;
map<string, bool> vis;
void bfs(string a) {
//起点入队
//标记起点
while (!q.empty()) {
//取队首, 队首出队
//判断队首是不是ed
//三种操作
for (int i = 0; i <= 2; i++) {
string ns = cur.s;
if (i == 0) {
//操作A
}
if (i == 1) {
//操作B
}
if (i == 2) {
//操作C
}
if (vis[ns] == false) {
//更新ans字符串
//标记,入队
}
}
}
cout << 0 << endl;
return ;
}
正确代码
#include<bits/stdc++.h>
using namespace std;
const int N = 1007;
int dx[] = {-3, 3, -1, 1};
struct node {
string s, ans;
int step;
};
int a[15];
string st = "12345678", ed = "";
queue<node> q;
map<string, bool> vis;
void bfs(string a) {
node tmp = {a, "", 0};
vis[a] = true;
q.push(tmp);
while (!q.empty()) {
node cur = q.front();
q.pop();
if (cur.s == ed) {
cout << cur.step << endl; cout << cur.ans << endl;
return ;
}
for (int i = 0; i <= 2; i++) {
string ns = cur.s;
if (i == 0) {
swap(ns[0], ns[7]);
swap(ns[1], ns[6]);
swap(ns[2], ns[5]);
swap(ns[3], ns[4]);
}
if (i == 1) {
swap(ns[3], ns[0]);
swap(ns[3], ns[2]);
swap(ns[2], ns[1]);
swap(ns[7], ns[4]);
swap(ns[4], ns[5]);
swap(ns[5], ns[6]);
}
if (i == 2) {
swap(ns[1], ns[2]);
swap(ns[1], ns[6]);
swap(ns[6], ns[5]);
}
if (vis[ns] == false) {
string sum = cur.ans;
if (i == 0) sum = sum + 'A';
if (i == 1) sum = sum + 'B';
if (i == 2) sum = sum + 'C';
node nxt = {ns, sum, cur.step + 1};
vis[ns] = true;
q.push(nxt);
}
}
}
cout << 0 << endl;
return ;
}
int main() {
for (int i = 1; i <= 8; i++) {char ch; cin >> ch; ed = ed + ch;}
bfs(st);
return 0;
}
T628732 矩阵距离【普及/提高−】
题目描述
给定一个
输出一个
输入格式
第一行两个整数
接下来一个
输出格式
一个
输入输出样例 #1
输入 #1
3 4
0001
0011
0110
输出 #1
3 2 1 0
2 1 0 0
1 0 0 1
说明/提示
正确思路
- 在输入的时候判断一下如果是
"1" ,就将这个位置入队,最后再搜索一次就好了 - 在搜索要边搜索边记录答案
正确代码
#include<bits/stdc++.h>
using namespace std;
const int N = 1007;
int n, m, xa, ya;
bool vis[N][N];
int ans[N][N];
char a[N][N];
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
struct node {
int x, y, step;
};
queue<node> q;
void bfs(int sx, int sy) {
node tmp = {sx, sy, 0};
vis[sx][sy] = true;
q.push(tmp);
while (!q.empty()) {
node cur = q.front();
q.pop();
ans[cur.x][cur.y] = min(ans[cur.x][cur.y], cur.step);
for (int i = 0; i <= 3; i++) {
int nx = cur.x + dx[i];
int ny = cur.y + dy[i];
if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && vis[nx][ny] == false) {
node nxt = {nx, ny, cur.step + 1};
vis[nx][ny] = true;
q.push(nxt);
}
}
}
return;
}
int main() {
cin >> n >> m;
memset(ans, 0x3f, sizeof (ans));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
if (a[i][j] == '1') {ans[i][j] = 0; q.push({i, j, 0});}
}
bfs(0, 0);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) cout << ans[i][j] << " ";
cout << endl;
}
return 0;
}
P1825 [USACO11OPEN] Corn Maze S【普及/提高−】
题目描述
去年秋天,农夫约翰带着奶牛们参观了一个玉米迷宫。但这不是一个普通的玉米迷宫:它有几个重力驱动的传送滑梯,可以让奶牛瞬间从迷宫中的一个点传送到另一个点。滑梯是双向的:奶牛可以瞬间从滑梯的起点滑到终点,或者从终点滑到起点。如果奶牛踩到滑梯的任一端,她必须使用滑梯。
玉米迷宫的外部完全由玉米包围,只有一个出口。
迷宫可以用一个
- 玉米(玉米网格元素不可通行)
- 草地(容易通过!)
- 滑梯端点(会将奶牛传送到另一个端点)
- 出口
奶牛只能从一个空间移动到相邻的下一个空间,前提是它们相邻且都不包含玉米。每个草地空间有四个潜在的邻居可以让奶牛到达。从一个草地空间移动到相邻空间需要
填满玉米的空间用井号(#)表示。草地空间用英文句号(.)表示。滑梯端点对用相同的大写字母(A 到 Z)表示,并且没有两个不同的滑梯端点用相同的字母表示。出口用等号(=)表示。
贝茜迷路了。她知道自己在网格中的位置,并用「at」符号(@)标记了她当前的草地空间。她需要的最短时间是多少才能移动到出口空间?
输入格式
第一行:两个用空格隔开的整数
第
输出格式
一个整数,表示起点到出口所需的最短时间。
输入输出样例 #1
输入 #1
5 6
###=##
#.W.##
#.####
#.@W##
######
输出 #1
3
说明/提示
例如以下矩阵,
###=##
#.W.##
#.####
#.@W##
######
唯一的一个装置的结点用大写字母
最优方案为:先向右走到装置的结点,花费一个单位时间,再到装置的另一个结点上,花费
正确思路
- 由于这道题加入了一个新的元素:传送门。因此我们需要在广搜中多增加一个特判:当前是否为传送门,是的话,我们就需要找到另一个传送门所在坐标,然后将那个传送门所在点的坐标储存进队列
- 其他的比较简单,在这里不多说
正确代码
#include <bits/stdc++.h>
using namespace std;
const int N = 350;
struct node {
int x, y, step;
};
queue<node> q;
char a[N][N];
bool vis[N][N];
int n,m;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int sx, sy;
void to(int &nx, int &ny) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i][j] == a[nx][ny] && (i != nx || j != ny)) {
nx = i; ny = j;
return;
}
}
int bfs(int sx, int sy) {
q.push({sx, sy, 0});
while (!q.empty()) {
node cur = q.front();
q.pop();
if (a[cur.x][cur.y] == '=') return cur.step;
if (a[cur.x][cur.y] >= 'A' && a[cur.x][cur.y] <= 'Z') to(cur.x,cur.y);
for (int i = 0; i <= 3; i++) {
int nx = cur.x + dx[i];
int ny = cur.y + dy[i];
if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && a[nx][ny] != '#' && vis[nx][ny] == false) {
vis[nx][ny] = true;
q.push({nx, ny, cur.step + 1});
}
}
}
return -1;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
if (a[i][j] == '@') sx = i, sy = j;
}
cout << bfs(sx, sy);
return 0;
}
家庭作业
都是一维bfs没什么好讲的
P1588 [USACO07OPEN] Catch That Cow S
题目描述
FJ 丢失了他的一头牛,他决定追回他的牛。已知 FJ 和牛在一条直线上,初始位置分别为
输入格式
第一行为一个整数
接下来每行包含一个两个正整数
输出格式
对于每组数据,输出最少步数。
输入输出样例 #1
输入 #1
1
5 17
输出 #1
4
正确代码
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e5 + 7;
int n, m, t, ans;
char a[1005][1005];
bool vis[MAXN];//标记数组
int dx[5] = {-1, 1, 0};//方向数组
struct node {
int x, step;
};
int bfs(int sx) {
queue<node> q;//创建队列
q.push({sx, 0});//加入起点信息
vis[sx] = true;//标记起点
while (!q.empty()) {
node cur = q.front();//取出队首
q.pop();//弹出
if (cur.x == m) return cur.step;//返回步数
dx[2] = cur.x;//由于2x = x + x,所以可以把dx[2]设成cur.x
for (int i = 0; i <= 2; i++) {
int nx = cur.x + dx[i];
if (nx >= 0 && nx <= MAXN && vis[nx] == false) {
node nxt = {nx, cur.step + 1};
q.push(nxt);
vis[nx] = true;
}
}
}
return -1;
}
int main() {
cin >> t;
while (t--) {
cin >> n >> m;
memset(vis, 0, sizeof (vis));
//多测不清空,爆0两行泪。注意!!!一定要清空!!!
cout << bfs(n) << endl;
}
return 0;
}
T582141 星际跃迁追踪协议
题目描述
在银河纪元2277年,侦察舰「追光者」需要回收一个失控的远古传送门核心。舰载AI测算出两者当前分别位于超空间坐标a(0≤a≤1e5)和b(0≤b≤1e5)处。舰体配备的曲速引擎支持以下跃迁模式:
- 量子滑移:从坐标 y 移动到 y-1或 y+1,消耗1个时空单元
- 超膜折叠:从坐标 y 直接折叠到2y,消耗1个时空单元
假设核心处于静默状态不会位移。请计算侦察舰达成空间重合所需的最小时空单元消耗量。
输入格式
第1行:两个整数a和b,表示初始坐标与目标坐标
输出格式
一个整数表示达成空间同步所需的最小时空单元数
输入输出样例 #1
输入 #1
1 8
输出 #1
3
输入输出样例 #2
输入 #2
10 23
输出 #2
3
输入输出样例 #3
输入 #3
9 80
输出 #3
4
正确代码
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 7;
int n, m, ans;
char a[1005][1005];
bool vis[MAXN];//标记数组
int dx[5] = {-1, 1, 0};//方向数组
struct node {
int x, step;
};
queue<node> q;//创建队列
int bfs(int sx) {
q.push({sx, 0});//加入起点信息
vis[sx] = true;//标记起点
while (!q.empty()) {
node cur = q.front();//取出队首
q.pop();//弹出
if (cur.x == m) return cur.step;//返回步数
dx[2] = cur.x;//由于2x = x + x,所以可以把dx[2]设成cur.x
for (int i = 0; i <= 2; i++) {
int nx = cur.x + dx[i];
if (nx >= 0 && nx <= MAXN && vis[nx] == false) {
node nxt = {nx, cur.step + 1};
q.push(nxt);
vis[nx] = true;
}
}
}
return -1;
}
int main() {
cin >> n >> m;
cout << bfs(n) << endl;
return 0;
}
P1135 奇怪的电梯
题目描述
呵呵,有一天我做了一个梦,梦见了一种很奇怪的电梯。大楼的每一层楼都可以停电梯,而且第
输入格式
共二行。
第一行为三个用空格隔开的正整数,表示
第二行为
输出格式
一行,即最少按键次数,若无法到达,则输出 -1。
输入输出样例 #1
输入 #1
5 1 5
3 3 1 2 5
输出 #1
3
说明/提示
对于
本题共
正确代码
#include<bits/stdc++.h>
using namespace std;
const int N = 205;
int n, a, b;
int k[N];
bool vis[N];//标记数组
int dx[] = {-1, 1};//方向数组
//用于上下距离不是固定的,而是a[i],因此可以直接把dx[]初始值设为{-1, 1},
//上就是a[i] * 1, 下就是a[i] * -1
struct node {//创建结构体
int x, step;
};
int bfs(int sa) {
queue<node> q;//创建队列
q.push({sa, 0});//加入起点信息
vis[sa] = true;//标记起点
while (q.empty() == false) {
node cur = q.front();//取出队首
q.pop();//弹出
if (cur.x == b) return cur.step;//返回步数
for (int i = 0; i <= 1; i++) {
int nx = cur.x + dx[i] * k[cur.x];
if (nx >= 1 && nx <= n && vis[nx] == false) {
node nxt = {nx,cur.step + 1};
vis[nx] = true;
q.push(nxt);
}
}
}
return -1;//若无法到达,则输出-1
}
int main() {
cin >> n >> a >> b;
for (int i = 1; i <= n; i++) cin >> k[i];
cout << bfs(a);
return 0;
}
扩展
以前基本做过,在这不多讲,应该看一下就能懂了
T582134 01矩阵
题目描述
给定一个由 0 和 1 组成的矩阵 mp,请输出一个大小相同的矩阵,其中每一个格子是 mp 中对应位置元素到最近的 0 的距离。 两个相邻元素间的距离为 1 。
输入格式
一个由 0 和 1 组成的矩阵 mp。
输出格式
一个大小相同的矩阵,其中每一个格子是输入矩阵中对应位置元素到最近的 0 的距离。
输入输出样例 #1
输入 #1
3 3
0 0 0
0 1 0
1 1 1
输出 #1
0 0 0
0 1 0
1 2 1
说明/提示
数据范围:1 <= m, n <= 10^3
正确代码
#include<bits/stdc++.h>
using namespace std;
const int N = 1007;
int n, m, xa, ya;
bool vis[N][N];
int ans[N][N];
char a[N][N];
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
struct node {
int x, y, step;
};
queue<node> q;
void bfs(int sx, int sy) {
node tmp = {sx, sy, 0};
vis[sx][sy] = true;
q.push(tmp);
while (!q.empty()) {
node cur = q.front();
q.pop();
ans[cur.x][cur.y] = min(ans[cur.x][cur.y], cur.step);
for (int i = 0; i <= 3; i++) {
int nx = cur.x + dx[i];
int ny = cur.y + dy[i];
if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && vis[nx][ny] == false) {
node nxt = {nx, ny, cur.step + 1};
vis[nx][ny] = true;
q.push(nxt);
}
}
}
return;
}
int main() {
cin >> n >> m;
memset(ans, 0x3f, sizeof (ans));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
if (a[i][j] == '0') {ans[i][j] = 0; q.push({i, j, 0});}
}
bfs(xa, ya);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) cout << ans[i][j] << " ";
cout << endl;
}
return 0;
}
T579123 黑吃黑
题目描述
给你一个由 #;如果该方格是白色的, .。
我们将重复执行下面的操作,直到所有的方格都是黑色为止:
- 每个与黑色方格共边的白色方格都会变成黑色。
求将要执行的操作次数。初始网格中至少有一个黑色方格。
输入格式
输入内容由标准输入法提供,格式如下:\
-
- 给定网格中至少有一个黑色方格。
正确代码
#include <bits/stdc++.h>
using namespace std;
int n, m, ans = 0;
char a[1005][1005];
bool vis[1005][1005];
int dx[] = {1, 0, 0, -1};
int dy[] = {0, -1, 1, 0};
struct node {
int x, y, step;
};
queue<node> q;
void bfs() {
while (!q.empty()) {
node cur = q.front();
q.pop();
for (int i = 0; i <= 3; i++) {
int nx = cur.x + dx[i];
int ny = cur.y + dy[i];
if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && vis[nx][ny] == false && a[nx][ny] != '#') {
node nxt = {nx, ny, cur.step + 1};
ans = max(ans, cur.step + 1);
q.push(nxt);
vis[nx][ny] = true;
}
}
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
if (a[i][j] == '#') {
q.push({i, j, 0});
vis[i][j] = true;
}
}
bfs();
cout << ans << endl;
return 0;
}
T582138 疯狂补能
题目描述
有一个具有
.: 空单元格。#: 障碍物。S: 空单元格和起点。T: 空单元格和终点。
人杰老师可以通过消耗
网格中有
人杰老师从起点出发,初始能量为
输入格式
第一行输入2个整数
接下来输入
接下来输入一个整数
接下来
输出格式
如果人杰老师能够从起点到达终点,则输出 Yes;否则,输出 No。
输入输出样例 #1
输入 #1
4 4
S...
#..#
#...
..#T
4
1 1 3
1 3 5
3 2 1
2 3 1
输出 #1
Yes
输入输出样例 #2
输入 #2
2 2
S.
T.
1
1 2 4
输出 #2
No
输入输出样例 #3
输入 #3
4 5
..#..
.S##.
.##T.
.....
3
3 1 5
1 2 3
2 2 1
输出 #3
Yes
说明/提示
-
1 ≤ h, w ≤ 200 -
S和T在A_{i,j} 中各出现一次。-
1 ≤ N ≤ 300 -
1 ≤ R_i ≤ h -
1 ≤ C_i ≤ w -
(R_i, C_i) ≠ (R_j, C_j)$ if $i ≠ j. -
-
1 ≤ E_i ≤ h*w
正确代码
#include <bits/stdc++.h>
using namespace std;
int n, m;
int h;
int ssx, ssy;
char a[205][205];
int dis[205][205];
int b[205][205];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
struct node {
int x, y, nl;
};
struct NODE {
int r, c, e;
}x[305];
queue<node> q;
void bfs(int sx, int sy) {
q.push({sx, sy, b[sx][sy]});
dis[sx][sy] = b[sx][sy];
while (!q.empty()) {
node cur = q.front();
q.pop();
if(a[cur.x][cur.y] == 'T') {
cout << "Yes"; return;
}
if (cur.nl == 0 || cur.nl < dis[cur.x][cur.y]) continue;
for (int i = 0; i <= 3; i++) {
int nx = cur.x + dx[i];
int ny = cur.y + dy[i];
if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && a[nx][ny] != '#') {
int v = 0;
if (b[nx][ny] > 0) v = max(cur.nl - 1, b[nx][ny]);
else v = cur.nl - 1;
if (v > dis[nx][ny]) {
q.push({nx, ny, v}); dis[nx][ny] = v;
}
}
}
}
cout << "No";
return;
}
int main() {
cin >> n >> m;
memset(dis, -0x3f, sizeof(dis));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
if (a[i][j] == 'S') ssx = i, ssy = j;
}
cin >> h;
for (int i = 1; i <= h; i++) {
cin >> x[i].r >> x[i].c >> x[i].e;
b[x[i].r][x[i].c] = x[i].e;
}
bfs(ssx, ssy);
return 0;
}