manthanthakker40

Flood Fill

Jan 14th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.26 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package code;
  7.  
  8. import java.util.ArrayList;
  9. import java.util.Arrays;
  10. import java.util.LinkedHashMap;
  11. import java.util.LinkedList;
  12. import java.util.List;
  13. import java.util.Scanner;
  14.  
  15. /**
  16.  *
  17.  * @author manth
  18.  */
  19. public class Code {
  20.  
  21.     /**
  22.      * @param args the command line arguments
  23.      */
  24.     /*
  25.     Given a string s and a non-empty string p, find all the start indices of p's anagrams in s.
  26.  
  27. Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.
  28.  
  29. The order of output does not matter.
  30.  
  31. Example 1:
  32.  
  33. Input:
  34. s: "cbaebabacd" p: "abc"
  35.  
  36. Output:
  37. [0, 6]
  38.  
  39. Explanation:
  40. The substring with start index = 0 is "cba", which is an anagram of "abc".
  41. The substring with start index = 6 is "bac", which is an anagram of "abc".
  42.     */
  43.     static int [][] sc=new int [25][25];
  44.     public static void main(String[] args) {
  45.        Scanner sc1=new Scanner(System.in);
  46.        String s=sc1.nextLine();
  47.        
  48.        
  49.        
  50.      
  51.        
  52.        floodfill(20,20);
  53.        for(int i=0;i<max_r;i++)
  54.        {
  55.            for(int j=0;j<max_c;j++)
  56.            {
  57.                System.out.print(sc[i][j]);
  58.            }
  59.            System.out.println();
  60.        }
  61.      
  62.        
  63.    
  64.      
  65.        }
  66.     static int color=25;
  67.     static int max_r=sc.length;
  68.     static int max_c=sc[0].length;
  69.     static boolean scr[][]=new boolean[25][25];
  70.     public static void floodfill(int r,int c)
  71.     {
  72.        if(r>=max_r|r<=-1|c>=max_c|c<=-1)
  73.        {
  74.           // System.out.println("returned");
  75.            return;
  76.        }
  77.       // System.out.println(r+"  "+c);
  78.      
  79.        if(scr[r][c]==true)
  80.            return;
  81.        sc[r][c]=color;
  82.        scr[r][c]=true;
  83.        
  84.        floodfill(r-1,c);
  85.        floodfill(r-1,c-1);
  86.        floodfill(r-1,c+1);
  87.        floodfill(r,c-1);
  88.        floodfill(r,c+1);
  89.        floodfill(r,c);
  90.        floodfill(r+1,c-1);
  91.        floodfill(r+1,c);
  92.        floodfill(r+1,c+1);
  93.        
  94.        
  95.     }
  96.    
  97.    
  98.    
  99.    
  100.    
  101.        
  102.        
  103.    
  104.    
  105.    
  106. }
Advertisement
Add Comment
Please, Sign In to add comment