山东省第三届ACM省赛The Best Seat in ACM Contest

题目描述

 

Cainiao is a university student who loves ACM contest very much. It is a festival for him once when he attends ACM Asia Regional Contest because he always can find some famous ACMers there.
Cainiao attended Asia Regional Contest Fuzhou Site on November 20, 2011. After he got seat map, he wanted to know which seat is the best one.
Cainiao have joined so many QQ Group about ACM/ICPC that he is almost familiar with the strength of each team. In his mind, the value of a seat is defined as following:

1. Strength of each team can be expressed as a positive integer.
2. The value of a seat is related to the adjacent seat (up/down/left/right, only four directions being considering).
3. For an adjacent seat, if the strength of this team is stronger than yours, the absolute value of difference of two teams should be added to your seat, otherwise, the absolute value of difference should be subtracted from your seat.
4. If the adjacent seat is empty (which means you are located at the most left/right/up/down), the value of your seat should be subtracted 1.
5. The best one in a contest is the seat that has the highest value.
6. The initial value of the seat is ZERO.

For example, there are 12 ( 3 X 4 ) teams in a contest, the strength of each team is as figure (a), and then you can calculate the value of each seat as figure (b).
山东省第三届ACM省赛The Best Seat in ACM Contest

输入

Input contain a positive integer T( T <=50 ) in the first line, which means T cases.
The first line of each case contains two positive integers N and M (3 <= N, M <= 20) which means the row and column number of the teams, then N rows following, each line contains M positive integers that represent the strengths of the teams.

输出

For each case, first output the case number, and then output the value and row number and column number of the best seat in one line for each case. 
If there are multiple solutions for one case, you should output the seat whose row number is largest and only output the seat whose column number is largest if still overlapping.

示例输入

1

3 4

1 5 3 4

6 3 3 4

4 3 2 1

示例输出

Case 1: 7 1 1

 

此题为搜索,bfs这个题要注意边界,主要是对数组进行清零然后数值加减即可

 1 #include<stdio.h>

 2 #include<string.h>

 3 #include<stdio.h>

 4 const int maxn = 31 ;

 5 int ch[maxn][maxn] ;

 6 int sh[maxn][maxn];

 7 int main()

 8 {

 9     int n ;

10     scanf("%d",&n) ;

11     for(int i = 1 ; i <= n ; i++)

12     {

13         int a,b ;

14         memset(ch,0,sizeof(ch)) ;

15         scanf("%d %d",&a,&b) ;

16         for(int j = 1 ; j <= a ; j++)

17         {

18             for(int k = 1 ; k <= b ; k++)

19             {

20                 scanf("%d",&ch[j][k]) ;

21             }

22         }

23         printf("Case %d: ",i) ;

24         memset(sh,0,sizeof(sh)) ;

25         for(int j = 1 ; j <= a ; j++)

26         {

27             for(int k = 1 ; k <= b ; k++)

28             {

29                 if(ch[j-1][k])

30                     sh[j][k] += (ch[j-1][k] -ch[j][k] );

31                 else sh[j][k]-- ;

32                 if(ch[j+1][k])

33                     sh[j][k] += (ch[j+1][k] -ch[j][k]) ;

34                 else sh[j][k]-- ;

35                 if(ch[j][k-1])

36                     sh[j][k] += (ch[j][k-1] -ch[j][k] );

37                 else sh[j][k]-- ;

38                 if(ch[j][k+1])

39                     sh[j][k] += (ch[j][k+1] -ch[j][k]) ;

40                 else sh[j][k]-- ;

41             }

42         }

43         int ii=1,jj=1 ;

44         int max = sh[1][1] ;

45         for(int j = 1 ; j <= a ; j++)

46         {

47             for(int k = 1 ; k <= b ; k++)

48             {

49                 if(sh[j][k] > max)

50                 {

51                     max = sh[j][k] ;

52                     ii = j ;

53                     jj = k ;

54                 }

55             }

56         }

57         printf("%d %d %d\n",max,ii,jj) ;

58     }

59     return 0 ;

60 }
View Code

在网上看到大神解得,用的函数调用

 1 #include<stdio.h>

 2 int y[107][107];

 3 int z[107][107];

 4 int m,n;

 5 int count=0,number=0,max,x1=0,y1=0,flag=1;

 6 int dir[4][2]= {{-1,0},{1,0},{0,1},{0,-1}};

 7 void bfs(int x2,int y2)

 8 {

 9     int xx,yy;

10     for(int e=0; e<4; e++)

11     {

12         xx=x2+dir[e][0];

13         yy=y2+dir[e][1];

14         if(xx<0||xx>=m||yy<0||yy>=n)

15         {

16             count--;

17             continue;

18         }

19         if(y[xx][yy]>y[x2][y2])

20         {

21             number=y[xx][yy]-y[x2][y2];

22             count+=number;

23         }

24         else

25         {

26             number=y[x2][y2]-y[xx][yy];

27             count-=number;

28         }

29     }

30 }

31 int main()

32 {

33     int t;

34     scanf("%d",&t);

35     while(t--)

36     {

37 

38         scanf("%d%d",&m,&n);

39         for(int a=0; a<m; a++)

40             for(int b=0; b<n; b++)

41                 scanf("%d",&y[a][b]);

42         for(int c=0; c<m; c++)

43             for(int d=0; d<n; d++)

44             {

45                 bfs(c,d);

46                 z[c][d]=count;

47                 count=0;

48             }

49         max=z[0][0];

50         for(int g=0; g<m; g++)

51             for(int h=0; h<n; h++)

52             {

53                 if(z[g][h]>=max)

54                 {

55                     max=z[g][h];

56                     x1=g;

57                     y1=h;

58                 }

59             }

60         printf("Case %d: %d %d %d\n",flag++,max,x1+1,y1+1);

61 

62     }

63     return 0;

64 }
View Code

看起来高级了不少,而且很整洁

你可能感兴趣的:(test)