POJ3194(DFS)

POJ3194(DFS)


Equidivisions

Description

An equidivision of an n × n square array of cells is a partition of the n2 cells in the array in exactly n sets, each one with n contiguous cells. Two cells are contiguous when they have a common side.A good equidivision is composed of contiguous regions. The figures show a good and a wrong equidivision for a 5 × 5 square:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qYVZtOkF-1592904248223)(http://poj.org/images/3194_1.png)]Note that in the second example the cells labeled with 4 describe three non-contiguous regions and cells labeled with 5 describe two non-contiguous regions. You must write a program that evaluates if an equidivision of the cells in a square array is good or not.

Input

It is understood that a cell in an n × n square array is denoted by a pair (i, j), with 1 ≤ i, jn. The input file contains several test cases. Each test case begins with a line indicating n, 0 < n < 100, the side of the square array to be partitioned. Next, there are n − 1 lines, each one corresponding to one partition of the cells of the square, with some non-negative integer numbers. Consecutive integers in a line are separated with a single blank character. A line of the forma1a2a3a4…means that cells denoted with the pairs (a1, a2), (a3, a4), … belong to one of the areas in the partition. The last area in the partition is defined by those cells not mentioned in the n − 1 given lines. If a case begins with n = 0 it means that there are no more cases to analyze.

Output

For each test case good must be printed if the equidivision is good, in other case, wrong must be printed. The answers for the different cases must preserve the order of the input.

Sample Input

2
1 2 2 1
5
1 1 1 2 1 3 3 2 2 2
2 1 4 2 4 1 5 1 3 1
4 5 5 2 5 3 5 5 5 4
2 5 3 4 3 5 4 3 4 4
5
1 1 1 2 1 3 3 2 2 2
2 1 3 1 4 1 5 1 4 2
4 5 5 2 5 3 5 5 5 4
2 4 1 4 3 5 4 3 4 4
0

Sample Output

wrong
good
wrong
题意

因为给出N*N的矩阵了,而且其中肯定有1到n的数,如果这1到n的数中,举例一个数:1,这个数都是都是左右相连的话,并且只有n个1左右相连的话可以,如果这n个数都是这种情况,那么就是good,否则wrong……

思路

用dfs深度搜索,搜索0~n每个数,检查每个数字是不是有n个即可。

代码
import java.util.Scanner;

public class Equidivisions {

    int[][] map = new int[105][105];
    int[] xs = {-1, 0, 0, 1};
    int[] ys = {0, -1, 1, 0};
    int count = 0;

    public void dfs(int x, int y, int index) {
        this.count++;
        if (this.map[x][y] == index) {
            this.map[x][y] = 0;
        }
        for (int i = 0; i < 4; i++) {
            if (this.map[x + xs[i]][y + ys[i]] == index) {
                dfs(x + xs[i], y + ys[i], index);
            }
        }
    }


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            Equidivisions e = new Equidivisions();
            int n = sc.nextInt();
            if (n == 0) {
                break;
            }
            for (int i = 1; i < n; i++) {
                for (int j = 1; j <= n; j++) {
                    int x = sc.nextInt();
                    int y = sc.nextInt();
                    e.map[x][y] = i;
                }
            }

            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    if (e.map[i][j] == 0) {
                        e.map[i][j] = n;
                    }
                }
            }
            boolean flag = true;
            for (int i = 1; i <= n + 1; i++) {
                for (int j = 1; j <= n; j++) {
                    if (e.map[i][j] != 0) {
                        e.dfs(i, j, e.map[i][j]);
                        if (e.count != n) {
                            flag = false;
                        }
                        e.count = 0;
                    }
                }
            }
            if (flag) {
                System.out.println("good");
            } else {
                System.out.println("wrong");
            }

        }
        sc.close();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值