complabel

2
import javax.xml.transform.Source; import java.awt.*; import java.awt.font.ImageGraphicAttribute; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamRead er; import java.util.Collections; /**  * Created by IntelliJ IDEA.  * User: ruzbeh  * Date: 6/29/12  * Time: 12:58 AM  * To change this template use File | Settings | File Templates.  */ public class Solution {  static int[][] img;  static void compLabel(int i, int j, int m,int n) {  if(i<0 || j<0 ||i > n-1||j > n-1) return;  if (img[i][j] == 1) {  img[i][j] = m;  compLabel(i - 1, j - 1, m,n);  compLabel(i - 1, j, m,n);  compLabel(i - 1, j + 1, m,n);  compLabel(i, j - 1, m,n);  compLabel(i, j + 1, m,n);  compLabel(i + 1, j - 1, m,n);  compLabel(i + 1, j, m,n);  compLabel(i + 1, j + 1, m,n);  }  } public static void main(String[] args) throws IOException {  BufferedReader br = new BufferedReader(new InputStreamReader(System.in)) ;  int T = Integer.parseInt(br.re adLine());  for (int t = 0; t < T; t++) {  int n = Integer.parseInt(br.re adLine());  img = new int[n][n];  int label = 2;  for (int i = 0; i < n; i++) {  int j = 0;  for (String str : br.readLine().split(" ")) {  int value = Integer.parseInt(str);  img[i][j] = value;  j++;  }  }  for (int y = 0; y < n; y++)  for (int x = 0; x < n; x++)  if (img[x][y] == 1) {  compLabel(x, y, ++label,n);

Upload: komotinaios

Post on 14-Oct-2015

3 views

Category:

Documents


0 download

DESCRIPTION

COMPLABEL

TRANSCRIPT

import javax.xml.transform.Source;import java.awt.*;import java.awt.font.ImageGraphicAttribute;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Collections;/** * Created by IntelliJ IDEA. * User: ruzbeh * Date: 6/29/12 * Time: 12:58 AM * To change this template use File | Settings | File Templates. */public class Solution { static int[][] img; static void compLabel(int i, int j, int m,int n) { if(i n-1) return; if (img[i][j] == 1) { img[i][j] = m; compLabel(i - 1, j - 1, m,n); compLabel(i - 1, j, m,n); compLabel(i - 1, j + 1, m,n); compLabel(i, j - 1, m,n); compLabel(i, j + 1, m,n); compLabel(i + 1, j - 1, m,n); compLabel(i + 1, j, m,n); compLabel(i + 1, j + 1, m,n); } } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int T = Integer.parseInt(br.readLine()); for (int t = 0; t < T; t++) { int n = Integer.parseInt(br.readLine()); img = new int[n][n]; int label = 2; for (int i = 0; i < n; i++) { int j = 0; for (String str : br.readLine().split(" ")) { int value = Integer.parseInt(str); img[i][j] = value; j++; } } for (int y = 0; y < n; y++) for (int x = 0; x < n; x++) if (img[x][y] == 1) { compLabel(x, y, ++label,n); } System.out.println(label - 2); } }}