Created
March 2, 2012 19:02
-
-
Save phaniram/1960411 to your computer and use it in GitHub Desktop.
Codechef-MarchContest-SPOON-(Accepted)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cyp.codechef.marcontest; | |
import java.io.IOException; | |
import java.util.Scanner; | |
/* | |
* Cypronmaya - Codechef March 2012 Contest - Problem Code : SPOON | |
*/ | |
class SPOON { | |
char[][] data; | |
int rows; | |
int columns; | |
public SPOON(char[][] data, int rows, int columns) { | |
this.data = data; | |
this.rows = rows; | |
this.columns = columns; | |
} | |
public static void main(String[] args) throws IOException { | |
Scanner sc = new Scanner(System.in); | |
int num_cases = sc.nextInt(); | |
for (int i = 0; i < num_cases; i++) { | |
int rows = sc.nextInt(); | |
int columns = sc.nextInt(); | |
char data[][] = new char[rows + 1][columns + 1]; | |
for (int r = 1; r <= rows; r++) { | |
String row = sc.next().toLowerCase(); | |
for (int c = 1; c <= columns; c++) { | |
data[r][c] = row.charAt(c - 1); | |
} | |
} | |
SPOON spoon = new SPOON(data, rows, columns); | |
spoon.solve(); | |
} | |
} | |
private void solve() { | |
boolean found = false; | |
for (int i = 1; i <= rows; i++) { | |
for (int j = 1; j <= columns - 4; j++) { | |
if (check_right(i, j)) { | |
found = true; | |
break; | |
} | |
} | |
} | |
once: | |
if (!found) { | |
for (int i = 1; i <= rows - 4; i++) { | |
for (int j = 1; j <= columns; j++) { | |
if (check_down(i, j)) { | |
found = true; | |
break once; | |
} | |
} | |
} | |
} | |
if (!found) { | |
System.out.println("There is indeed no spoon!"); | |
} | |
} | |
private boolean check_down(int i, int j) { | |
boolean ret = false; | |
if (data[i][j] == 's' && data[i + 1][j] == 'p' && data[i + 2][j] == 'o' && data[i + 3][j] == 'o' && data[i + 4][j] == 'n') { | |
System.out.println("There is a spoon!"); | |
ret = true; | |
} | |
return ret; | |
} | |
private boolean check_right(int i, int j) { | |
boolean ret = false; | |
if (data[i][j] == 's' && data[i][j + 1] == 'p' && data[i][j + 2] == 'o' && data[i][j + 3] == 'o' && data[i][j + 4] == 'n') { | |
System.out.println("There is a spoon!"); | |
ret = true; | |
} | |
return ret; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Chef recently saw the movie Matrix. He loved the movie overall but he didn't agree with some things in it. Particularly he didn't agree with the bald boy when he declared - There is no spoon. Being a chef, he understands the importance of the spoon and realizes that the universe can't survive without it. Furthermore, he is sure there is a spoon; he saw it in his kitchen this morning. So he has set out to prove the bald boy is wrong and find a spoon in the matrix. He has even obtained a digital map already. Can you help him? Formally you're given a matrix of lowercase and uppercase Latin letters. Your job is to find out if the word "Spoon" occurs somewhere in the matrix or not. A word is said to be occurred in the matrix if it is presented in some row from left to right or in some column from top to bottom. Note that match performed has to be case insensitive.
Input
The first line of input contains a positive integer T, the number of test cases. After that T test cases follow. The first line of each test case contains two space separated integers R and C, the number of rows and the number of columns of the matrix M respectively. Thereafter R lines follow each containing C characters, the actual digital map itself.
Output
For each test case print one line. If a "Spoon" is found in Matrix, output "There is a spoon!" else output "There is indeed no spoon!" (Quotes only for clarity).
Constraints
1 ≤ T ≤ 100
1 ≤ R, C ≤ 100
Sample Input
3
3 6
abDefb
bSpoon
NIKHil
6 6
aaaaaa
ssssss
xuisdP
oooooo
ioowoo
bdylan
6 5
bdfhj
cacac
opqrs
ddddd
india
yucky
Sample Output
There is a spoon!
There is a spoon!
There is indeed no spoon!
Explanation
In the first test case, "Spoon" occurs in the second row. In the second test case, "spOon" occurs in the last column.