1. Title Description: Click to open the link
2. Problem-Solving ideas: The use of reverse thinking + greedy method to solve. Because the problem has been told that there must be solutions, so we can consider the use of greedy method. The beauty of this problem is that the answer is irrelevant to the initial matrix and only to the target matrix. Because no matter what the initial matrix looks like, as long as the operation, coupled with the existence of the solution, the resulting target matrix must be the same. The next step is to find the sequence of operations.
Assuming that the last step is executed, we get the target matrix, because all operations are performed on an entire row or column, so there must be one row or column all the same. In this way, we can start from the target matrix, reverse the process to find, if a row or a column is found to be all the same, then the corresponding operation into the queue, at the same time the line is all zero, this operation also set zero.
Note: The scanning order of the subject is more important. We have a rule to scan from top to bottom, from left to right, so that when some rows or columns are 0, the remaining rows or columns of non-0 elements remain contiguous. Another benefit is that when the entire matrix is set to 0, the remaining queued operations are irrelevant.
3. Code:
#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <iostream> #include <algorithm> #include <cassert> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include <queue> #include <deque> #include <cstdlib> #include < cstdio> #include <cstring> #include <cmath> #include <ctime> #include <cctype> #include < functional>using namespace std; #define ME (s) memset (s,0,sizeof (s)) typedef long long ll;typedef unsigned int uint;type def unsigned long long ull;typedef pair <int, int> p;const int n=105;int a[n][n];int x[5*n],y[5*n],s[5*n][2];int a[5 *n];int N,m;int Main () {int T; 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", &a[i][j]);//Because the target matrix is the key to the subject, only the target matrix for (int i=1;i<=m;i++) scanf ("%s%d%d", S[i],&x[i],&y[i]) is saved; int k=0; while (K<M)//cycle is whether M operations are found {for (int i=1;i<=m;i++) if (X[i])//x[i] equivalent to the identifier, greater than 0 indicates that there is a corresponding operation, equal to 0 Description operation has been used {if (s[i][0]== ' L ')//column operation {int J; for (j=1;j<=n;j++)//from the top down to see if it is continuous equal and is greater than 0 if (a[j][x[i]]&&a[j][x[i]]!=y[i]) break; if (j>n)//if consecutive equal or continuous is equal to 0 {a[k++]=i; for (j=1;j<=n;j++) a[j][x[i]]=0;//the entire column is 0 x[i]=0; Set the action identifier to 0}} else//line operation ibid. {Int J; for (j=1;j<=n;j++) if (a[x[i]][j]&&a[x[i]][j]!=y[i]) break; if (j>n) {a[k++]=i; for (j=1;j<=n;j++) a[x[i]][j]=0; x[i]=0; }}}} for (int i=k-1;i>=0;i--) printf ("%d%c", a[i], "\ n" [i==0]); }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 5386 Cover (2015-year School competition 8th)