SOJ 2409 The Best Seat in ACM Contest (BFS)

题目描述

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).

 

输入

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
 
 
这道题目做了两天,提交了8遍,各种错误,一度想要放弃了,后来想想算了,还是好好做吧,看了大神的代码之后,还是有一个地方不是很明白,希望大家能够帮帮我:
这个题目的意思是:找每个位置的价值,有两种可能情况:
1),如果周围四个方向的价值比你的高;2),如果周围四个方向的价值比你的低;
但是具有相同的计算方法:
都是用其余四个方向的价值-当前你的座位价值,如果满足四个方向都有座位,则没有座位的-1.
求最大价值:
#include <iostream>
using namespace std;
int count=0;
int k=1;
int n,m;
int str[105][105];
int value[105][105];
int dist[4][2]= {0,1,1,0,0,-1,-1,0};
void bfs(int xx,int yy)
{
    int x1,y1;
    for(int i=0; i<4; i++)
    {
        x1=xx+dist[i][0];
        y1=yy+dist[i][1];
        if(x1<0||x1>=n||y1<0||y1>=m)
        {
            count--;
            continue;
        }
        if(str[x1][y1]>str[xx][yy])
            count+=str[x1][y1]-str[xx][yy];
        else
            count-=str[xx][yy]-str[x1][y1];
    }
}
int main()
{
    int t;
    int x,y;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
                cin>>str[i][j];
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
            {
                bfs(i,j);
                value[i][j]=count;
                count=0;
            }
        //cout<<"df"<<endl;
        int max=value[0][0];
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
            {
                //cout<<value[i][j]<<" ";
                if(value[i][j]>=max)
                {
                    max=value[i][j];
                    x=i;
                    y=j;
                }
            }
        cout<<"Case "<<k++<<": "<<max<<" "<<x+1<<" "<<y+1<<endl;
    }
    return 0;
}

上述为AC代码,但是我不明白为什么下面的这个不可以:
#include <iostream>
using namespace std;
int count=0;
int k=1;
int n,m;
int str[105][105];
int value[105][105];
int dist[4][2]= {0,1,1,0,0,-1,-1,0};
void bfs(int xx,int yy)
{
    int x1,y1;
    for(int i=0; i<4; i++)
    {
        x1=xx+dist[i][0];
        y1=yy+dist[i][1];
        if(x1<0||x1>=n||y1<0||y1>=m)
        {
            count--;
            continue;
        }
        if(str[x1][y1]>str[xx][yy])
            count+=str[x1][y1]-str[xx][yy];
        else
            count-=str[xx][yy]-str[x1][y1];
    }
}
int main()
{
    int t;
    int x,y;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
                {cin>>str[i][j];

                bfs(i,j);
                value[i][j]=count;
                count=0;
            }
        //cout<<"df"<<endl;
        int max=value[0][0];
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
            {
                //cout<<value[i][j]<<" ";
                if(value[i][j]>=max)
                {
                    max=value[i][j];
                    x=i;
                    y=j;
                }
            }
        cout<<"Case "<<k++<<": "<<max<<" "<<x+1<<" "<<y+1<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值