-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexist.java
More file actions
executable file
·132 lines (118 loc) · 3.92 KB
/
exist.java
File metadata and controls
executable file
·132 lines (118 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package backtracking;
import array.LIS;
import java.awt.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Created by Administrator on 2018/4/2 0002.
*/
public class exist {
List<List<Integer>> result=new ArrayList<List<Integer>>();
public boolean exist(char[][] board, String word) {
int size=word.length();
char[] chars=new char[size];
for(int i=0;i<size;i++){
chars[i]=word.charAt(i);
}
int m=board.length;
int n=board[0].length;
List<int[]> list=new ArrayList<>();
//boolean[] hasChar=new boolean[size];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(board[i][j]==chars[0]) {
int a[]=new int[]{i,j};
list.add(a);
}
}
}
if(list.size()==0)return false;
for(int [] array:list){
List<Integer> listback=new ArrayList<>();
char boardtmp[][]=new char[m][n];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
boardtmp[i][j]=board[i][j];
}
}
boardtmp[array[0]][array[1]]='0';
backtracking(array,boardtmp,word,word.length()-1,1,listback);
if(result.size()!=0)
return true;
}
return false;
}
public void backtracking(int[]a,char[][] board,String word,int length,int start,List<Integer> list){
if(length==0) {
result.add(new ArrayList<>(list));
return;
}
int m=board.length;
int n=board[0].length;
int x=a[0];
int y=a[1];
if(x+1<m) {
int [] atmp=new int[]{x+1,y};
list.add(1);
char tmp=board[x+1][y];
if(board[x+1][y]!='0'&&board[x+1][y]==word.charAt(start)){
board[x+1][y]='0';
backtracking(atmp,board,word,length-1,start+1,list);
list.remove(list.size()-1);
}
else {
list.remove(list.size()-1);
}
board[x+1][y]=tmp;
}
if(x-1>=0) {
int [] atmp=new int[]{x - 1, y};
list.add(1);
char tmp=board[x-1][y];
if(board[x-1][y]!='0'&&board[x-1][y]==word.charAt(start)){
board[x-1][y]='0';
backtracking(atmp,board,word,length-1,start+1,list);
list.remove(list.size()-1);
}
else {
list.remove(list.size()-1);
}
board[x-1][y]=tmp;
}
if(y+1<n){
int [] atmp=new int[]{x , y+1};
list.add(1);
char tmp=board[x][y+1];
if(board[x][y+1]!='0'&&board[x][y+1]==word.charAt(start)){
board[x][y+1]='0';
backtracking(atmp,board,word,length-1,start+1,list);
list.remove(list.size()-1);
}
else {
list.remove(list.size()-1);
}
board[x][y+1]=tmp;
}
if(y-1>=0){
int [] atmp=new int[]{x , y-1};
list.add(1);
char tmp=board[x][y-1];
if(board[x][y-1]!='0'&&board[x][y-1]==word.charAt(start)){
board[x][y-1]='0';
backtracking(atmp,board,word,length-1,start+1,list);
list.remove(list.size()-1);
}
else {
list.remove(list.size()-1);
}
board[x][y-1]=tmp;
}
}
public static void main(String[] args) {
char [][] board=new char[][]{{'c','a','a'},{'a','a','a'},{'b','c','d'}};
exist exist=new exist();
System.out.println(exist.exist(board,"aab"));
}
}