codeforces 359A- Table(简单思维)

codeforces 359A- Table


不得不说codeforces上的题目,思维真的很活跃。


上一期的A题,写了那么多,这个题我也用最朴实的想法来写的时候,发现超时了!!!【代码二】


有提交错误时的测试数据得出:输出的数据只有2种可能“2”和“4”


经过对题目的本质的分析,题目的格子处理只有2种可能:

<1>“好格子”都在中间,选任意一个“好格子”,需要涂4次,可将棋盘全部涂满;

<2>“存在”位于边界的“好格子”,选任意一个边界上的“好格子”,需要涂2次即可将棋盘全部涂满。


找到本质后,题目就是“so easy!”!


对输入的棋盘的“好格子”的情况进行处理,要是有处于边界上的“好格子”,直接输出“2”;否则,直接输出“4”。

(见【代码一】)



【代码一】

#include<stdio.h>
int main()
{
    int n,m;
    int u;
    int i,j;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int flag=0;
        for(i=0;i<n;i++)
            for(j=0;j<m;j++)
            {
                scanf("%d",&u);
                if(u)
                    if(i==0||i==n-1||j==0||j==m-1)  //边界有good cell
                    flag++;
            }
        if(flag)printf("2\n");
        else printf("4\n");
    }
    return 0;
}


【代码二

//Warnning:This is the TLE code , please don't copy it for study!
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int n,m;    //the size of the table
int map[52][52];  //the table ,the "0" is where wait for paint
int gn;  //good point number
int sum;  //all the number of point need to be paint
int ans;  //the step to paint all the point ,the ans what we need
int x1,y1,x2,y2;
struct node  //the infromation of the good point
{
    int x;
    int y;
    int max;  //the max point can paint if we choice this good point
    int maxdir;  //the direction the max point can paint1 2
                                              //        3 4
}gp[2500];
bool cmp(node a,node b)  //to sort the good point by it's max point can be paint
{
    return b.max-a.max;
}
void initial()  //initial the infromations
{
    int u;
    int i,j;
    ans=0;
    sum=n*m;
    gn=0;
    memset(map,0,sizeof(map));
    for(i=1;i<=n;i++)
        for(j=1;j<=m;j++)
        {
            scanf("%d",&u);
            if(u)
            {
                gp[gn].x=i;
                gp[gn].y=j;
                gn++;
            }
        }
}
void chang()  //choice one good point and paint some point
{
    int i,j;
    sum-=gp[0].max;
    if(gp[0].maxdir==1)
    {
        x1=1;x2=gp[0].x;
        y1=1;y2=gp[0].y;
    }
    else if(gp[0].maxdir==2)
    {
        x1=1;x2=gp[0].x;
        y1=gp[0].y;y2=m;
    }
    else if(gp[0].maxdir==3)
    {
        x1=gp[0].x;x2=n;
        y1=1;y2=gp[0].y;
    }
    else
    {
        x1=gp[0].x;x2=n;
        y1=gp[0].y;y2=m;
    }
    for(i=x1;i<=x2;i++)
        for(j=y1;j<=y2;j++)
            map[i][j]=1;
}
int cout1()
{
    int cou=0;
    int i,j;
    for(i=x1;i<=x2;i++)
        for(j=y1;j<=y2;j++)
            if(!map[i][j])cou++;
    return cou;
}
void updata()  //updata all the point number the good point can paint
{
    int i,j,k;
    int maxn;
    for(k=0;k<gn;k++)
    {
        gp[k].max=0;
        x1=1;x2=gp[0].x;
        y1=1;y2=gp[0].y;
        maxn=cout1();
        if(maxn>gp[k].max)
        {
            gp[k].max=maxn;
            gp[k].maxdir=1;
        }
        x1=1;x2=gp[0].x;
        y1=gp[0].y;y2=m;
        maxn=cout1();
        if(maxn>gp[k].max)
        {
            gp[k].max=maxn;
            gp[k].maxdir=2;
        }
        x1=gp[0].x;x2=n;
        y1=1;y2=gp[0].y;
        maxn=cout1();
        if(maxn>gp[k].max)
        {
            gp[k].max=maxn;
            gp[k].maxdir=3;
        }
        x1=gp[0].x;x2=n;
        y1=gp[0].y;y2=m;
        maxn=cout1();
        if(maxn>gp[k].max)
        {
            gp[k].max=maxn;
            gp[k].maxdir=4;
        }
    }
    sort(gp,gp+gn,cmp);
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        initial();
        updata();
        while(sum)
        {
            ans++;
            if(sum==gp[0].max)break;
            chang();
            updata();
        }
        cout<<ans<<endl;
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值