hdu5386 Cover

本文介绍了一种矩阵操作转换问题的解决方法,该问题要求通过一系列行或列颜色替换操作,将初始矩阵转换为目标矩阵。文章提供了一个具体的编程实现案例,并详细解释了如何通过逆向思维来简化问题求解过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description
You have an nn 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 m 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 T
For each case:
First line has two integer n,m
Then n lines,every line has n integers,describe the initial matrix
Then n lines,every line has n integers,describe the goal matrix
Then m lines,every line describe an operating

1color[i][j]n
T=5
1n100
1m500
 

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

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
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值