Sky Map

来源:互联网 发布:spss软件如何下载 编辑:程序博客网 时间:2024/05/18 03:01


Sky Map

You are makingsky map to draw constellation (set of starts) onthe sky in the night.

A constellation is compossed of stars and these stars arelocated in their upper, left, right and lower part.

(Only a star also can be a constellation.)

Number 1 means a star and 0 means void from the inputs.

If two starts are located in each diagonal part,

then the stars can not be a constellation.

How many constellations are in your sky map, and how many stars in the biggestconstellation?

Time limit : 2 sec (Java : 4 sec)

[Input]
There can be more than one test case in the input.

The first line has T, the number of test cases.

Then the totally T test cases are provided in thefollowing lines (T<=10)

In each test case, the first line has an integer N(5 N 25), the size ofmap.

The map is a square, and is represented as N x N matrix.

For next N lines, each contains each raw of the matrix

[Output]

For each test case, you should print "Case #T"in the first line where T means the case number. For each test case, you shouldoutput the number of constellation and the number of starts in the greatestconstellation separated by blank.


[I/O Example]
Input
2
7
0 1 1 0 0 0 0
0 1 1 0 1 0 0
1 1 1 0 1 0 1
0 0 0 0 1 1 1
1 0 0 0 0 0 0
0 1 1 1 1 1 0
0 1 0 1 1 0 0
5
0 1 0 0 0
0 1 0 0 0
0 1 0 0 0
0 0 0 0 0
0 0 0 0 0


Output

Case #1

4 8

Case #2

1 3


#include<stdio.h>

#define MAX_N 26

int Array[MAX_N][MAX_N];

int Visit[MAX_N][MAX_N];

int Table[MAX_N];

int temp[MAX_N];

int num = 0;

int Answer = 0 ;

int a;

int N=0;


int Find(int i,int j)

{

     Visit[i][j] = 1 ;

    if((j+1)<=(N-1))//right

     {

          if((Visit[i][j+1] == 0)&&(Array[i][j+1] == 1))

          {         

                num++;

                Find(i,j+1);         

          }

     }

     

     if((i+1)<=(N-1))//down

     {

          if((Visit[i+1][j] == 0)&&(Array[i+1][j] == 1))

          {         

                num++;

                Find(i+1,j);         

          }

     }

 

     if((j-1)>=0)//left

     {

          if((Visit[i][j-1] == 0)&&(Array[i][j-1] == 1))

          {         

                num++;

                Find(i,j-1);         

          }

     }

 

     if((i-1)>=0)//up

     {

          if((Visit[i-1][j] == 0)&&(Array[i-1][j] == 1))

          {         

                num++;

                Find(i-1,j);         

          }

     }    

     returnnum ;

}

int main(void)

{

     intT, test_case;

     /*

        The freopen function below opens input.txt file in read only mode, andafterward,

        the program will read from input.txt file instead of standard(keyboard)input.

        To test your program, you may save input data in input.txt file,

        and use freopen function to read from the file when using scanffunction.

        You may remove the comment symbols(//) in the below statement and useit.

        But before submission, you must remove the freopen function or rewritecomment symbols(//).

       */

     freopen("sample_input.txt","r", stdin);

 

     /*

        If you remove the statement below, your program's output may not berocorded

        when your program is terminated after the time limit.

        For safety, please use setbuf(stdout, NULL); statement.

       */

     setbuf(stdout, NULL);

 

     scanf("%d", &T);

     for(test_case= 0; test_case < T; test_case++)

     {

          int i,j;        

          int ret = 0 ;

          a = 0;

          num = 0;

 

          scanf("%d",&N);

          

          for(i=0;i<N;i++)

          {

                for(j=0;j<N;j++)

                {

                     scanf("%d",&Array[i][j]);

                }

          }

          

          for(i=0;i<N;i++)

          {

                for(j=0;j<N;j++)

                {

                     Visit[i][j] = 0 ;

                }

          }

          

          for(i=0;i<N;i++)

          {

                for(j=0;j<N;j++)

                {

                     if((Visit[i][j]== 0)&&(Array[i][j] == 1))

                     {

                           num++;

                           Find(i,j);

                           if(num >= 1)

                           {

                                 temp[a] = num;

                                 if((a >= 1)&&(temp[a] < temp [a-1]))

                                 {

                                      temp[a] =temp [a-1];

                                 }

                                 a++;

                           }

                           num = 0 ;

                     }

                }

          }

          /////////////////////////////////////////////////////////////////////////////////////////////

          /*

             Implement your algorithm here.

             The answer to the case will be stored in variable Answer.

            */

          /////////////////////////////////////////////////////////////////////////////////////////////

          //Answer = 0;

 

          // Print the answer to standardoutput(screen).

          printf("Case #%d\n", test_case+1);

          printf("%d %d\n", a,temp[a-1]);

     }

     return0;//Your program shouldreturn 0 on normal termination.

}



0 0
原创粉丝点击