MYF

HDU 5386 Cover

题目链接

HDU 5386

方法:模拟,暴力枚举

题目分析

题目大意

给出初始n*n的方阵和经过m次染色的n*n矩阵,问这m次染色怎么排列才能形成这个结果?

解析

把所有的染色全部push到队列里,然后每次取队首的那次染色,如果该行可以是染完色的结果,就把它标记,否则就把他push到队尾,因为题意中说,保证有解,所以这样一直操作到队列为空时即可。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <iomanip>
#include <cstring>
#include <iostream>
#include <algorithm>
#define Memset(a,val) memset(a,val,sizeof(a))
#define PI acos(-1)
#define rt(n) (i == n ? '\n' : ' ')
#define hi printf("Hi----------\n")
#define IN freopen("input.txt","r",stdin);
#define OUT freopen("output.txt","w",stdout);
#define debug(x) cout<<"Debug : ---"<<x<<"---"<<endl;
#pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long ll;
using namespace std;
const int maxn=100000+5;
const int mod=1000000007;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
int a[105][105],ed[105][105],ans[505];
struct node{
char dir;
int pos,val;
int id;
node(char d,int p,int v,int idd){
dir=d;
pos=p;
val=v;
id=idd;
}
node(){}
};
int main(){
int t,n,m,p,v;
char ch[2];
scanf("%d",&t);
while (t--) {
scanf("%d%d",&n,&m);
for (int i=1; i<=n; i++) {
for (int j=1; j<=n; j++) {
scanf("%d",&a[i][j]);
}
}
for (int i=1; i<=n; i++) {
for (int j=1; j<=n; j++) {
scanf("%d",&ed[i][j]);
}
}
queue<node>q;
for (int i=1; i<=m; i++) {
scanf("%s%d%d",ch,&p,&v);
q.push(node(ch[0],p,v,i));
}
int cnt=0;
while (!q.empty()) {
node tmp = q.front();
q.pop();
bool isLast=true;
if (tmp.dir=='H') {
for (int i=1; i<=n; i++) {
if (ed[tmp.pos][i]==tmp.val||ed[tmp.pos][i]==-1) {
continue;
}
else{
isLast=false;
break;
}
}
if (isLast) {
for (int i=1; i<=n; i++) {
ed[tmp.pos][i]=-1;
}
ans[cnt++]=tmp.id;
}
else
q.push(tmp);
}
else{
for (int i=1; i<=n; i++) {
if (ed[i][tmp.pos]==tmp.val||ed[i][tmp.pos]==-1) {
continue;
}
else{
isLast=false;
break;
}
}
if (isLast) {
for (int i=1; i<=n; i++) {
ed[i][tmp.pos]=-1;
}
ans[cnt++]=tmp.id;
}
else
q.push(tmp);
}
}
for (int i=cnt-1; i>=0; i--) {
printf("%d%c",ans[i],i==0?'\n':' ');
}
}
return 0;
}