FarmIrrigation

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;


public class Field {
 
 public  char element;
 public  int num;
 public  Field(char a,int b)
 {
  this.element=a;
  this.num=b;
 }

}

public class FarmIrrigationLast {

 public static int[][] field = { { 1, 1, 0, 0 }, { 0, 1, 1, 0 },
   { 1, 0, 0, 1 }, { 0, 0, 1, 1 }, { 0, 1, 0, 1 }, { 1, 0, 1, 0 },
   { 1, 1, 1, 0 }, { 1, 1, 0, 1 }, { 1, 0, 1, 1 }, { 0, 1, 1, 1 },
   { 1, 1, 1, 1 } };
 public static int m;// 行
 public static int n;// 列
 public static int k = 0;// 记录测试数据的组数;
 public static int count; // 集合的个数
 public static Field[][] a; // 存放输入的地,并转换成相应的整数。
 public static String result =""; // 存放魅族数据的测试结果。
 public static Scanner sc = new Scanner(System.in);
 public static InputStreamReader isr = new InputStreamReader(System.in);

 public static boolean link(int u, int v, int flag) {
  // flag为1时表示测试上下两块地的水管是否连接
  // flag为0时表示测试左右两块地的水管是否连接
  // u在上或者左,v在下或者右
  if (flag == 1) {
   if (field[u][3] == 1 && field[v][1] == 1)
    return true;
   else
    return false;
  } else {
   if (field[u][2] == 1 && field[v][0] == 1)
    return true;
   else
    return false;
  }
 }

 public static void merge(int p, int q) // 合并两个集合,将q集合并并到p集合中
 {
  count--;
  for (int i = 0; i < m; i++)
   for (int j = 0; j < n; j++) {
    if (a[i][j].num == q)
     a[i][j].num = p;
   }
 }

 public static int find(Field b) // 找出指定字符在那个集合中。
 {
  return b.num;
 }

 public static void initial() // 初始化程序
 {
  try {
   count = m * n;
   a = new Field[m][n];
   for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
     char temp1 = (char) isr.read();
     int temp2 = i * n + j;
     a[i][j] = new Field(temp1, temp2);
    }
    isr.read();
    isr.read();
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 public static void main(String[] args) {
  // A=65;

  m = sc.nextInt(); // 行数
  n = sc.nextInt();// 列数
  while (n != 0 && m != 0) {//直到输入m=0,n=0时输出每组测试数据的结果程序结束
   initial();

   for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++) {

     boolean temp;
     if (i > 0 || j > 0) {
      if (i == 0) // -65
      {
       temp = link((int) a[i][j - 1].element - 65,
         (int) a[i][j].element - 65, 0);

       if (temp == true)// 有亲戚关系
       {
        int p = find(a[i][j - 1]);
        int q = find(a[i][j]);
        if (p != q)// 不在一个集合
        {
         merge(p, q);
        }
       }
      }
      if (j == 0) {
       temp = link((int) a[i - 1][j].element - 65,
         (int) a[i][j].element - 65, 1);

       if (temp == true) {
        int p = find(a[i - 1][j]);
        int q = find(a[i][j]);
        if (p != q)// 不在一个集合
        {
         merge(p, q);
        }
       }
      }
      if (i > 0 && j > 0) {
       temp = link((int) a[i - 1][j].element - 65,
         (int) a[i][j].element - 65, 1);

       if (temp == true) {
        int p = find(a[i - 1][j]);
        int q = find(a[i][j]);
        if (p != q)// 不在一个集合
        {
         merge(p, q);
        }
       }

       temp = link((int) a[i][j - 1].element - 65,
         (int) a[i][j].element - 65, 0);

       if (temp == true) {
        int p = find(a[i][j - 1]);
        int q = find(a[i][j]);
        if (p != q)// 不在一个集合
        {
         merge(p, q);
        }
       }
      }
     }
    }

   //System.out.println(count);
   result=result+count+"/r/n";
   m = sc.nextInt();
   n = sc.nextInt();
  }
  System.out.println(result);
 }
}


本文来自CSDN博客,转载请标明出处:https://i-blog.csdnimg.cn/blog_migrate/595981fc8a7b9be8b4f1508c4752b7b6.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值