Skip to content

Instantly share code, notes, and snippets.

@bknarendra
Created May 14, 2012 07:47
Show Gist options
  • Save bknarendra/2692525 to your computer and use it in GitHub Desktop.
Save bknarendra/2692525 to your computer and use it in GitHub Desktop.
CoderCharts:Crosswords
import java.util.*;
import java.io.*;
class Node {
char content;
boolean marker;
Collection<Node> child;
public Node(char c){
child = new LinkedList<Node>();
marker = false;
content = c;
}
public Node subNode(char c){
if(child!=null){
for(Node eachChild:child){
if(eachChild.content == c){
return eachChild;
}
}
}
return null;
}
}
class Trie{
private Node root;
public Trie(){
root = new Node(' ');
}
public void insert(String s){
char c;
Node current = root;
if(s.length()==0)
current.marker=true;
for(int i=0;i<s.length();i++){
c=s.charAt(i);
if(c>='A'&&c<='Z') c=(char)((c-'A')+'a');
Node child = current.subNode(c);
if(child!=null){
current = child;
}
else{
current.child.add(new Node(c));
current = current.subNode(c);
}
if(i==s.length()-1)
current.marker = true;
}
}
public boolean search(String s){
Node current = root;
while(current != null){
for(int i=0;i<s.length();i++){
if(current.subNode(s.charAt(i)) == null)
return false;
else
current = current.subNode(s.charAt(i));
}
if (current.marker == true)
return true;
else
return false;
}
return false;
}
}
public class crosswords
{
public static void main(String args[]) throws Exception
{
Trie t=new Trie();
//long c=System.currentTimeMillis();
Scanner sc=new Scanner(new File(args[1]));
while(sc.hasNext())
t.insert(sc.nextLine());
sc.close();
sc=new Scanner(new File(args[0]));
int i,j,k,l,m,o,p,n=sc.nextInt();sc.nextLine();
StringBuffer grid[]=new StringBuffer[n];
String look="";
for(i=0;i<n;i++)
grid[i]=new StringBuffer(sc.nextLine());
String g="";m=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{
for(l=0;l<n;l++)
{
g="";
if(i==k)
{
if(Math.abs(j-l)<2)
continue;
if(j>l)
for(m=j;m>=l;m--)
g+=g.valueOf(grid[i].charAt(m));
else
for(m=j;m<=l;m++)
g+=g.valueOf(grid[i].charAt(m));
}
else if(j==l)
{
if(Math.abs(i-k)<2)
continue;
if(i>k)
for(m=i;m>=k;m--)
g+=g.valueOf(grid[m].charAt(j));
else
for(m=i;m<=k;m++)
g+=g.valueOf(grid[m].charAt(j));
}
else
{
if(Math.abs(k-i)==Math.abs(l-j))
{
if(i<k&&j>l)
for(m=i,o=j;m<=k&&o>=l;m++,o--)
g+=g.valueOf(grid[m].charAt(o));
else if(i>k&&j<l)
for(m=i,o=j;m>=k&&o<=l;m--,o++)
g+=g.valueOf(grid[m].charAt(o));
else if(j>l&&i>k)
for(m=i,o=j;m>=k&&o>=l;m--,o--)
g+=g.valueOf(grid[m].charAt(o));
else
for(m=i,o=j;m<=k&&o<=l;m++,o++)
g+=g.valueOf(grid[m].charAt(o));
}
else continue;
}
look=g;
if(look.length()<3)
continue;
if(t.search(look))
System.out.println(i+" "+j+" "+k+" "+l+" "+look);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment