tags: Sword refers to OFFER
Design a function to determine if there is a path containing a string in the matrix
public class Offer17 {
public static void main(String[] args) {
char[][] data= {
{'a','s','d'},
{'z','x','c'},
{'q','w','e'},
};
char[] temp = new String("sxw").toCharArray();
System.out.println(hasPath(data,temp));
}
/**
* Backtracking
*/
public static boolean hasPath(char[][] data,char[] word){
// 1. Data source judgment
if(data.length==0)return false;
// 2. New record matrix
boolean[][] temp = new boolean[data.length][data[0].length];
// 3. Back
for(int i=0;i<data.length;i++){
for(int j=0;i<data[0].length;j++){
if(dfs(data,temp,word,i,j,0))return true;
}
}
return false;
}
public static boolean dfs(char[][] data,boolean[][] temp,char[] word,int i,int j,int index){
// Problem
if(i<0||i>=data.length||j<0||j>=data[0].length||temp[i][j]==true)return false;
// Character does not match
if(data[i][j]!=word[index])return false;
// The following is a data matching situation
if(index==word.length-1)return true;
// Tag access
temp[i][j] = true;
boolean flag = dfs(data,temp,word,i+1,j,index+1)||
dfs(data,temp,word,i-1,j,index+1)||
dfs(data,temp,word,i,j+1,index+1)||
dfs(data,temp,word,i,j-1,index+1);
// Match failed, need to change the visited flag
temp[i][j] = false;
return flag;
}
}
About the hungry, use the new version of vue-cli to mock data, the problems encountered, the new version of vue-cli is missing dev-sever.js and dev-client.js When mocking data in vue-cli, the data int...
Article Directory Introduction to Nginx Nginx environment preparation Configuration file Nginx start, stop Custom error page Access control Web hosting settings Based on port number Based on domain na...
1. Multi-threaded realization There are two ways to achieve multithreading: 1.1. Inherit the Thread class => Example: A a=new A(); a.start(); 1.2. Implement the Runnable interface => Example: A ...
http://codeforces.com/contest/371/problem/B More violent way of writing: bfs search (can pass, but there are better ways), a total of six expansion directions, one point of optimization is: eat big. c...
When doing CIFAR image recognition, I want to visualize the image Article Directory 1. Official website description 2. The purpose of the function 1. Official website description The documents on the ...
Solution: field data type to the decimal data type, the data type for applications requiring very high precision calculations...
1010 yuan polynomial guidance Design function to ask a dollar derivative. (Note: First stage derivative Nx ^ (N-1) of x ^ n (n is an integer))) Input format: Enter polynomial non-zero coefficients and...
This article mainly introduces the use of various optimizers. Because the task of most machine learning is to minimize the loss, in the case of loss definition, the rest of the work is left to the opt...
Configuration Logic Solution The two applications of this machine need to store the configuration in the H2 file, so the H2 embedded database is used. So you can solve the file_lock = no without a loc...
Keepalived + Nginx High Access Cluster (Double Main Mode) 1. Cluster architecture diagram: Description: Continue to do experiments in the above environment, just modify the configuration files for the...