codeforce 35C fire again

2017-08-25 17:04:07

writer:pprp

题目描述:

• Codeforces 35C Fire Again
• N*M的格子,最开始有K个点 (坐标给定) 开始着火
• 每一秒着火的点会扩散到与其距离为1的其他点
• 求最后一个着火的点
• 1 ≤ n, m ≤ 2000
• 1 ≤ K ≤ 10

模拟的题,本来想用dfs做感觉有点复杂,

可以通过判断两个点之间横纵距离之和来计算出时间

参见的是cf上某位大佬的代码,差距还是很大,要加油了,

话说cf真是好,越来越觉得cf好用了

代码如下:

/*
theme:cf 35c fire again
writer:pprp
declare:reference from WannabeStronger
type: 模拟
date:2017/8/25
*/

#include <cstdio>
#include <bits/stdc++.h>


using namespace std;

struct point
{
    int x, y;
} pt[11];

const int maxn = 2500;

int mp[maxn][maxn];

int main()
{
    freopen("input.txt","r",stdin);//这几句很神奇,不知道为什么加上这个就可以运行过去,不加就过不去
    freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(false);
    
    int n, m, k;
    cin >> n >> m >>k;

    for(int i = 1; i <= k ; i++)
        cin >> pt[i].x >> pt[i].y;
    //初始化
    for(int i = 1 ; i <= n ; i++)
    {
        for(int j = 1 ; j <= m ; j++)
        {
            mp[i][j] = 1e6;
        }
    }
    //开始枚举着火点的时间
    for(int l = 1 ; l <= k ; l++)
    {
        for(int i = 1 ; i <= n ; i++)
        {
            for(int j = 1 ; j <= m ; j++)
            {
                int _x = abs(i - pt[l].x);
                int _y = abs(j - pt[l].y);
                mp[i][j] = min(_x+_y, mp[i][j]);
            }
        }
    }
    //找到最大值,也就是最晚被烧到的树
    int ans = INT_MIN;
    int ans_x, ans_y;
    for(int i = 1 ; i <= n ; i++)
    {
        for(int j = 1; j <= m ; j++)
        {
            if(ans < mp[i][j])
            {
                ans = mp[i][j];
                ans_x = i;
                ans_y = j;
            }
        }
    }

    cout << ans_x << " " << ans_y << endl;

    return 0;
}

 关于freopen部分的代码找到原因了

题目中有要求所以要这样做

posted @ 2017-08-25 17:08  pprp  阅读(326)  评论(0编辑  收藏  举报