質問編集履歴

2

関数内にsearchを入れて再帰させてみた

2020/05/30 19:00

投稿

Mosato
Mosato

スコア1

test CHANGED
File without changes
test CHANGED
@@ -20,7 +20,7 @@
20
20
 
21
21
  ### 該当のソースコード
22
22
 
23
-
23
+ ```processing
24
24
 
25
25
  void setup(){
26
26
 
@@ -28,65 +28,69 @@
28
28
 
29
29
  void draw(){
30
30
 
31
- print(search((n-1)));
31
+ print(search((n-1)));
32
32
 
33
33
  }
34
34
 
35
35
  int [][] N={
36
36
 
37
- {0,1,1,0,0,0,0,0,0,0},
37
+ {0,1,1,0,0,0,0,0,0,0},
38
38
 
39
- {1,0,0,1,1,0,0,0,0,0},
39
+ {1,0,0,1,1,0,0,0,0,0},
40
40
 
41
- {1,0,0,0,0,1,1,0,0,0},
41
+ {1,0,0,0,0,1,1,0,0,0},
42
42
 
43
- {0,1,0,0,0,0,0,0,0,0},
43
+ {0,1,0,0,0,0,0,0,0,0},
44
44
 
45
- {0,1,0,0,0,0,0,1,1,0},
45
+ {0,1,0,0,0,0,0,1,1,0},
46
46
 
47
- {0,0,1,0,0,0,0,0,0,0},
47
+ {0,0,1,0,0,0,0,0,0,0},
48
48
 
49
- {0,0,1,0,0,0,0,0,0,1},
49
+ {0,0,1,0,0,0,0,0,0,1},
50
50
 
51
- {0,0,0,0,1,0,0,0,0,0},
51
+ {0,0,0,0,1,0,0,0,0,0},
52
52
 
53
- {0,0,0,0,1,0,0,0,0,0},
53
+ {0,0,0,0,1,0,0,0,0,0},
54
54
 
55
- {0,0,0,0,0,0,1,0,0,0}};
55
+ {0,0,0,0,0,0,1,0,0,0}};
56
56
 
57
57
  int n=10;
58
58
 
59
- int a,b;
59
+ int []a={0,0,0,0,0,0,0,0,0,0};
60
60
 
61
61
  int search(int i){
62
62
 
63
- int j;
63
+ int j;
64
64
 
65
- for(j=0;j<n;j++){
65
+ a[i]=1;
66
66
 
67
- if(N[i][j]==1&&i<j){
67
+ for(j=0;j<n;j++){
68
68
 
69
- a=i;
69
+ if(N[i][j]==1&&a[j]==0){
70
70
 
71
- b=j;
71
+ return search(j);
72
72
 
73
- i=j;
73
+ }else if(N[i][j]==1&&a[i]==0&&a[j]==1){
74
74
 
75
- }else if(N[i][j]==1&&i>j){
75
+ a[i]=2;
76
76
 
77
- i=a;
77
+ return search(j);
78
78
 
79
+ }
80
+
81
+ }
82
+
79
- j=b;
83
+ if(i==0){
84
+
85
+ noLoop();
86
+
87
+ }
88
+
89
+ return i;
80
90
 
81
91
  }
82
92
 
83
- }
93
+ ```
84
-
85
- return i;
86
-
87
- }
88
-
89
-
90
94
 
91
95
  ### 試したこと
92
96
 

1

2020/05/30 19:00

投稿

Mosato
Mosato

スコア1

test CHANGED
File without changes
test CHANGED
@@ -22,11 +22,69 @@
22
22
 
23
23
 
24
24
 
25
- ```
25
+ void setup(){
26
26
 
27
+ }
27
28
 
29
+ void draw(){
28
30
 
31
+ print(search((n-1)));
32
+
33
+ }
34
+
35
+ int [][] N={
36
+
37
+ {0,1,1,0,0,0,0,0,0,0},
38
+
39
+ {1,0,0,1,1,0,0,0,0,0},
40
+
41
+ {1,0,0,0,0,1,1,0,0,0},
42
+
43
+ {0,1,0,0,0,0,0,0,0,0},
44
+
45
+ {0,1,0,0,0,0,0,1,1,0},
46
+
47
+ {0,0,1,0,0,0,0,0,0,0},
48
+
49
+ {0,0,1,0,0,0,0,0,0,1},
50
+
51
+ {0,0,0,0,1,0,0,0,0,0},
52
+
53
+ {0,0,0,0,1,0,0,0,0,0},
54
+
55
+ {0,0,0,0,0,0,1,0,0,0}};
56
+
57
+ int n=10;
58
+
59
+ int a,b;
60
+
61
+ int search(int i){
62
+
63
+ int j;
64
+
65
+ for(j=0;j<n;j++){
66
+
67
+ if(N[i][j]==1&&i<j){
68
+
29
- ```
69
+ a=i;
70
+
71
+ b=j;
72
+
73
+ i=j;
74
+
75
+ }else if(N[i][j]==1&&i>j){
76
+
77
+ i=a;
78
+
79
+ j=b;
80
+
81
+ }
82
+
83
+ }
84
+
85
+ return i;
86
+
87
+ }
30
88
 
31
89
 
32
90