admin 管理员组

文章数量: 1184232

An equidivision of an n × n square array of cells is a partition of the n 2 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:

问题链接
问题简述 :给定一个n*n的矩阵,其中放置n个数字,判定四连通的相同数字的个数是否都等于n?
问题分析 :用DFS来解决,也可以用泛洪(Flood fill)算法来解决。输入处理有点繁琐。
程序说明 :(略)
参考链接 :(略)
题记 :(略)

AC的C++语言程序如下:

/* UVA11110 POJ3194 Equidivisions */#include<iostream>#include<cstdio>#include<cctype>#include<cstring>usingnamespace std;constint dr[]={0,0,1,-1};constint dc[]={1,-1,0,0};constint L =sizeof dr /sizeof dr[0];constint N =100;int n, g[N][N], vis[N][N];char s[1024];intdfs(int row,int col){
    vis[row][col]=1;int sum =1;for(int i =0; i < L; i++){int nextrow = row + dr[i];int nextcol = col + dc[i];if(0<= nextrow && nextrow < n &&0<= nextcol && nextcol < n &&
                vis[nextrow][nextcol]==0&& g[nextrow][nextcol]== g[row][col])
            sum +=dfs(nextrow, nextcol);}return sum;}intmain(){while(~scanf("%d",&n)&& n){getchar();memset(g,0,sizeof g);for(int i =1; i < n; i++){gets(s);int len =strlen(s);for(int j =0; j < len;){int r =0, c =0;while(j < len &&!isdigit(s[j])) j++;if(j >= len)break;while(j < len &&isdigit(s[j]))
                    r = r *10+ s[j++]-'0';while(j < len &&!isdigit(s[j])) j++;while(j < len &&isdigit(s[j]))
                    c = c *10+ s[j++]-'0';
                g[r -1][c -1]= i;}}memset(vis,0,sizeof vis);int flag =1;for(int i =0; flag && i < n; i++)for(int j =0; j < n; j++)if(vis[i][j]==0){if(dfs(i, j)!= n){
                        flag =0;break;}}puts(flag ?"good":"wrong");}return0;}

本文标签: 中的 跨平台挑 编程