codeforces 432E Square Tiling 贪心

题目大意:给定一个n?m的矩阵,要求用正方形砖块覆盖,使得任意两块同颜色的正方形砖块不相邻且字典序最小

枚举每一块砖,如果这个位置为空,就填入字典序最小的砖块,然后将边长一格格拓展

如果当前右侧位置可以填入字典序更小的砖块,就不拓展

否则判断能否拓展并拓展

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define M 110
using namespace std;
int m,n;
char ans[M][M];
bool Able(int x,int y,char c)
{
    static const int dx[]={1,-1,0,0};
    static const int dy[]={0,0,1,-1};
    int i;
    for(i=0;i<4;i++)
        if(ans[x+dx[i]][y+dy[i]]==c)
            return false;
    return true;
}
void Extend(int x,int y)
{
    int i,j;
    for(i=‘A‘;i<=‘Z‘;i++)
        if(Able(x,y,i))
        {
            ans[x][y]=i;
            break;
        }
    for(i=2;x+i-1<=m&&y+i-1<=n;i++)
    {
        for(j=‘A‘;j<ans[x][y];j++)
            if(Able(x,y+i-1,j))
                return ;
        for(j=x;j<=x+i-1;j++)
            if(ans[j][y+i-1])
                return ;
        for(j=y;j<=y+i-1;j++)
            if(ans[x+i-1][j])
                return ;
        if(ans[x-1][y+i-1]==ans[x][y])
            return ;
        if(ans[x+i-1][y-1]==ans[x][y])
            return ;
        for(j=x;j<=x+i-1;j++)
            if(ans[j][y+i]==ans[x][y])
                return ;
        for(j=y;j<=y+i-1;j++)
            if(ans[x+i][j]==ans[x][y])
                return ;
        for(j=x;j<=x+i-1;j++)
            ans[j][y+i-1]=ans[x][y];
        for(j=y;j<=y+i-1;j++)
            ans[x+i-1][j]=ans[x][y];
    }
}
int main()
{
    int i,j;
    cin>>m>>n;
    for(i=1;i<=m;i++)
        for(j=1;j<=n;j++)
            if(!ans[i][j])
                Extend(i,j);
    for(i=1;i<=m;i++)
        puts(ans[i]+1);
    return 0;
}
时间: 2024-08-30 03:22:29

codeforces 432E Square Tiling 贪心的相关文章

Codeforces 432E Square Tiling(构造+贪心)

我们通常这么写 using (SqlDataReader drm = sqlComm.ExecuteReader()) { drm.Read();//以下把数据库中读出的Image流在图片框中显示出来. MemoryStream ms = new MemoryStream((byte[])drm["Logo"]); Image img = Image.FromStream(ms); this.pictureBox1.Image = img; } 我的写数据 private void b

codeforces 432E Square Tiling

codeforces 432E Square Tiling 题意 题解 代码 #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define mp make_pair #define pb push_back #define rep(i, a, b) for(int i=(a); i<(b); i++) #define sz(x) (int)x.size() #define d

Codeforces Round #300——C贪心——Tourist&#39;s Notes

Description A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, me

Codeforces 77C 树形dp + 贪心

题目链接:点击打开链接 题意: 给定n个点, 每个点的豆子数量 下面是一棵树 再给出起点 每走到一个点,就会把那个点的豆子吃掉一颗. 问:回到起点最多能吃掉多少颗豆子 思路:树形dp 对于当前节点u,先把子节点v都走一次. 然后再往返于(u,v) 之间,直到u点没有豆子或者v点没有豆子. dp[u] 表示u点的最大值.a[u] 是u点剩下的豆子数. #include <cstdio> #include <vector> #include <algorithm> #inc

Codeforces 788A Functions again - 贪心

Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about

Codeforces 486C Palindrome Transformation(贪心)

题目链接:Codeforces 486C Palindrome Transformation 题目大意:给定一个字符串,长度N,指针位置P,问说最少花多少步将字符串变成回文串. 解题思路:其实只要是对称位置不相同的,那么指针肯定要先移动到这里,修改字符只需要考虑两种方向哪种更优即 可.然后将所有需要到达的位置跳出来,贪心处理. #include <cstdio> #include <cstring> #include <cstdlib> #include <vec

codeforces 735C Tennis Championship(贪心+递推)

Tennis Championship 题目链接:http://codeforces.com/problemset/problem/735/C --每天在线,欢迎留言谈论. 题目大意: 给你一个 n (2≤n≤10^18),代表一共有n位参加比赛的选手. 游戏规则: ①每次比赛,输的选手将离开赛场 ②相互比赛的选手 他们的获胜的次数相差不能超过1(获胜4次的选手只能跟3或5次的选手比赛) 问题:最终赢得比赛的选手,胜场最多能为多少. 思路: 贪心:①选一名选手让他一直获胜且优先让他参加比赛 ②当

HDU 5903 Square Distance (贪心+DP)

题意:一个字符串被称为square当且仅当它可以由两个相同的串连接而成. 例如, "abab", "aa"是square, 而"aaa", "abba"不是. 两个长度相同字符串之间的 hamming distance是对应位置上字符不同的位数. 给定一行字符串和 m,输出字典序最小的字符串. 析:首先先用dp判断能不能形成这样的字符串,然后再打印出来,dp[i][j] 表示 i - 中间的数能不能改 j 个字符得到,最后打印

CodeForces 545C Woodcutters (贪心orDP)

[题目链接]:click here~~ [题目大意]: 有n棵树,给出每棵树的位置和高度,然后把树砍掉,树可以向左倒也可以向右倒.输出最多能砍几棵树. [思路]:利用贪心的思想.第一棵树的左边和最后一棵树的右边没树,所以他们向两边倒,然后对于中间的树来说,首先先向左边倒,然后左边距离如果不够的话再向右边倒,向右倒的时候注意更新一下距离. 代码: /* * Problem: CodeForces 545C * Running time: 46MS * Complier: G++ * Author: