hdu5386 Cover

Problem 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

这题看了题解后。感觉挺水的。。由于保证有解,所以能够从后面往前推,遇到整行的颜色和当中没有訪问过的一个操作一样的时候。就把这一行的数都变为0(即随意颜色。由于前面的颜色会被后面的覆盖),当矩阵所有为0就输出结果。

这里假设用set存储的话注意操作符的定义,由于假设定义为x或者y间的比較。可能会把一些同样的操作删除掉。导致WA.
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
int gra[106][106],c[600];
struct node{
	int f,x,y,idx;
}b,temp;
bool operator <(node a,node b){
	return a.idx<b.idx;
}

set<node>myset;
set<node>::iterator it;

int main()
{
	int n,m,i,j,T,sum,a,x,y,tot,flag,f,t,flag1,idx,num1;
	char s[10];
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&m);
		sum=n*n;
		for(i=1;i<=n;i++){
			for(j=1;j<=n;j++){
				scanf("%d",&a);
			}
		}
		for(i=1;i<=n;i++){
			for(j=1;j<=n;j++){
				scanf("%d",&gra[i][j]);
			}
		}
		myset.clear();
		for(i=1;i<=m;i++){
			scanf("%s%d%d",s,&x,&y);
            if(s[0]==‘L‘){
            	b.f=1;
            }
            else b.f=0;
            b.x=x;b.y=y;b.idx=i;
            myset.insert(b);
		}
		t=0;
		while(1)
		{
			//if(myset.size()==0)break;// || sum==0
			if(sum==0)break;
			flag=0;
			for(it=myset.begin();it!=myset.end();it++){
				temp=*it;
				x=temp.x;y=temp.y;f=temp.f;idx=temp.idx;
				if(f==1){
					flag1=1;tot=0;
					for(i=1;i<=n;i++){
						if(gra[i][x]==0)continue;
						if(gra[i][x]==y)tot++;
						else{
							flag1=0;break;
						}
					}
					if(tot==0 || flag1==0)continue;

					flag=1;
					for(i=1;i<=n;i++){
						if(gra[i][x]==0)continue;
						else {gra[i][x]=0;sum--;}
					}
					t++;c[t]=idx;
					myset.erase(it);break;
				}

				else if(f==0){
					flag1=1;tot=0;
					for(i=1;i<=n;i++){
						if(gra[x][i]==0)continue;
						if(gra[x][i]==y)tot++;
						else{
							flag1=0;break;
						}
					}
					if(tot==0 || flag1==0)continue;

					flag=1;
					for(i=1;i<=n;i++){
						if(gra[x][i]==0)continue;
						else {gra[x][i]=0;sum--;}
					}
					t++;c[t]=idx;
					myset.erase(it);break;
				}
			}
			if(!flag)break;
		}
		for(i=1;i<=m;i++){
			flag=0;
			for(j=1;j<=t;j++){
				if(i==c[j]){
					flag=1;break;
				}
			}
			if(flag==0){
				printf("%d ",i);
			}
		}
		for(i=t;i>=1;i--){
			if(i==1)printf("%d\n",c[i]);
			else printf("%d ",c[i]);
		}
	}
	return 0;
}
/*
100
3 7
2 2 1
2 3 3
2 1 3
3 2 2
1 1 2
1 1 1
L 2 3
L 1 3
H 2 1
H 3 3
L 4 3
L 3 2
H 3 1
*/

时间: 2024-10-10 00:52:39

hdu5386 Cover的相关文章

hdu5386 Cover(暴力,观察)

题目: Cover Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1449    Accepted Submission(s): 498 Special Judge Problem Description You have an n?n matrix.Every grid has a color.Now there are two t

hdu5386(2015多校8)--Cover

题目链接:点击打开链接 题目大意:给出一个n*n的矩阵的初始值,和最终的值,现在有m个操作 L i j ,将第i列的值重置为j,H i j,将第i行的值重置为j.问m个操作应该怎么执行,可以完成矩阵的变化. 从最终的值向前找寻方案,每次找行和列中剩余的颜色全部相同的,看是否存在没被使用的操作可以完成它,如果有就记录下来,那么最终按照记录的逆序输出,就是可以完成变化的序列啦 #include <cstdio> #include <cstring> #include <stack

HUST 1017 - Exact cover (Dancing Links 模板题)

1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 5584 次提交 2975 次通过 题目描述 There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is a selection of rows such that every column has a 1 in exactly one of the selected rows. Try to find o

搜索(DLX):HOJ 1017 - Exact cover

1017 - Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 6751 Solved: 3519 Description There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is a selection of rows such that every column has a 1 in e

二分图匹配 + 最小点覆盖 - Vertex Cover

Vertex Cover Problem's Link Mean: 给你一个无向图,让你给图中的结点染色,使得:每条边的两个顶点至少有一个顶点被染色.求最少的染色顶点数. analyse: 裸的最小点覆盖问题,二分图的最大匹配,直接套模版即可. Time complexity: O(N^2) view code

Dancing Link --- 模板题 HUST 1017 - Exact cover

1017 - Exact cover Problem's Link:   http://acm.hust.edu.cn/problem/show/1017 Mean: 略 analyse: A Time complexity: O(n) Source code:  #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vecto

background-size: contain 与cover的区别,以及ie78的兼容写法

一:background-size: contain 与cover的区别: 作用: 都是将图片以**相同宽高比**缩放以适应整个容器的宽高. 不同之处在于: 1.  在no-repeat情况下,如果容器宽高比与图片宽高比不同, cover:图片宽高比不变.铺满整个容器的宽高,而图片多出的部分则会被截掉: contain:图片自身的宽高比不变,缩放至图片自身能完全显示出来,所以容器会有留白区域: PS:其实,从英文的意思来说:cover意味着“遮罩.遮盖”---此处理解为“塞满”较恰当,conta

Gym 100431E Word Cover 题解:KMP神用

题意: 给你一个串,问你他的每个前缀的最小重复单元,其中单元是可以重叠的,最后按顺序输出即可.比如样例中abaabaa的最小重复单元为abaa,所以相应输出为4. 样例: input : abaabaababa outpit:1 2 3 4 5 3 4 5 3 10 3 kmp过程就不用多说了,现在我们利用next数组的性质来对问题进行求解. 我们首先用一个ans[maxn]数组来记录最后的答案,且我们的字符串下标从0开始,显然,我们ans[i]的最大值为i+1,我们因此也用此值对其进行初始化.

background-size:cover与-webkit-background-size

在使用jade和styl写手机页面时,且元素为背景图片时,常会使用到background-size cover,它的意思是使背景图片铺满整个元素,且保证背景图片的宽高比. 即当图片小于容器时,又无法使用background-repeat来实现时,我们就可以采用cover;将背景图片放大到适合容器的大小 或者是设定元素的宽高分别为背景图片的宽高,且设置margin-left为元素宽度的一半来使元素居中于手机屏幕.