题目描述
找到它是一个小游戏,你需要在一个矩阵中找到给定的单词。
假设给定单词 HELLOWORD,在矩阵中只要能找到 H->E->L->L->O->W->O->R->L->D连成的单词,就算通过。
注意区分英文字母大小写,并且您只能上下左右行走,不能走回头路。
输入描述
输入第 1 行包含两个整数 n、m (0 < n,m < 21) 分别表示 n 行 m 列的矩阵,
第 2 行是长度不超过100的单词 W (在整个矩阵中给定单词 W 只会出现一次),
从第 3 行到第 n+2 行是指包含大小写英文字母的长度为 m 的字符串矩阵。
输出描述
如果能在矩阵中连成给定的单词,则输出给定单词首字母在矩阵中的位置(第几行 第几列),
否则输出“NO”。
用例
输入 |
5 5 HELLOWORLD CPUCY EKLQH CHELL LROWO DGRBC |
输出 |
3 2 |
说明 |
无 |
输入 |
5 5 HELLOWORLD CPUCY EKLQH CHELL LROWO AGRBC |
输出 |
NO |
说明 |
无 |
题目解析
C++
#include <iostream>
#include <cstring>
using namespace std;
const int N = 105;
string g[N];
string w;
int n, m;
bool st[N][N];
bool dfs(int x, int y, int k)
{
if (g[x][y] != w[k]) return false; // 当前字符不匹配
if (k == w.size() - 1) return true; // 找到了最后一个字符
st[x][y] = true; // 标记当前点已经走过
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
for (int i = 0; i < 4; i ++ )
{
int a = x + dx[i], b = y + dy[i];
if (a >= 0 && a < n && b >= 0 && b < m && !st[a][b])
if (dfs(a, b, k + 1)) return true; // 向四个方向继续搜索
}
st[x][y] = false; // 回溯
return false;
}
int main()
{
cin >> n >> m >> w;
for (int i = 0; i < n; i ++ ) cin >> g[i];
for (int i = 0; i < n; i ++ )
for (int j = 0; j < m; j ++ )
if (dfs(i, j, 0))
{
cout << i + 1 << ' ' << j + 1 << endl;
return 0;
}
cout << "NO" << endl;
return 0;
}
java
import java.util.Scanner;
public class Main {
static String[] g = new String[105];
static String w;
static int n, m;
static boolean[][] st = new boolean[105][105];
public static boolean dfs(int x, int y, int k) {
if (g[x].charAt(y) != w.charAt(k)) return false;
if (k == w.length() - 1) return true;
st[x][y] = true;
int[] dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};
for (int i = 0; i < 4; i++) {
int a = x + dx[i], b = y + dy[i];
if (a >= 0 && a < n && b >= 0 && b < m && !st[a][b]) {
if (dfs(a, b, k + 1)) return true;
}
}
st[x][y] = false;
return false;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
w = sc.next();
for (int i = 0; i < n; i++) {
g[i] = sc.next();
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (dfs(i, j, 0)) {
System.out.println((i + 1) + " " + (j + 1));
return;
}
}
}
System.out.println("NO");
}
}
javaScript
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const lines = [];
let n, m;
let word;
rl.on("line", (line) => {
lines.push(line);
if (lines.length === 2) {
[n, m] = lines[0].split(" ").map(Number);
word = lines[1];
}
if (n && lines.length === n + 2) {
const matrix = lines.slice(2).map((line) => line.split(""));
console.log(dfs(matrix, n, m, word));
lines.length = 0;
}
});
function dfs(matrix, n, m, word) {
const len = word.length;
const visited = new Array(n).fill(0).map(() => new Array(m).fill(false));
const backTracking = (i, j, k) => {
if (k === len) return true;
if (
i < 0 ||
i >= n ||
j < 0 ||
j >= m ||
visited[i][j] ||
matrix[i][j] !== word[k]
)
return false;
visited[i][j] = true;
const nextK = k + 1;
const res =
backTracking(i - 1, j, nextK) ||
backTracking(i + 1, j, nextK) ||
backTracking(i, j - 1, nextK) ||
backTracking(i, j + 1, nextK);
visited[i][j] = false;
return res;
}
for (let i = 0; i < n; i++) {
for (let j = 0; j < m; j++) {
if (backTracking(i, j, 0)) {
return `${i + 1} ${j + 1}`;
}
}
}
return "NO";
}
python
n, m = map(int, input().split())
w = input()
g = []
for i in range(n):
g.append(input())
st = [[False] * m for _ in range(n)]
def dfs(x, y, k):
if g[x][y] != w[k]:
return False
if k == len(w) - 1:
return True
st[x][y] = True
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
for i in range(4):
a = x + dx[i]
b = y + dy[i]
if a >= 0 and a < n and b >= 0 and b < m and not st[a][b]:
if dfs(a, b, k + 1):
return True
st[x][y] = False
return False
for i in range(n):
for j in range(m):
if dfs(i, j, 0):
print(i + 1, j + 1)
exit()
print("NO")