7 задание:
Code
#include <stdio.h>
#include <stdlib.h>
#define ERR -1
#define MAX_S 100
int SIZE;
int** matr;
int* max_str;
int* cur_str;
void print_matr(){
int i,j;
for( i = 0; i < SIZE; i++ ){
for( j = 0; j < SIZE; j++ ){
printf("%d ",matr[i][j]);
}
printf("\n");
}
}
void print_max_str(){
int i;
for( i = 1; i <= max_str[0]; i++){
printf("%d ",max_str[i]);
}
}
void initialize(FILE* fp){
int i,j;
matr = (int**)malloc( SIZE*sizeof(int) );
max_str = (int*)malloc( MAX_S*sizeof(int) );
cur_str = (int*)malloc( MAX_S*sizeof(int) );
max_str[0] = 0;
cur_str[0] = 0;
for( i = 0; i < SIZE; i++ ){
matr[i] = (int*)malloc( SIZE*sizeof(int) );
for( j = 0; j < SIZE; j++ ){
fscanf( fp,"%d",(matr[i]+j) );
}
}
}
int read_data(char* name){
FILE* fp;
if( ( fp = fopen(name,"r+") ) < 0 ){
return ERR;
}
fscanf(fp,"%d",&SIZE);
printf("SIZE = %d\n",SIZE);
initialize(fp);
print_matr();
return 1;
}
void cpy_new_max(){
int i;
for( i = 0; i <= cur_str[0]; i++){
max_str[i] = cur_str[i];
}
}
void find(){
int j,m;
m = cur_str[cur_str[0]];
for( j = 0; j < SIZE; j++ ){
if( matr[m][j] == 1){
cur_str[0] += 1;
cur_str[cur_str[0]] = j;
matr[m][j] = -1;
find();
matr[m][j] = 1;
cur_str[0] -= 1;
}
}
if( cur_str[0] > max_str[0] ){
cpy_new_max();
}
}
void find_steams(){
int i;
for( i = 0; i < SIZE; i++ ){
cur_str[0] = 1;
cur_str[1] = i;
find();
}
}
int main(int arg,char* argv[]){
if(arg<2 || read_data(*++argv) == ERR){
printf("Err read file\n");
exit(0);
}
find_steams();
printf("\nAnswer: ");
print_max_str();
return 0;
}
##############################
test_file:
8
0 0 0 0 1 1 0 1
1 0 0 1 0 0 0 0
0 0 0 1 0 1 0 1
1 0 0 0 1 0 0 0
0 0 1 0 0 0 1 0
1 0 1 1 0 0 0 0
0 1 0 1 0 1 0 1
0 1 0 1 0 0 1 0
##############################
tests:
1)------------------------------
4
0 0 0 1
1 0 1 0
0 0 0 0
0 0 1 0
ANS:
1 0 3 2
2)------------------------------
8
0 0 0 0 1 1 0 1
1 0 0 1 0 0 0 0
0 0 0 1 0 1 0 1
1 0 0 0 1 0 0 0
0 0 1 0 0 0 1 0
1 0 1 1 0 0 0 0
0 1 0 1 0 1 0 1
0 1 0 1 0 0 1 0
ANS:
6 1 0 4 2 5 0 5 2 7 1 3 0 7 3 4 6 7 6 5 3
3)------------------------------
5
0 0 0 0 1
1 0 0 1 1
0 0 0 1 0
0 0 0 0 1
0 1 1 0 0
ANS:
1 0 4 1 3 4 2 3
4)------------------------------
3
0 0 1
1 0 1
0 1 0
ANS:
1 0 2 1 2
5)------------------------------
9
0 0 0 0 1 1 0 1 0
1 0 0 1 0 0 0 0 1
0 0 0 1 0 1 0 1 0
1 0 0 0 1 0 0 0 1
0 0 1 0 0 0 1 0 1
1 0 1 1 0 0 0 0 0
0 1 0 1 0 1 0 1 1
0 1 0 1 0 0 1 0 0
1 1 0 0 0 0 1 0 0
ANS:
4 2 5 0 4 6 1 0 5 2 7 1 3 0 7 3 4 8 1 8 6 7 6 5 3 8 0
6)------------------------------
10
0 0 1 0 1 0 0 1 0 0
0 0 0 0 1 0 0 0 1 0
0 1 0 1 0 1 0 0 0 1
1 0 1 0 1 0 0 0 1 0
0 1 0 1 0 0 0 1 0 0
1 0 0 0 0 0 1 0 1 1
1 0 1 0 0 0 0 1 0 0
0 0 1 0 1 0 1 0 0 0
1 0 0 1 0 0 0 1 0 1
0 1 0 0 0 1 0 0 1 0
ANS:
5 6 0 2 1 4 1 8 0 4 3 0 7 2 3 2 5 8 3 8 7 4 7 6 2 9 5 9 8 9 1