Advertisement
Manh_LK

Ốc sên ăn rau

Mar 16th, 2020
1,036
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.43 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class OcSenAnRau {
  3.  
  4.     static class toaDo
  5.     {
  6.         int x;
  7.         int y;
  8.         public toaDo(int x, int y)
  9.         {
  10.             this.x=x;
  11.             this.y=y;
  12.         }
  13.     }
  14.     static class Queue
  15.     {
  16.         final int size=100;
  17.         toaDo [] q;
  18.         int front, rear;
  19.         //Constructor
  20.         public Queue()
  21.         {
  22.             q=new toaDo[size];
  23.             front=rear=-1;
  24.         }
  25.         public boolean isEmpty()
  26.         {
  27.             if(rear==front) return true;
  28.             return false;
  29.         }
  30.         public void enQueue(toaDo a)
  31.         {
  32.             rear=(rear+1)%size;
  33.             q[rear]=a;
  34.         }
  35.         public toaDo deQueue()
  36.         {
  37.             front=(front+1)%size;
  38.             return q[front];
  39.         }
  40.     }
  41.     public static int[]X= {-1,1,0,0};
  42.     public static int[]Y= {0,0,-1,1};
  43.     public static void main(String[] args) throws FileNotFoundException {
  44.         System.setIn(new FileInputStream("D:\\Program\\STP\\senRau.txt"));
  45.         Scanner sc=new Scanner(System.in);
  46.         int m,n,res=1;
  47.         m=sc.nextInt();
  48.         n=sc.nextInt();
  49.         int x=sc.nextInt();
  50.         int y=sc.nextInt();
  51.         int [][]A=new int[m][n];
  52.         for(int i=0;i<m;i++)
  53.         {
  54.             for(int j=0;j<n;j++)
  55.             {
  56.                 A[i][j]=sc.nextInt();
  57.             }
  58.         }
  59.         Queue QQ=new Queue();
  60.         A[x-1][y-1]=1;
  61.         QQ.enQueue(new toaDo(x-1, y-1));
  62.         while(!QQ.isEmpty())
  63.         {
  64.             toaDo tmpt=QQ.deQueue();
  65.             for(int i=0;i<4;i++)
  66.             {
  67.                 int u=tmpt.x+X[i];
  68.                 int v=tmpt.y+Y[i];
  69.                 if(u>=0&&u<m&&v>=0&&v<n&&A[u][v]==0)
  70.                 {
  71.                     res++;
  72.                     QQ.enQueue(new toaDo(u, v));
  73.                     A[u][v]=1;
  74.                 }
  75.             }
  76.         }
  77.         System.out.println(res);
  78.     }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement