zoj3780 Paint the Grid Again 拓扑排序模拟

比赛时候看完题目就觉得是拓扑排序,当时心里隐隐觉得跟相框叠加那个题有点相似的

然后wzy问我no solution 是什么情况,我就一直去想是不是构成了什么排列就一定是no solution

其实只用再参考相框叠加那个题往前想一丁点就够了,就是从最后涂的那一层开始往前找,每一次都必然有一行或一整列是一样的

每次按逆字母序删除这一行或列就是了。

拓扑排序的题总是类似而且简单的,找到关系,敲代码完全不存在问题。

#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#define inf 0x3f3f3f3f
#define ll long long
#define mod 1000000007
using namespace std;

struct node
{
    char dir;
    int index;
}ans[1010];

int r[510],c[510],n;
char mp[510][510];

bool ok()
{
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            if(mp[i][j]!=‘.‘)
                return 1;
    return 0;
}

int main()
{
    int t,i,j,flag,cnt;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=0;i<n;i++)
            scanf("%s",mp[i]);
        memset(c,0,sizeof c);
        memset(r,0,sizeof r);
        cnt=0;flag=1;
        while(ok())
        {
           /* for(i=0;i<n;i++,putchar(‘\n‘))
                for(j=0;j<n;j++)
                    putchar(mp[i][j]);*/
            //从最后一次操作开始找 要使顺序是字母序 那么倒着找就是逆字母序
            //因此 先R后C 从右下到左上

            if(flag==0) break;//没找到符合的行列 且整个没有结束
            for(i=n-1;i>=0;i--)
            {
                if(r[i]) continue;
                flag=1;
                for(j=0;j<n;j++)
                {
                    if(c[j]) continue;
                    if(mp[i][j]!=‘X‘)
                    {
                        flag=0;
                        break;
                    }
                }
                if(flag)
                {
                    for(j=0;j<n;j++)
                        mp[i][j]=‘.‘;
                    r[i]=1;
                    ans[cnt].dir=‘R‘;
                    ans[cnt].index=i+1;
                    cnt++;
                    break;
                }
            }
            if(flag) continue;

            for(i=n-1;i>=0;i--)
            {
                if(c[i]) continue;
                flag=1;
                for(j=0;j<n;j++)
                {
                    if(r[j]) continue;
                    if(mp[j][i]!=‘O‘)
                    {
                        flag=0;
                        break;
                    }
                }
                if(flag)
                {
                    for(j=0;j<n;j++)
                        mp[j][i]=‘.‘;
                    c[i]=1;
                    ans[cnt].dir=‘C‘;
                    ans[cnt].index=i+1;
                    cnt++;
                    break;
                }
            }
        }
        if(flag)
        {
            for(i=cnt-1;i>0;i--)
                printf("%c%d ",ans[i].dir,ans[i].index);
            printf("%c%d\n",ans[i].dir,ans[i].index);
        }
        else printf("No solution\n");
    }
    return 0;
}

zoj3780 Paint the Grid Again 拓扑排序模拟

时间: 2024-08-02 02:47:35

zoj3780 Paint the Grid Again 拓扑排序模拟的相关文章

zoj 3780 Paint the Grid Again (拓扑排序)

Paint the Grid Again Time Limit: 2 Seconds      Memory Limit: 65536 KB Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black or white). Leo has a magical brush which can paint any row with black color, or an

ZOJ 3780 E - Paint the Grid Again 拓扑排序

https://vjudge.net/problem/49919/origin 题意:给你n*n只出现O和X的字符阵.有两种操作,一种操作Ri将i行全变成X,一种操作Ci将i列全变成O,每个不同的操作最多进行一次.现给出目标状态,求空盘下的字典序最小的操作顺序是怎样的. 思路:拿到题目看起来很复杂,但仔细读题会发现X和O只由特定操作出现,且操作只进行一次,那么单独地考虑每行每列,如i行中出现了O,且存在X(如果不存在X那么可以略去Ri操作),说明在Ri操作后进行了Cj的操作,同样的方法去考虑列,

ZOJ - 3780-Paint the Grid Again-(拓扑排序)

Description Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black or white). Leo has a magical brush which can paint any row with black color, or any column with white color. Each time he uses the brush, the

ZOJ 3780 Paint the Grid Again(隐式图拓扑排序)

Paint the Grid Again Time Limit: 2 Seconds      Memory Limit: 65536 KB Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black or white). Leo has a magical brush which can paint any row with black color, or an

2014 Super Training #4 D Paint the Grid Again --模拟

原题:ZOJ 3780 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 刚开始看到还以为是搜索题,没思路就跳过了.结果后来发现就是一个简单的模拟啊,因为每行每列都只能消去一次,直接慢慢消去就好了,因为按字典序从小到大,那就按行从大到小,列从大到小的顺序来消就可以了,消完了标记一下,把那行或者那列的元素都赋为一个特殊的字符'*'即可. 还是应该多思考啊,不要被题目吓到了.探寻题目的本质才能更好的解题. 代码: #i

Day1:T1 模拟 T2 拓扑排序

T1:模拟 自己第一天的简直跟白痴一样啊...模拟都会打错.. 当时貌似在更新最大值的时候打逗比了... if((sum[x]==max && x<maxh) || sum[x]>max){  max=sum[x];  maxh=x; //现在(也就是9月+)再看,脑袋里只有sortsortsort,连最基本的更新最大指都忘了....智商唉.... 附上代码: #include<cstdio> #include<cstring> using namesp

【模拟题(63550802...)】解题报告【贪心】【拓扑排序】【找规律】【树相关】

目录: 1.A[树相关]    2.B[找规律]    3.C[贪心][拓扑排序] A. 描述(A 输入文件 : A.input 输出文件 : A.output)一个城市的构成是一颗n 个节点的树(2 ≤ n ≤ 200), 现在需要在树中找出两条不相交的路径(即两条路径不能有重边也不能有重点),使得路径的长度的乘积最大.输入描述第一行一个数n 表示这个城市一共有 n 个节点.接下来 n-1 行,每行两个数ai 和bi (1 ≤ ai,bi ≤ n ),分别表示从ai 到bi,有一条边,每条边的

【noip模拟赛4】找啊找啊找BF 拓扑排序

描述 sqybi上次找GF的工作十分不成功,于是依旧单身的他在光棍节前的某天突发奇想,要给自己找一个BF(这里指的是男性的好朋友……),这样既可以和人分享内心的压抑(路人甲:压抑还分享么……),也可以保证自己能够有资格过今年的光棍节. 这次sqybi为了增加成功率,希望先对他提前确定的几个人定一下重要度.每个人的重要度都用一个自然数表示,这里的自然数包括0. 现在sqybi的心目中已经有了一些对于这些人的看法.他对于某个人的看法是基于另一个人的基础之上的,比如他会认为a比b的重要度至少大k. 现

【CSP模拟赛】益智游戏(最短路&amp;拓扑排序)

题目描述 小P和小R在玩一款益智游戏.游戏在一个正权有向图上进行. 小P 控制的角色要从A 点走最短路到B 点,小R 控制的角色要从C 点走最短路到D 点. 一个玩家每回合可以有两种选择,移动到一个相邻节点或者休息一回合. 假如在某一时刻,小P 和小R 在相同的节点上,那么可以得到一次特殊奖励,但是在每 个节点上最多只能得到一次. 求最多能获得多少次特殊奖励 输入格式 第一行两个整数n,m 表示有向图的点数和边数. 接下来m 行每行三个整数xi,yi,li,表示从xi 到yi 有一条长度为li