hdu 5386 Cover

Description

You have an matrix.Every grid has a color.Now there are two types of operating:

L x y: for(int i=1;i<=n;i++)color[i][x]=y;

H x y:for(int i=1;i<=n;i++)color[x][i]=y;

Now give you the initial matrix and the goal matrix.There are operatings.Put in order to arrange operatings,so that the initial matrix will be the goal matrix after doing these operatings

It’s guaranteed that there exists solution.

Input

There are multiple test cases,first line has an integer

For each case:

First line has two integer ,

Then lines,every line has integers,describe the initial matrix

Then lines,every line has integers,describe the goal matrix

Then lines,every line describe an operating

Output

For each case,print a line include integers.The i-th integer x show that the rank of x-th operating is

Sample Input

1

3 5

2 2 1

2 3 3

2 1 3

3 3 3

3 3 3

3 3 3

H 2 3

L 2 2

H 3 3

H 1 3

L 2 3

Sample Output

5 2 4 3 1

题目大意:给你n×n的初始图形,和目标图形,还有m个操作,操作和一把一行或一列变成一种颜色。如今问使初始图形变成目标图形的操作的顺序。每一个操作都要用上,且一定有解。

解题思路:初始图形没实用。直接从目标图形開始进行操作。

对于一个操作。推断该操作相应的那一行或一列的颜色,是否除0之外所有都是与该操作变换的颜色同样,是的话,将那一行或一列所有变为0,而且记录该操作,最后逆序输出。

注意,把所有操作遍历一遍是不够的。由于有一些操作是要在别的操作已进行过的基础上才干进行。所以在遍历操作的外层要再加一层循环(cnt

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std; const int N = 205;
const int M = 2005;
typedef long long ll;
int ans[M];
int G1[N][N], G2[N][N];
struct Node{
int pos, val, id;
}H[M], L[M];
int n, m, cntH, cntL; void init() {
memset(L, 0, sizeof(L));
memset(H, 0, sizeof(H));
memset(G2, 0, sizeof(G2));
cntH = cntL = 0;
} void input() {
scanf("%d %d", &n, &m);
int a, b;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
scanf("%d", &G1[i][j]);
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
scanf("%d", &G2[i][j]);
}
}
char s[20];
for (int i = 1; i <= m; i++) {
scanf("%s", s);
scanf("%d %d", &a, &b);
if (strcmp(s, "H") == 0) {
H[cntH].pos = a;
H[cntH].val = b;
H[cntH++].id = i;
} else {
L[cntL].pos = a;
L[cntL].val = b;
L[cntL++].id = i;
}
}
} void solve() {
int cnt = 0;
while (cnt < m) {
for (int i = 0; i < cntH; i++) {
int k = H[i].pos, flag = 1;
if (!k) continue;
for (int j = 1; j <= n; j++) {
if(G2[k][j] && G2[k][j] != H[i].val) {
flag = 0;
break;
}
}
if(flag) {
ans[cnt++] = H[i].id;
for(int j = 1; j <= n; j++) G2[k][j] = 0;
H[i].pos = 0;
}
}
for (int i = 0; i < cntL; i++) {
int k = L[i].pos, flag = 1;
if (!k) continue;
for(int j = 1; j <= n; j++) {
if(G2[j][k] && G2[j][k] != L[i].val) {
flag = 0;
break;
}
}
if(flag) {
ans[cnt++] = L[i].id;
for(int j = 1; j <= n; j++) G2[j][k] = 0;
L[i].pos = 0;
}
}
}
for (int i = m - 1; i >= 1; i--) printf("%d ", ans[i]);
printf("%d\n", ans[0]);
} int main() {
int T;
scanf("%d", &T);
while (T--) {
init();
input();
solve();
}
return 0;
}

hdu 5386 Cover (暴力)的更多相关文章

  1. HDU 5386 Cover

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5386 题目大意:给一个初始矩阵(n×n).一个目标矩阵(n×n)和m个操作,要求找到一种操作顺序,使初 ...

  2. HDU 5386 Cover(模拟)

    Cover Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Subm ...

  3. 暴力/思维 HDOJ 5386 Cover

    题目传送门 /* 题意:给出刷墙的所有的方法,求一种顺序,使得原矩阵刷成目标矩阵 暴力:(题解)我们只要每次找一行或一列颜色除了0都相同的,然后如果有对应的操作,就把这行这列都赋值成0即可 */ /* ...

  4. HDU 6311 Cover (无向图最小路径覆盖)

    HDU 6311 Cover (无向图最小路径覆盖) Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...

  5. HDU 5386 暴力

    题目 也是个坑题,可惜没有发现这是个水题,被矩阵的气势吓住了,其实后来做出来的人挺多,就应该想到没那么难了.(两个队友陷入DP无法自拔,没有想换题的打算). 题意:告诉初始矩阵,目的矩阵,告诉n个步骤 ...

  6. hdoj 5386 Cover

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5386 倒着推的一个挺暴力的题,看着和数学有关系,然而并没有, 不要一看到含有数学元素就考虑这是一个数学 ...

  7. HDU 5510 Bazinga 暴力匹配加剪枝

    Bazinga Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5510 ...

  8. HDU 5522 Numbers 暴力

    Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5522 ...

  9. hdu 5077 NAND(暴力打表)

    题目链接:hdu 5077 NAND 题目大意:Xiaoqiang要写一个编码程序,然后依据x1,x2,x3的值构造出8个字符.如今给定要求生成的8个字符.问 说Xiaoqiang最少要写多少行代码. ...

随机推荐

  1. convertquota - 把老的配额文件转换为新的格式

    总览 (SYNOPSIS) convertquota [ -ug ] filesystem 描述 (DESCRIPTION) convertquota 把老的配额文件 quota.user 和 quo ...

  2. bind - 将一个名字和一个套接字绑定到一起

    SYNOPSIS 概述 #include <sys/types.h> #include <sys/socket.h> int bind(int sockfd, struct s ...

  3. SQL Server 兼容级别

    ALTER DATABASE (Transact-SQL) 兼容级别 适用对象:SQL Server(从 2008 版开始)Azure SQL 数据库Azure SQL 数据仓库并行数据仓库 将某些数 ...

  4. TX-LCN事务控制原理

    TX-LCN由两大模块组成, TxClient.TxManager,TxClient作为模块的依赖框架,提供TX-LCN的标准支持,TxManager作为分布式事务的控制方.事务发起方或者参与方都由T ...

  5. Git的入门

    Git的基本介绍: Git:是一个版本控制工具. Github:是非常有名的在线版本管理网站(速度比较慢). Oschina:中国版本的github,(旗下的的码云地址:gitee.com,速度快) ...

  6. Windows——bat中的路径和工具栏运行bat的坑

    工具栏添加的批处理环境 编写一个简单的批处理文件 set testEnv = %cd% pause 这里第一句:设置当前文件夹路径为环境变量testEnv的值 这里第二句:暂停命令窗口 第一次我们直接 ...

  7. 18Spring后置通知

    Spring后置通知,和前置通知类似,直接看代码: package com.cn.spring.aop.impl; //加减乘除的接口类 public interface ArithmeticCalc ...

  8. matplotlib.pyplot.pcolormesh

     matplotlib.pyplot.pcolormesh(*args, alpha=None, norm=None, cmap=None, vmin=None, vmax=None, shading ...

  9. Phong 光照模型(镜面反射)

    Phong 光照模型 镜面反射(高光),是光线经过物体表面,反射到视野中,当反射光线与人的眼睛看得方向平行时,强度最大,高光效果最明显,夹角为90度时,强度最小. specular = I*R*V: ...

  10. hdu3592(差分约束) (线性)

    题意:一些牛按序号排成一条直线,有两种要求,A和B距离不得超过X,还有一种是A和B距离不得少于Y,问1和N可能的最大距离. 和poj那题一样,就是多了多组数据. #include<cstring ...