Title Description:
Word Maze is a network game where you need to find food that is labeled with letters, but it is required to be eaten in the order of the given word letters. For example, if you have a given word if, you must first eat I before you can eat F.
但现在你的任务可没有这么简单,你现在处于一个迷宫Maze(n×m的矩阵)当中,里面到处都是以字母标注的食物,但你只能吃掉能连成给定单词W的食物。
, specify W as "Solo", and the word "solo" is marked red in the map.
Note that the letters are case-sensitive, you can only walk up or down.
Run time limit: Unlimited
Memory Limit: Unlimited
Input:
Enter the first line containing two integers n, m (0
Output:
If you can connect a given word to the map, output "YES" or Output "NO". Note: Each letter can only be used once.
Sample input:
5 5
SOLO
Cpucy
Eklqh
Crsol
Eklqo
Pgrbc
Sample output:
YES
Thinking Analysis:
- This problem is also not required for time and space, so the use of brute force
- Traverse the two-bit array, look for the letter of the corresponding word, find the set= ', and then conninue
Code:
ImportJava.util.ArrayList;ImportJava.util.Collections;ImportJava.util.List;ImportJava.util.Scanner; Public class Main { Public Static void Main(string[] args) {Scanner scan =NewScanner (system.in); while(Scan.hasnext ()) {intn = scan.nextint ();intm = Scan.nextint ();Char[] wave =New CharN [M]; String input = Scan.next ();Char[] Inputarray = Input.tochararray (); for(inti =0; i < n;i++) {String a = Scan.next ();Char[] Aarray = A.tochararray (); Wave[i] = Aarray; }intlength = Inputarray.length;Boolean[] flag =New Boolean[Length]; for(intQ =0Q < length;q++) {Flag[q] =false; } for(intc =0; c < length;c++) { for(intj =0; J < n;j++) { for(intK =0; k < m;k++) {if(Wave[j][k] = = Inputarray[c]) {if(Flag[c] = =false) {Flag[c] =true; WAVE[J][K] ="'; } } } } } for(intp =0;p < length;p++) {if(Flag[p] = =false) {System.out.println ("NO");return; }} System.out.println ("YES"); } }}
My QR code is as follows, welcome to exchange discussion
You are welcome to pay attention to the "It question summary" subscription number. Every day to push the classic face test and interview tips, are dry! The QR code of the subscription number is as follows:
Huawei Machine Question "13"-wave array find alphabet game