| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2585 人关注过本帖
标题:[已解决]最大连续的面积。谢谢大家,特别是种子染色法。
只看楼主 加入收藏
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
我来扔砖头..有玉的一起来啊
#include<stdio.h>
int re=0;
int seed_fill(int a[][4],int i,int j)
{  
    if(a[i][j]!=1) { re++;a[i][j]=1;}
    if(a[i][j]==1)
        return 0;
    if(i>1)
    seed_fill(a,i-1,j);
    if(j>1)
    seed_fill(a,i,j-1);
    if(i<3)
    seed_fill(a,i+1,j);
    if(j<3)
    seed_fill(a,i,j+1);
    
}
int main()
{
    int a[][4]={{1,0,0,1},
                {1,0,0,1},
                {1,0,0,1}};
    for(int i=0;i<12;i++)
        if(a[i/4][i%4]==0)
            seed_fill(a,i/4,i%4);
     printf("%d\n",re);
     return 0;
}

学习需要安静。。海盗要重新来过。。
2008-04-26 11:22
yxwsbobo
Rank: 5Rank: 5
等 级:职业侠客
帖 子:345
专家分:306
注 册:2007-10-29
收藏
得分:0 
````````我的思路

用while读取每一行,并同时纪录两个连续*的起始和结束位置,读取下一行的时候,遇到的*进行向上探测,如果是连着的话,再把他们的面积加到一起,继续用样的循环

How are you 怎么是你?
How old are you   怎么老是你?
2008-04-26 11:28
SNAKEQX
Rank: 1
等 级:新手上路
帖 子:112
专家分:3
注 册:2006-4-11
收藏
得分:0 
额。。。。。这个 ,我尽量不看SKD的代码把此题完成吧,但是到现在代码调试还是有错误。给我点时间,我很笨。。。。。。。。。
2008-04-27 20:04
SNAKEQX
Rank: 1
等 级:新手上路
帖 子:112
专家分:3
注 册:2006-4-11
收藏
得分:0 
#include <stdio.h>
#include <stdlib.h>

////////////////////////////////
// Global variable
int nMap[1100][100];// Max map buff
int nMaxArea=0;       // final output
int nTmax=0;          // save temp max area
int nWidth=0,nHeight=0;

////////////////////////////////
// Function protocol
int SeedFill(int, int);// mian algorithm

////////////////////////////////
// mian
int main(int argc, char *argv[])
{
  char ct;
  FILE *in,*out;
  
  in=fopen("satpix.in","r");
  out=fopen("satpix.out","w");
  
  // Map ini
  // map description:
  // 0: not usable   1: usable   2:already counted
  fscanf(in,"%d%d",&nWidth,&nHeight);
  printf("%d %d\n",nWidth,nHeight);
  fscanf(in,"%c",&ct);// be careful of '\n'
  for(int i=0;i<nHeight;i++)
    for (int j=0;j<=nWidth;j++)// be careful of '\n'
    {
        fscanf(in,"%c",&ct);
        if (ct=='.') nMap[i][j]=0;
        if (ct=='*') nMap[i][j]=1;
    }  
         
  //print Map
  for(int i=0;i<nHeight;i++)
  {
      for (int j=0;j<nWidth;j++)
      {
          printf("%d",nMap[i][j]);
      }
      printf("\n");
  }
     

  // Main algorithm here
  for (int i=0;i<nHeight;i++)
    for (int j=0;j<nWidth;j++)
    {
        SeedFill(i,j);
        if(nTmax>nMaxArea) nMaxArea=nTmax;
        nTmax=0;
    }
   
  //print Map
  printf ("\n");
  for(int i=0;i<nHeight;i++)
  {
      for (int j=0;j<nWidth;j++)
      {
          printf("%d",nMap[i][j]);
      }
      printf("\n");
  }     

  printf ("%d\n",nMaxArea);
  fprintf(out,"%d",nMaxArea);
  
  system("PAUSE");    
  fclose(in);// discard files
  fclose(out);
  return 0;
}

/////////////////////////////////
// Functions
int SeedFill(int width, int height)
{
    // if this position is avaliable and not counted!
    if (nMap[width][height]!=1) return 0;
    nTmax++;//temp area +1
    nMap[width][height]=2;//flag here countec
   
    // if not the most right
    if (width<(nWidth-1))
        SeedFill(width+1,height);
        
    // if not the most left   
    if (width>0)
        SeedFill(width-1,height);
        
    // if not the most up   
    if (height>0)
        SeedFill(width,height-1);
        
    // if not the most down   
    if (height<(nHeight-1))
        SeedFill(width,height+1);
              
    return 0;
}

[[it] 本帖最后由 SNAKEQX 于 2008-4-29 12:08 编辑 [/it]]
2008-04-29 12:05
SNAKEQX
Rank: 1
等 级:新手上路
帖 子:112
专家分:3
注 册:2006-4-11
收藏
得分:0 
文件satpix.in
10 5
..........
**********
..........
..........
..........

按说结果是10
但是输出的是5。
2008-04-29 12:06
SNAKEQX
Rank: 1
等 级:新手上路
帖 子:112
专家分:3
注 册:2006-4-11
收藏
得分:0 
原因找到了。
int SeedFill(int width, int height)
{
    // if this position is avaliable and not counted!
    if (nMap[height][width]!=1) return 0;
    nTmax++;//temp area +1
    //nMap[width][height]=2;这里错了,正确的在下一行
    nMap[height][width]=2;
   
    // if not the most right
    if (width<(nWidth-1))
        SeedFill(width+1,height);
        
    // if not the most left   
    if (width>0)
        SeedFill(width-1,height);
        
    // if not the most up   
    if (height>0)
        SeedFill(width,height-1);
        
    // if not the most down   
    if (height<(nHeight-1))
        SeedFill(width,height+1);
              
    return 0;
}

但是还是不对,数组里有未被访问的区域。
我要晕了。

[[it] 本帖最后由 SNAKEQX 于 2008-4-29 13:37 编辑 [/it]]
2008-04-29 13:10
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
搜索到0就递归..然后把下找下一个0保证都找到

学习需要安静。。海盗要重新来过。。
2008-04-29 13:14
SNAKEQX
Rank: 1
等 级:新手上路
帖 子:112
专家分:3
注 册:2006-4-11
收藏
得分:0 
找到了。在这里
  // Main algorithm here
  for (int i=0;i<nHeight;i++)
    for (int j=0;j<nWidth;j++)
    {
        //SeedFill(i,j);
        SeedFill(j,i);
        if(nTmax>nMaxArea) nMaxArea=nTmax;
        nTmax=0;
    }
2008-04-29 13:39
SNAKEQX
Rank: 1
等 级:新手上路
帖 子:112
专家分:3
注 册:2006-4-11
收藏
得分:0 
终于搞定了。下面是完全的代码。
//PROG:satpix
//LANG:C++
//ID:qian.xin1
#include <stdio.h>
#include <stdlib.h>

////////////////////////////////
// Global variable
int nMap[1100][100];// Max map buff
int nMaxArea;       // final output
int nTmax=0;          // save temp max area
int nWidth=0,nHeight=0;

////////////////////////////////
// Function protocol
int SeedFill(int, int);// mian algorithm

////////////////////////////////
// mian
int main(int argc, char *argv[])
{
  char ct;
  FILE *in,*out;
  
  in=fopen("satpix.in","r");
  out=fopen("satpix.out","w");
  
  // Map ini
  // map description:
  // 0: not usable   1: usable   2:already counted
  fscanf(in,"%d%d",&nWidth,&nHeight);
  printf("%d %d\n",nWidth,nHeight);
  fscanf(in,"%c",&ct);// be careful of '\n'
  for(int i=0;i<nHeight;i++)
    for (int j=0;j<=nWidth;j++)
    {
        fscanf(in,"%c",&ct);
        if (ct=='.') nMap[i][j]=0;
        if (ct=='*') nMap[i][j]=1;
    }  
         
  // Main algorithm here
  for (int i=0;i<nHeight;i++)
    for (int j=0;j<nWidth;j++)
    {
        SeedFill(j,i);
        if(nTmax>nMaxArea) nMaxArea=nTmax;
        nTmax=0;
    }
   
   
  fprintf(out,"%d\n",nMaxArea);
      
  fclose(in);// discard files
  fclose(out);
  return 0;
}

/////////////////////////////////
// Functions
int SeedFill(int width, int height)
{
    // if this position is avaliable and not counted!
    if (nMap[height][width]!=1) return 0;
    nTmax++;//temp area +1
    nMap[height][width]=2;//flag here counted
   
    // if not the most right
    if (width<(nWidth-1))
        SeedFill(width+1,height);
        
    // if not the most left   
    if (width>0)
        SeedFill(width-1,height);
        
    // if not the most up   
    if (height>0)
        SeedFill(width,height-1);
        
    // if not the most down   
    if (height<(nHeight-1))
        SeedFill(width,height+1);
              
    return 0;
}
2008-04-29 13:43
SNAKEQX
Rank: 1
等 级:新手上路
帖 子:112
专家分:3
注 册:2006-4-11
收藏
得分:0 
ANALYSIS MODE
Submit solutions for your own enjoyment.
Evaluating 'satpix'---------------------
* Compiling C++ program satpix
  > Compile: OK
* Executing
  > View Test Data:
    Run  #1  Run  #3  Run  #5  Run  #7  Run  #9  Run #11
    Run  #2  Run  #4  Run  #6  Run  #8  Run #10
  > Run 1: OK [0.000 secs, 3272 KB]
  > Run 2: OK [0.011 secs, 3272 KB]
  > Run 3: OK [0.000 secs, 3272 KB]
  > Run 4: OK [0.000 secs, 3268 KB]
  > Run 5: OK [0.000 secs, 3268 KB]
  > Run 6: OK [0.011 secs, 3272 KB]
  > Run 7: OK [0.000 secs, 3272 KB]
  > Run 8: OK [0.022 secs, 3268 KB]
  > Run 9: OK [0.011 secs, 3272 KB]
  > Run 10: OK [0.022 secs, 3272 KB]
  > Run 11: OK [0.032 secs, 5060 KB]
Analysis mode: Program NOT saved for grading.

acm给的反馈。
2008-04-29 13:43
快速回复:[已解决]最大连续的面积。谢谢大家,特别是种子染色法。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013390 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved