For example, a n*n matrix in which the data given in the matrix is positive and all is less than an integer m, and now wants to traverse all possible occurrences of the matrix first column Matrix[0][i] (0<=i<=n):
Then the range of matrix[0][0] is 0~m;
The value range of matrix[0][1] is 0~m;
...
The value range of Matrix[0][i] (0<=i<=n) is also 0~m;
There are a total of n^m,
But how do you enumerate these situations? The simplest idea: use N for loop implementation, but there are two disadvantages, first, because do not know the size of n, so it is difficult to control the number of the For loop, the second, n heavy for the loop, the complexity of the larger.
Another idea is to work out all the possible situations (a total of num[n]) and then assign the desired situation through a for loop.
Code:
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int num[8]={0,1,4,16,64,256,1024,4096};//here m=4
int flag[9][9];//matrix size n*n max 9
int main ()
{
int n;
while (scanf ("%d", &n)!=eof)
{
int count=0;
for (int i=0;i<num[n+1];i++)
{
memset (flag,0,sizeof (flag));
flag[1][1]=i%4;
printf ("count=%d flag[1][1]=%d ", ++count,flag[1][1]);
for (int j=2;j<=n;j++)
{
flag[1][j]= (i%num[j+1])/num[j];
printf ("flag[1][%d]=%d ", J,flag[1][j]);
printf ("\ n")
;
}
return 0;
}
A set of output data:
3
Count=1 flag[1][1]=0 flag[1][2]=0 flag[1][3]=0
count=2 flag[1][1]=1 flag[1][2]=0 flag[1][3]=0
Count=3 flag[1][1]=2 flag[1][2]=0 flag[1][3]=0
count=4 flag[1][1]=3 flag[1][2]=0 flag[1][3]=0
Count=5 flag[1][1]=0 flag[1][2]=1 flag[1][3]=0
Count=6 flag[1][1]=1 flag[1][2]=1 flag[1][3]=0
count=7 flag[1][1]=2 flag[1][2]=1 flag[1][3]=0
count=8 flag[1][1]=3 flag[1][2]=1 flag[1][3]=0
Count=9 flag[1][1]=0 flag[1][2]=2 flag[1][3]=0
count=10 flag[1][1]=1 flag[1][2]=2 flag[1][3]=0
count=11 flag[1][1]=2 flag[1][2]=2 flag[1][3]=0
count=12 flag[1][1]=3 flag[1][2]=2 flag[1][3]=0
count=13 flag[1][1]=0 flag[1][2]=3 flag[1][3]=0
count=14 flag[1][1]=1 flag[1][2]=3 flag[1][3]=0
Count=15 flag[1][1]=2 flag[1][2]=3 flag[1][3]=0
Count=16 flag[1][1]=3 flag[1][2]=3 flag[1][3]=0
Count=17 flag[1][1]=0 flag[1][2]=0 flag[1][3]=1
Count=18 flag[1][1]=1 flag[1][2]=0 flag[1][3]=1
count=19 flag[1][1]=2 flag[1][2]=0 flag[1][3]=1
Count=20 flag[1][1]=3 flag[1][2]=0 flag[1][3]=1
count=21 flag[1][1]=0 flag[1][2]=1 flag[1][3]=1
count=22 flag[1][1]=1 flag[1][2]=1 flag[1][3]=1
count=23 flag[1][1]=2 flag[1][2]=1 flag[1][3]=1
count=24 flag[1][1]=3 flag[1][2]=1 flag[1][3]=1
count=25 flag[1][1]=0 flag[1][2]=2 flag[1][3]=1
count=26 flag[1][1]=1 flag[1][2]=2 flag[1][3]=1
count=27 flag[1][1]=2 flag[1][2]=2 flag[1][3]=1
count=28 flag[1][1]=3 flag[1][2]=2 flag[1][3]=1
count=29 flag[1][1]=0 flag[1][2]=3 flag[1][3]=1
count=30 flag[1][1]=1 flag[1][2]=3 flag[1][3]=1
count=31 flag[1][1]=2 flag[1][2]=3 flag[1][3]=1
count=32 flag[1][1]=3 flag[1][2]=3 flag[1][3]=1
count=33 flag[1][1]=0 flag[1][2]=0 flag[1][3]=2
count=34 flag[1][1]=1 flag[1][2]=0 flag[1][3]=2
count=35 flag[1][1]=2 flag[1][2]=0 flag[1][3]=2
count=36 flag[1][1]=3 flag[1][2]=0 flag[1][3]=2
count=37 flag[1][1]=0 flag[1][2]=1 flag[1][3]=2
count=38 flag[1][1]=1 flag[1][2]=1 flag[1][3]=2
count=39 flag[1][1]=2 flag[1][2]=1 flag[1][3]=2
count=40 flag[1][1]=3 flag[1][2]=1 flag[1][3]=2
count=41 flag[1][1]=0 flag[1][2]=2 flag[1][3]=2
count=42 flag[1][1]=1 flag[1][2]=2 flag[1][3]=2
count=43 flag[1][1]=2 flag[1][2]=2 flag[1][3]=2
count=44 flag[1][1]=3 flag[1][2]=2 flag[1][3]=2
count=45 flag[1][1]=0 flag[1][2]=3 flag[1][3]=2
count=46 flag[1][1]=1 flag[1][2]=3 flag[1][3]=2
count=47 flag[1][1]=2 flag[1][2]=3 flag[1][3]=2
count=48 flag[1][1]=3 flag[1][2]=3 flag[1][3]=2
count=49 flag[1][1]=0 flag[1][2]=0 flag[1][3]=3
Count=50 flag[1][1]=1 flag[1][2]=0 flag[1][3]=3
count=51 flag[1][1]=2 flag[1][2]=0 flag[1][3]=3
count=52 flag[1][1]=3 flag[1][2]=0 flag[1][3]=3
count=53 flag[1][1]=0 flag[1][2]=1 flag[1][3]=3
count=54 flag[1][1]=1 flag[1][2]=1 flag[1][3]=3
count=55 flag[1][1]=2 flag[1][2]=1 flag[1][3]=3
count=56 flag[1][1]=3 flag[1][2]=1 flag[1][3]=3
count=57 flag[1][1]=0 flag[1][2]=2 flag[1][3]=3
count=58 flag[1][1]=1 flag[1][2]=2 flag[1][3]=3
count=59 flag[1][1]=2 flag[1][2]=2 flag[1][3]=3
count=60 flag[1][1]=3 flag[1][2]=2 flag[1][3]=3
count=61 flag[1][1]=0 flag[1][2]=3 flag[1][3]=3
count=62 flag[1][1]=1 flag[1][2]=3 flag[1][3]=3
count=63 flag[1][1]=2 flag[1][2]=3 flag[1][3]=3
count=64 flag[1][1]=3 flag[1][2]=3 flag[1][3]=3
The input is 4^3=64, so a total of 64 cases are enumerated.
In this way, it can be very simple to cite the possibility of n^m species.
An example:
An easy Puz
wddpdh find an interesting mini-game in the bbs of whu, called "An easy puz". it ' S a 6 * 6 chess board and each cell has a number in the range of 0 and 3 (it can be 0, 1, 2 or 3). each time you can choose a number a (I,&NBSP;J) in i-th row and j-th column, then the number a (i, j) and the numbers around it (A (i-1, j ), a (I+1, j), A (i, j-1), A (i, j+1), sometimes there may just be 2 or 3 numbers.) will minus 1 (3 to 2, 2 to 1, 1 to 0, 0 &NBSP;TO&NBSP;3) . you can do it finite times. the goal is to make all numbers become 0. wddpdh now come up with An extended problem about it. he will give you a number N (3 <= n <= 6) indicate the size of the Board. you should tell him the minimum steps to reach the goal.
Input
The input consists of multiple test cases. For each test case, it contains a positive integer n (3 <= n <= 6). n lines follow, each line contains N columns indicating the "each number" in the chess board.
Output
For each test case, output minimum steps to reach the goal. If you can ' t reach the goal, output-1 instead.
Sample Input
3
1 1 0
1 0 1
0 1 1
3
2 3 1
2 2 1
0 1 0
Sample Output
2
3
This question is the summer training topic, the difficulty is also good, is worth doing.
Idea: First each lattice is selected 3 times and the check order and the final answer is irrelevant, and the range is small, so will consider the enumeration, but the direct enumeration is 4^36 certainly unacceptable. After considering the state of the first row, the state of the second row can be uniquely determined, so it is 4^6 to enumerate the first row directly. The final complexity is O (4^6*36). Because it is only enumerating the first row, it is the same as enumerating the n^m.
AC Code:
#include <iostream> #include <stdio.h> #include <cstring> #include <algorithm> #include <
Cmath> #define MAX 7 using namespace std;
const int num[8]={0,1,4,16,64,256,1024,4096};
int chess[max+1][max+1];
int flag[max+1][max+1];
int n,minnum;
BOOL last;
int main () {//freopen ("test.in", "R", stdin);
Freopen ("Test.out", "w", stdout);
while (scanf ("%d", &n)!=eof) {memset (chess,0,sizeof (chess));
minnum=1000000;
for (int k=1;k<=n;k++) for (int m=1;m<=n;m++) scanf ("%d", &chess[k][m]);
int count=0;
for (int i=0;i<num[n+1];i++)//enumerates all conditions that can be changed in the first row {memset (flag,0,sizeof (flag));
Last=true;
flag[1][1]=i%4;
printf ("count=%d flag[1][1]=%d", ++count,flag[1][1]);
for (int j=2;j<=n;j++) {flag[1][j]= (i%num[j+1])/num[j];
printf ("flag[1][%d]=%d", J,flag[1][j]); }//printf ("\ n"); for (int k=2;k<=n+1;k++) {to (int m=1;m<=n;m++) {in T temp=chess[k-1][m]-flag[k-1][m-1]-flag[k-1][m]-flag[k-1][m+1]-flag[k-2][m];//this equation is important//printf ("temp=
%d\n ", temp);
while (temp<0) temp+=4;
Flag[k][m]=temp;
} for (int j=1;j<n+1;j++)//Consider the n+1 line and get the nth row to meet the condition if (Flag[n+1][j])
Last=false;
if (last)//satisfies the condition, find out the step number {int result=0;
for (int k=1;k<n+1;k++) for (int m=1;m<n+1;m++) result=result+flag[k][m]; if (Result<minnum) minnum=result;//Gets the minimum number of steps}} if (minnum=
=1000000)//Impossible situation printf (" -1\n");
else printf ("%d\n", minnum);
} return 0;
}