'Algorithmus' 카테고리의 다른 글

'Algorithmus'에 해당되는 글 11건

  1. 2021.10.25 C# 10430 분배법칙
  2. 2020.08.11 20200811 임시, 추가 변수없이 두 정수값 교체
  3. 2019.06.12 [설탕 배달]06-12
  4. 2019.05.21 AStar algorithm 4방향(캐릭터 이동)
  5. 2019.05.20 AStar algorithm
  6. 2019.05.14 [Palindrome Number]05-14
  7. 2019.05.04 [TwoSum]05-04
  8. 2019.05.03 [TwoSum]05-03

C# 10430 분배법칙

Algorithmus 2021. 10. 25. 10:19

https://www.acmicpc.net/problem/10430

 

using System;
using System.Threading;
using System.Collections.Generic;

namespace NetHappy
{
    class Program
    {
        static void Main(string[] args)
        {
            string str;
            str = Console.ReadLine();
            string[] result = str.Split(new char[] { ' ' });
            double a = (double)Convert.ToInt32(result[0]);
            double b = (double)Convert.ToInt32(result[1]);
            double c = (double)Convert.ToInt32(result[2]);

            Console.WriteLine((a + b) % c);
            Console.WriteLine(((a % c) + (b % c)) % c);
            Console.WriteLine((a * b) % c);
            Console.WriteLine(((a % c) * (b % c)) % c);
        }
    }
}

'Algorithmus' 카테고리의 다른 글

20200811 임시, 추가 변수없이 두 정수값 교체  (0) 2020.08.11
[설탕 배달]06-12  (0) 2019.06.12
AStar algorithm 4방향(캐릭터 이동)  (0) 2019.05.21
AStar algorithm  (0) 2019.05.20
[Palindrome Number]05-14  (0) 2019.05.14
:

20200811 임시, 추가 변수없이 두 정수값 교체

Algorithmus 2020. 8. 11. 18:18

http://www.csharpstudy.com/algo/qa.aspx?Id=17&pg=0

두개의 정수 a,b의 값을 서로 바꾸는 C# 코드를 쓰시오. (단, a,b이외의 임시 변수 사용 불가)

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Algorithm20200811
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 17;
            int b = 11;
 
            a ^= b;
            b ^= a;
            a ^= b;
 
            Console.WriteLine("{0}, {1}", a, b);
            Console.ReadKey();
        }
    }
}
 
cs



1.a^=b

a = a^b, b=b

2.b^=a
a = a^b, b = b^(a^b) = a^(b^b) = a^0 = a

3.a^=b
a = (a^b)^b = a^(b^b) = a^0 = a, b=a

4.WriteLine()
a = 11, b = 17

XOR교체

*그냥 하나 더 쓰자..

'Algorithmus' 카테고리의 다른 글

C# 10430 분배법칙  (0) 2021.10.25
[설탕 배달]06-12  (0) 2019.06.12
AStar algorithm 4방향(캐릭터 이동)  (0) 2019.05.21
AStar algorithm  (0) 2019.05.20
[Palindrome Number]05-14  (0) 2019.05.14
:

[설탕 배달]06-12

Algorithmus 2019. 6. 12. 15:53


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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
 
namespace TestSugarDelivery
{
    public class App
    {
        public App()
        {
 
        }
 
        public void Start()
        {
            int a = 2;
            this.SugerDelivery(a);
            a = 3;
            this.SugerDelivery(a);
            a = 5;
            this.SugerDelivery(a);
            a = 7;
            this.SugerDelivery(a);
            a = 8;
            this.SugerDelivery(a);
            a = 11;
            this.SugerDelivery(a);
            a = 13;
            this.SugerDelivery(a);
            a = 15;
            this.SugerDelivery(a);
            a = 18;
            this.SugerDelivery(a);
        }
 
        public void SugerDelivery(int a)
        {
            Console.WriteLine("배달할 무게 : {0}", a);
            int suger = a;
            int kg5 = 0;
            int kg3 = 0;
 
            if (suger % 5 == 0)
            {
                kg5 = suger / 5;
                Console.WriteLine("설탕봉지 : {0}개", kg5 + kg3);
 
            }
            else
            {
                while (true)
                {
                    if (suger - 5 > 0)
                    {
                        suger -= 5;
                        kg5++;
                    }
                    if (suger % 3 == 0)
                    {
                        kg3 = suger / 3;
                        Console.WriteLine("설탕봉지 : {0}개", kg5 + kg3);
                        break;
                    }
                    if (suger - 5 < 0)
                    {
                        Console.WriteLine("-1");
                        break;
                    }
                }
            }
            Console.WriteLine();
        }
    }
}
cs


'Algorithmus' 카테고리의 다른 글

C# 10430 분배법칙  (0) 2021.10.25
20200811 임시, 추가 변수없이 두 정수값 교체  (0) 2020.08.11
AStar algorithm 4방향(캐릭터 이동)  (0) 2019.05.21
AStar algorithm  (0) 2019.05.20
[Palindrome Number]05-14  (0) 2019.05.14
:

AStar algorithm 4방향(캐릭터 이동)

Algorithmus 2019. 5. 21. 18:54


애니메이션이 이상해


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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class Test0517 : MonoBehaviour
{
    public int row, col;
    public int startX, startY;
    public int targetX, targetY;
    public Button btnNext;
    public GameObject character;
 
    private GameObject[] tilePrefabs;
    private GameObject[,] tiles;
    private GameObject motherNode;
    private GameObject startNode;
    private GameObject targetNode;
    private List<GameObject> listTile;
    private List<GameObject> openList;
    private List<GameObject> closeList;
    private List<GameObject> motherNodes;
 
    private Vector2[,] coordinate;
 
    void Start()
    {
        this.tilePrefabs = Resources.LoadAll<GameObject>("Tiles");
        this.coordinate = new Vector2[this.row, this.col];
        this.tiles = new GameObject[this.row, this.col];
        this.listTile = new List<GameObject>();
        this.openList = new List<GameObject>();
        this.closeList = new List<GameObject>();
 
        this.SetCorrdinate();
        this.CreateTiles();
        this.character.transform.position = this.startNode.transform.position;
 
        this.SetMotherNode(this.tiles[startX, startY]);
        //this.FindPath(this.tiles[targetX, targetY]);
        this.FindPath_4(this.tiles[targetX, targetY]);
        this.closeList.Add(this.motherNode);
        this.btnNext.onClick.AddListener(() =>
        {
 
            //현재 박스 저장한 뒤
            this.OpenTileSort(this.openList);
            //Debug.LogFormat("{0}  F : {1}", this.openList[0].GetComponent<TestTile>().textCoord.text, this.openList[0].GetComponent<TestTile>().f);
 
            this.SetMotherNode(this.openList[0]);
            this.ResetNodeVal(this.openList);
            //this.openList.Clear();
            this.openList = new List<GameObject>();
            //this.FindPath(this.tiles[targetX, targetY]);
            this.FindPath_4(this.tiles[targetX, targetY]);
            this.closeList.Add(this.motherNode);
 
            if (this.motherNode.GetComponent<TestTile>().x == targetX && this.motherNode.GetComponent<TestTile>().y == targetY)
            {
                Debug.Log("찾음!");
 
                this.btnNext.gameObject.SetActive(false);
                StartCoroutine(this.MoveCharacter());
 
            }
        });
 
    }
 
    private IEnumerator MoveCharacter()
    {
        //Debug.LogFormat("움직여라 닝겐 : {0}", this.closeList.Count);
 
        for (int i = 0; i < this.closeList.Count; i++)
        {
            var dis = Vector2.Distance(this.closeList[i].transform.position, this.character.transform.position);
            var dir = (this.closeList[i].transform.position - this.character.transform.position);
            var dirX = Mathf.Round(dir.x);
            var dirY = Mathf.Round(dir.y);
            Debug.LogFormat("x : {0},y : {1}", dirX, dirY);
 
            if (dirX > 0)
            {
                Debug.Log("오른");
                this.character.GetComponent<Animator>().Play("Right");
            }
            else if (dirX < 0)
            {
                Debug.Log("왼");
                this.character.GetComponent<Animator>().Play("Left");
            }
            else if (dirY > 0)
            {
                Debug.Log("위");
                this.character.GetComponent<Animator>().Play("Up");
            }
            else if (dirY < 0)
            {
                Debug.Log("아래");
                this.character.GetComponent<Animator>().Play("Down");
            }
 
            while (true)
            {
                if (dis <= 0)
                {
                    break;
                }
                this.character.transform.position += dir * 1f * Time.deltaTime;
                dis -= 1f * Time.deltaTime;
                yield return null;
            }
        }
        Debug.Log("도착!");
        this.character.GetComponent<Animator>().Play("idle");
    }
 
    private void ResetNodeVal(List<GameObject> openList)
    {
        for (int i = 0; i < openList.Count; i++)
        {
            openList[i].GetComponent<TestTile>().textF.text = "F";
            openList[i].GetComponent<TestTile>().textG.text = "G";
            openList[i].GetComponent<TestTile>().textH.text = "H";
            openList[i].GetComponent<TestTile>().Arrow.gameObject.SetActive(false);
        }
    }
 
    private void OpenTileSort(List<GameObject> openList)
    {
        //Debug.Log("솔트시작");
        int i = 0;
        int j = 0;
        int min = 0;
 
        for (i = 0; i < openList.Count - 1; i++)//전체 훑기 Count-1 : 마지막칸은 자동으로 정렬되고 뒷칸이 없으므로 제외
        {
            min = i;//최소값을 들고 있을 제 3의 손//맨 처음엔 검사하는 첫배열을 들고있음
 
            for (j = i + 1; j < openList.Count; j++)// 최소값을 탐색한다.
            {
                if (openList[j].GetComponent<TestTile>().f < openList[min].GetComponent<TestTile>().f)//초기값 or 최소값이 j보다 크면 j를 최소값에 넣는다.
                {
                    min = j;
                }
            }
            if (min != 0)//스왑 알고리즘
            {
                SwapList(i, min);
            }
        }
    }
 
    private void SwapList(int index1, int index2)
    {
        //값을 임시로 저장할 temp변수
        var temp = openList[index2];
        openList[index2] = openList[index1];
        openList[index1] = temp;
    }
 
    private void FindPath(GameObject target)
    {
        this.FindNodes();
    }
 
    private void FindNodes()
    {
        var mother = this.motherNode.GetComponent<TestTile>();
        var motherX = this.motherNode.GetComponent<TestTile>().x - 1;
        var motherY = this.motherNode.GetComponent<TestTile>().y - 1;
        var maxX = this.motherNode.GetComponent<TestTile>().x + 1;
        var maxY = this.motherNode.GetComponent<TestTile>().y + 1;
 
        if (motherX < 0)
        {
            motherX = 0;
        }
        if (motherY < 0)
        {
            motherY = 0;
        }
 
        for (int i = motherX; i <= maxX; i++)
        {
            for (int j = motherY; j <= maxY; j++)
            {
                if (this.tiles[i, j].GetComponent<TestTile>().tileType == 0)
                {
                    this.listTile.Add(this.tiles[i, j]);
                    this.openList.Add(this.tiles[i, j]);
                    this.SetH(this.tiles[i, j]);
                    this.SetG(this.tiles[i, j]);
                    this.SetF(this.tiles[i, j]);
                    this.TurnArrow(this.tiles[i, j]);
                    var sprite = this.tiles[i, j].GetComponentInChildren<SpriteRenderer>();
                    sprite.color = Color.cyan;
                }
                else
                {
                    this.tiles[i, j].GetComponent<TestTile>().closeNode = true;
                }
            }
        }
    }
 
    private void FindPath_4(GameObject target)
    {
        this.FindNodes_4();
    }
 
    private void FindNodes_4()
    {
        var mother = this.motherNode.GetComponent<TestTile>();
        var motherX = this.motherNode.GetComponent<TestTile>().x - 1;
        var motherY = this.motherNode.GetComponent<TestTile>().y - 1;
        var maxX = this.motherNode.GetComponent<TestTile>().x + 1;
        var maxY = this.motherNode.GetComponent<TestTile>().y + 1;
 
        if (motherX < 0)
        {
            motherX = 0;
        }
        if (motherY < 0)
        {
            motherY = 0;
        }
 
        if (this.tiles[motherX, mother.y].GetComponent<TestTile>().tileType == 0)
        {
            this.listTile.Add(this.tiles[motherX, mother.y]);
            this.openList.Add(this.tiles[motherX, mother.y]);
            this.SetH(this.tiles[motherX, mother.y]);
            this.SetG(this.tiles[motherX, mother.y]);
            this.SetF(this.tiles[motherX, mother.y]);
            this.TurnArrow(this.tiles[motherX, mother.y]);
            var sprite = this.tiles[motherX, mother.y].GetComponentInChildren<SpriteRenderer>();
            sprite.color = Color.cyan;
 
        }
        if (this.tiles[maxX, mother.y].GetComponent<TestTile>().tileType == 0)
        {
            this.listTile.Add(this.tiles[maxX, mother.y]);
            this.openList.Add(this.tiles[maxX, mother.y]);
            this.SetH(this.tiles[maxX, mother.y]);
            this.SetG(this.tiles[maxX, mother.y]);
            this.SetF(this.tiles[maxX, mother.y]);
            this.TurnArrow(this.tiles[maxX, mother.y]);
            var sprite = this.tiles[maxX, mother.y].GetComponentInChildren<SpriteRenderer>();
            sprite.color = Color.cyan;
 
        }
        if (this.tiles[mother.x, motherY].GetComponent<TestTile>().tileType == 0)
        {
            this.listTile.Add(this.tiles[mother.x, motherY]);
            this.openList.Add(this.tiles[mother.x, motherY]);
            this.SetH(this.tiles[mother.x, motherY]);
            this.SetG(this.tiles[mother.x, motherY]);
            this.SetF(this.tiles[mother.x, motherY]);
            this.TurnArrow(this.tiles[mother.x, motherY]);
            var sprite = this.tiles[mother.x, motherY].GetComponentInChildren<SpriteRenderer>();
            sprite.color = Color.cyan;
 
        }
        if (this.tiles[mother.x, maxY].GetComponent<TestTile>().tileType == 0)
        {
            this.listTile.Add(this.tiles[mother.x, maxY]);
            this.openList.Add(this.tiles[mother.x, maxY]);
            this.SetH(this.tiles[mother.x, maxY]);
            this.SetG(this.tiles[mother.x, maxY]);
            this.SetF(this.tiles[mother.x, maxY]);
            this.TurnArrow(this.tiles[mother.x, maxY]);
            var sprite = this.tiles[mother.x, maxY].GetComponentInChildren<SpriteRenderer>();
            sprite.color = Color.cyan;
 
        }
    }
 
    private void TurnArrow(GameObject tile)
    {
        var script = tile.GetComponent<TestTile>();
        var xPos = this.motherNode.transform.position.x - script.Arrow.transform.position.x;
        var yPos = this.motherNode.transform.position.y - script.Arrow.transform.position.y;
        var rad = Mathf.Atan2(yPos, xPos) * Mathf.Rad2Deg;
        script.Arrow.transform.rotation = Quaternion.Euler(new Vector3(00, rad + 270));
 
        script.Arrow.SetActive(true);
    }
 
    private void SetMotherNode(GameObject mother)
    {
        this.motherNode = mother;
        this.motherNode.GetComponent<TestTile>().tileType = 2;
        this.motherNode.GetComponentInChildren<SpriteRenderer>().color = Color.gray;
    }
 
    private void SetCorrdinate()
    {
        for (int i = 0; i < this.row; i++)
        {
            for (int j = 0; j < this.col; j++)
            {
                this.coordinate[i, j] = new Vector2(i, j);
            }
        }
    }
 
    private void SetF(GameObject tile)
    {
        var tileScript = tile.GetComponent<TestTile>();
        tileScript.f = tileScript.g + tileScript.h;
        tileScript.textF.text = tileScript.f.ToString();
    }
    private void SetH(GameObject tile)
    {
        var tileScript = tile.GetComponent<TestTile>();
        var targetScript = this.targetNode.GetComponent<TestTile>();
        var dis = (int)(Vector2.Distance(tile.transform.position, this.targetNode.transform.position) * 10);
        //Debug.Log(dis);
        tileScript.textH.text = dis.ToString();
        tileScript.h = dis;
    }
    private void SetG(GameObject tile)
    {
        var tileScript = tile.GetComponent<TestTile>();
        var targetScript = this.targetNode.GetComponent<TestTile>();
        var dis = (int)Mathf.Round(((Vector2.Distance(tile.transform.position, this.motherNode.transform.position)) * 10));
        //Debug.LogFormat("G{0} : {1}", tile.transform.position, dis);
        tileScript.textG.text = dis.ToString();
        tileScript.g = dis;
    }
 
 
    private void CreateTiles()
    {
 
        for (int i = 0; i < this.row; i++)
        {
            for (int j = 0; j < this.col; j++)
            {
                var screenPos = Camera.main.ScreenToWorldPoint(new Vector2((j * 100+ 400, (i * -100+ 550));
                screenPos.z = 0;
                if (i == this.startX && j == this.startY)
                {
                    var tile = GameObject.Instantiate(this.tilePrefabs[1]);
                    this.tiles[i, j] = tile;
                    this.startNode = tile;
                    var script = tile.GetComponent<TestTile>();
                    script.x = i;
                    script.y = j;
                    script.textCoord.text = script.x.ToString() + ", " + script.y.ToString();
                    tile.transform.position = screenPos;
                }
                else if (i == this.targetX && j == this.targetY)
                {
                    var tile = GameObject.Instantiate(this.tilePrefabs[2]);
                    this.tiles[i, j] = tile;
                    this.targetNode = tile;
                    var script = tile.GetComponent<TestTile>();
                    script.x = i;
                    script.y = j;
                    script.textCoord.text = script.x.ToString() + ", " + script.y.ToString();
                    tile.transform.position = screenPos;
                }
                else if ((i >= 1 && i < 4&& j == 3)
                {
                    var tile = GameObject.Instantiate(this.tilePrefabs[0]);
                    this.tiles[i, j] = tile;
                    var script = tile.GetComponent<TestTile>();
                    script.tileType = 1;
                    script.x = i;
                    script.y = j;
                    script.textCoord.text = script.x.ToString() + ", " + script.y.ToString();
                    tile.transform.position = screenPos;
                }
                else
                {
                    var tile = GameObject.Instantiate(this.tilePrefabs[3]);
                    this.tiles[i, j] = tile;
                    var script = tile.GetComponent<TestTile>();
                    script.x = i;
                    script.y = j;
                    script.textCoord.text = script.x.ToString() + ", " + script.y.ToString();
                    tile.transform.position = screenPos;
                }
            }
        }
    }
}
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class TestTile : MonoBehaviour
{
    public TextMesh textCoord;
    public TextMesh textF;
    public TextMesh textG;
    public TextMesh textH;
    public GameObject Arrow;
    public int x, y;
    public int tileType;
    public Vector2 vec2;
    public int g, h, f;
    public bool closeNode;
 
    public TestTile()
    {
        this.vec2 = new Vector2(this.x, this.y);
    }
}
 
cs


'Algorithmus' 카테고리의 다른 글

20200811 임시, 추가 변수없이 두 정수값 교체  (0) 2020.08.11
[설탕 배달]06-12  (0) 2019.06.12
AStar algorithm  (0) 2019.05.20
[Palindrome Number]05-14  (0) 2019.05.14
[TwoSum]05-04  (0) 2019.05.04
:

AStar algorithm

Algorithmus 2019. 5. 20. 18:15



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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class Test0517 : MonoBehaviour
{
    public int row, col;
    public int startX, startY;
    public int targetX, targetY;
    public Button btnNext;
 
    private GameObject[] tilePrefabs;
    private GameObject[,] tiles;
    private GameObject motherNode;
    private GameObject startNode;
    private GameObject targetNode;
    private List<GameObject> listTile;
    private List<GameObject> openList;
    private List<GameObject> closeList;
    private List<GameObject> motherNodes;
 
    private Vector2[,] coordinate;
 
    void Start()
    {
        this.tilePrefabs = Resources.LoadAll<GameObject>("Tiles");
        this.coordinate = new Vector2[this.row, this.col];
        this.tiles = new GameObject[this.row, this.col];
        this.listTile = new List<GameObject>();
        this.openList = new List<GameObject>();
 
        this.SetCorrdinate();
        this.CreateTiles();
 
 
        this.SetMotherNode(this.tiles[startX, startY]);
        this.FindPath(this.tiles[targetX, targetY]);
 
        this.btnNext.onClick.AddListener(() =>
        {
 
            //현재 박스 저장한 뒤
            this.OpenTileSort(this.openList);
            Debug.LogFormat("{0}  F : {1}"this.openList[0].GetComponent<TestTile>().textCoord.text, this.openList[0].GetComponent<TestTile>().f);
 
            this.SetMotherNode(this.openList[0]);
            this.ResetNodeVal(this.openList);
            this.openList.Clear();
            this.openList = new List<GameObject>();
            this.FindPath(this.tiles[targetX, targetY]);
 
            if (this.motherNode.GetComponent<TestTile>().x == targetX && this.motherNode.GetComponent<TestTile>().y == targetY)
            {
                Debug.Log("도착!");
                this.btnNext.gameObject.SetActive(false);
            }
        });
 
    }
 
    private void ResetNodeVal(List<GameObject> openList)
    {
        for (int i = 0; i < openList.Count; i++)
        {
            openList[i].GetComponent<TestTile>().textF.text = "F";
            openList[i].GetComponent<TestTile>().textG.text = "G";
            openList[i].GetComponent<TestTile>().textH.text = "H";
            openList[i].GetComponent<TestTile>().Arrow.gameObject.SetActive(false);
        }
    }
 
    private void OpenTileSort(List<GameObject> openList)
    {
        Debug.Log("솔트시작");
        int i = 0;
        int j = 0;
        int min = 0;
 
        Debug.Log("솔트전");
        foreach (var data in openList)
        {
            Debug.LogFormat("{0}  F : {1}", data.GetComponent<TestTile>().textCoord.text, data.GetComponent<TestTile>().f);
        }
 
        for (i = 0; i < openList.Count - 1; i++)//전체 훑기 Count-1 : 마지막칸은 자동으로 정렬되고 뒷칸이 없으므로 제외
        {
            min = i;//최소값을 들고 있을 제 3의 손//맨 처음엔 검사하는 첫배열을 들고있음
 
            for (j = i + 1; j < openList.Count; j++)// 최소값을 탐색한다.
            {
                if (openList[j].GetComponent<TestTile>().f < openList[min].GetComponent<TestTile>().f)//초기값 or 최소값이 j보다 크면 j를 최소값에 넣는다.
                {
                    min = j;
                }
            }
            if (min != 0)//스왑 알고리즘
            {
                SwapList(i, min);
            }
        }
 
        Debug.Log("솔트 후");
        foreach (var data in openList)
        {
            Debug.LogFormat("{0}  F : {1}", data.GetComponent<TestTile>().textCoord.text, data.GetComponent<TestTile>().f);
        }
    }
 
    private void SwapList(int index1, int index2)
    {
        //값을 임시로 저장할 temp변수
        var temp = openList[index2];
        openList[index2] = openList[index1];
        openList[index1] = temp;
    }
 
    private void FindPath(GameObject target)
    {
        this.FindNodes();
    }
 
    private void FindNodes()
    {
        var mother = this.motherNode.GetComponent<TestTile>();
        var motherX = this.motherNode.GetComponent<TestTile>().x - 1;
        var motherY = this.motherNode.GetComponent<TestTile>().y - 1;
        var maxX = this.motherNode.GetComponent<TestTile>().x + 1;
        var maxY = this.motherNode.GetComponent<TestTile>().y + 1;
 
        if (motherX < 0)
        {
            motherX = 0;
        }
        if (motherY < 0)
        {
            motherY = 0;
        }
 
        for (int i = motherX; i <= maxX; i++)
        {
            for (int j = motherY; j <= maxY; j++)
            {
                if (this.tiles[i, j].GetComponent<TestTile>().tileType == 0)
                {
                    this.listTile.Add(this.tiles[i, j]);
                    this.openList.Add(this.tiles[i, j]);
                    this.SetH(this.tiles[i, j]);
                    this.SetG(this.tiles[i, j]);
                    this.SetF(this.tiles[i, j]);
                    this.TurnArrow(this.tiles[i, j]);
                    var sprite = this.tiles[i, j].GetComponentInChildren<SpriteRenderer>();
                    sprite.color = Color.cyan;
                }
                else
                {
                    this.tiles[i, j].GetComponent<TestTile>().closeNode = true;
                }
            }
        }
    }
 
    private void TurnArrow(GameObject tile)
    {
        var script = tile.GetComponent<TestTile>();
        var xPos = this.motherNode.transform.position.x - script.Arrow.transform.position.x;
        var yPos = this.motherNode.transform.position.y - script.Arrow.transform.position.y;
        var rad = Mathf.Atan2(yPos, xPos) * Mathf.Rad2Deg;
        script.Arrow.transform.rotation = Quaternion.Euler(new Vector3(00, rad + 270));
 
        script.Arrow.SetActive(true);
    }
 
    private void SetMotherNode(GameObject mother)
    {
        this.motherNode = mother;
        this.motherNode.GetComponent<TestTile>().tileType = 2;
        this.motherNode.GetComponentInChildren<SpriteRenderer>().color = Color.gray;
    }
 
    private void SetCorrdinate()
    {
        for (int i = 0; i < this.row; i++)
        {
            for (int j = 0; j < this.col; j++)
            {
                this.coordinate[i, j] = new Vector2(i, j);
            }
        }
    }
 
    private void SetF(GameObject tile)
    {
        var tileScript = tile.GetComponent<TestTile>();
        tileScript.f = tileScript.g + tileScript.h;
        tileScript.textF.text = tileScript.f.ToString();
    }
    private void SetH(GameObject tile)
    {
        var tileScript = tile.GetComponent<TestTile>();
        var targetScript = this.targetNode.GetComponent<TestTile>();
        var dis = (int)(Vector2.Distance(tile.transform.position, this.targetNode.transform.position) * 10);
        //Debug.Log(dis);
        tileScript.textH.text = dis.ToString();
        tileScript.h = dis;
    }
    private void SetG(GameObject tile)
    {
        var tileScript = tile.GetComponent<TestTile>();
        var targetScript = this.targetNode.GetComponent<TestTile>();
        var dis = (int)((Vector2.Distance(tile.transform.position, this.motherNode.transform.position)) * 10);
        //Debug.LogFormat("{0} : {1}", tile.transform.position, dis);
        tileScript.textG.text = dis.ToString();
        tileScript.g = dis;
    }
 
 
    private void CreateTiles()
    {
 
        for (int i = 0; i < this.row; i++)
        {
            for (int j = 0; j < this.col; j++)
            {
                var screenPos = Camera.main.ScreenToWorldPoint(new Vector2((j * 100+ 400, (i * -100+ 550));
                screenPos.z = 0;
                if (i == this.startX && j == this.startY)
                {
                    var tile = GameObject.Instantiate(this.tilePrefabs[1]);
                    this.tiles[i, j] = tile;
                    this.startNode = tile;
                    var script = tile.GetComponent<TestTile>();
                    script.x = i;
                    script.y = j;
                    script.textCoord.text = script.x.ToString() + ", " + script.y.ToString();
                    tile.transform.position = screenPos;
                }
                else if (i == this.targetX && j == this.targetY)
                {
                    var tile = GameObject.Instantiate(this.tilePrefabs[2]);
                    this.tiles[i, j] = tile;
                    this.targetNode = tile;
                    var script = tile.GetComponent<TestTile>();
                    script.x = i;
                    script.y = j;
                    script.textCoord.text = script.x.ToString() + ", " + script.y.ToString();
                    tile.transform.position = screenPos;
                }
                else if ((i >= 1 && i < 4&& j == 3)
                {
                    var tile = GameObject.Instantiate(this.tilePrefabs[0]);
                    this.tiles[i, j] = tile;
                    var script = tile.GetComponent<TestTile>();
                    script.tileType = 1;
                    script.x = i;
                    script.y = j;
                    script.textCoord.text = script.x.ToString() + ", " + script.y.ToString();
                    tile.transform.position = screenPos;
                }
                else
                {
                    var tile = GameObject.Instantiate(this.tilePrefabs[3]);
                    this.tiles[i, j] = tile;
                    var script = tile.GetComponent<TestTile>();
                    script.x = i;
                    script.y = j;
                    script.textCoord.text = script.x.ToString() + ", " + script.y.ToString();
                    tile.transform.position = screenPos;
                }
            }
        }
    }
}
 
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class TestTile : MonoBehaviour
{
    public TextMesh textCoord;
    public TextMesh textF;
    public TextMesh textG;
    public TextMesh textH;
    public GameObject Arrow;
    public int x, y;
    public int tileType;
    public Vector2 vec2;
    public int g, h, f;
    public bool closeNode;
 
    public TestTile()
    {
        this.vec2 = new Vector2(this.x, this.y);
    }
}
 
cs


'Algorithmus' 카테고리의 다른 글

[설탕 배달]06-12  (0) 2019.06.12
AStar algorithm 4방향(캐릭터 이동)  (0) 2019.05.21
[Palindrome Number]05-14  (0) 2019.05.14
[TwoSum]05-04  (0) 2019.05.04
[TwoSum]05-03  (0) 2019.05.03
:

[Palindrome Number]05-14

Algorithmus 2019. 5. 14. 17:20




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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Algorithm2
{
    public class App
    {
        public void Start()
        {
            int a;
            bool b;
 
            a = 121;
            b = this.IsPalindrome(a);
            Console.WriteLine("{0} : {1}", a, b);
 
            a = -121;
            b = this.IsPalindrome(a);
            Console.WriteLine("{0} : {1}", a, b);
 
            a = 122222222;
            b = this.IsPalindrome(a);
            Console.WriteLine("{0} : {1}", a, b);
 
            a = -143289579;
            b = this.IsPalindrome(a);
            Console.WriteLine("{0} : {1}", a, b);
 
            Console.WriteLine("======================");
 
            a = 121;
            b = this.IsPalindrome2(a);
            Console.WriteLine("{0} : {1}", a, b);
 
            a = -121;
            b = this.IsPalindrome2(a);
            Console.WriteLine("{0} : {1}", a, b);
 
            a = 122222222;
            b = this.IsPalindrome2(a);
            Console.WriteLine("{0} : {1}", a, b);
 
            a = -122125135;
            b = this.IsPalindrome2(a);
            Console.WriteLine("{0} : {1}", a, b);
 
        }
 
        //
        public bool IsPalindrome(int x)
        {
            if (x < 0)
            {
                x *= -1;
            }
 
            var charX = x.ToString();
 
            return charX[0== charX[charX.Length - 1];
        }
        
        //문자열로 바꾸지 않고 하기
        public bool IsPalindrome2(int x)
        {
            if (x < 0)
            {
                x *= -1;
            }
 
            int i = 1;
            int firstNum = 0;
 
            //첫번째 자리 구하기
            while (true)
            {
                var a = x / i;
 
                if (a == 0)
                {
                    break;
                }
                i *= 10;
                firstNum = a;
            }
 
            var lastNum = x % 10;
 
            return firstNum == lastNum;
        }
    }
}
 
cs


'Algorithmus' 카테고리의 다른 글

AStar algorithm 4방향(캐릭터 이동)  (0) 2019.05.21
AStar algorithm  (0) 2019.05.20
[TwoSum]05-04  (0) 2019.05.04
[TwoSum]05-03  (0) 2019.05.03
<선택정렬 알고리즘>수정1  (0) 2019.03.29
:

[TwoSum]05-04

Algorithmus 2019. 5. 4. 10:56

TwoSum(); // 이중포문 사용

TwoSum2(); //이중포문 사용 안 함

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Algorithm1
{
    public class App
    {
        private int[] arr = { 111927 };
 
        public App()
        {
 
        }
 
        public void Start()
        {
            //a의 각원소를 더해 9가 나오나 검사
            for (int i = 0; i < this.arr.Length; i++)
            {
                Console.WriteLine("index[{0}] : {1}", i, arr[i]);
            }
            Console.WriteLine();
 
            var arr2 = this.TwoSum(this.arr, 9);
            for (int i = 0; i < arr2.Length; i++)
            {
                Console.WriteLine("index : {0}", arr2[i]);
            }
 
            var arr3 = this.TwoSum2(this.arr, 9);
 
            for (int i = 0; i < arr3.Length; i++)
            {
                Console.WriteLine("index : {0}", arr3[i]);
            }
        }
 
        public int[] TwoSum(int[] nums, int target)
        {
            int[] returnArr = new int[2];
            for (int i = 0; i < nums.Length - 1; i++)
            {
                for (int j = 1; j < nums.Length; j++)
                {
                    if (nums[i] + nums[j] == target)
                    {
                        Console.WriteLine("index[{0}], index[{1}] = {2}", i, j, nums[i] + nums[j]);
                        returnArr[0= i;
                        returnArr[1= j;
                    }
                }
            }
            return returnArr;
        }
 
        public int[] TwoSum2(int[] nums, int target)
        {
            int[] returnAtrr = new int[2];
            int index = 0;
            for (int i = 0; i < nums.Length; i++)
            {
                Console.WriteLine("index[{0}] + index[{1}] : {2} ", index, i, nums[index] + nums[i]);
                if (nums[index] + nums[i] == target)
                {
                    returnAtrr[0= index;
                    returnAtrr[1= i;
                    break;
                }
 
                if (returnAtrr[0== 0 && returnAtrr[1== 0 && i == nums.Length-1)
                {
                    index++;
                    i = 0;
                }
            }
 
            return returnAtrr;
        }
    }
}
cs


'Algorithmus' 카테고리의 다른 글

AStar algorithm  (0) 2019.05.20
[Palindrome Number]05-14  (0) 2019.05.14
[TwoSum]05-03  (0) 2019.05.03
<선택정렬 알고리즘>수정1  (0) 2019.03.29
<선택정렬>  (0) 2019.03.29
:

[TwoSum]05-03

Algorithmus 2019. 5. 3. 10:43

TwoSum(); 이중포문 사용
TwoSum2(); 이중포문 사용 안 함

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Algorithm1
{
    public class App
    {
        private int[] arr = { 111927 };
 
        public App()
        {
 
        }
 
        public void Start()
        {
            //a의 각원소를 더해 9가 나오나 검사
            for (int i = 0; i < this.arr.Length; i++)
            {
                Console.WriteLine("index[{0}] : {1}", i, arr[i]);
            }
            Console.WriteLine();
 
            var arr2 = this.TwoSum(this.arr, 9);
            for (int i = 0; i < arr2.Length; i++)
            {
                Console.WriteLine("index : {0}", arr2[i]);
            }
 
            var arr3 = this.TwoSum2(this.arr, 9);
 
            for (int i = 0; i < arr3.Length; i++)
            {
                Console.WriteLine("index : {0}", arr3[i]);
            }
        }
 
        public int[] TwoSum(int[] nums, int target)
        {
            int[] returnArr = new int[2];
            for (int i = 0; i < nums.Length - 1; i++)
            {
                for (int j = 1; j < nums.Length; j++)
                {
                    if (nums[i] + nums[j] == target)
                    {
                        Console.WriteLine("index[{0}], index[{1}] = {2}", i, j, nums[i] + nums[j]);
                        returnArr[0= i;
                        returnArr[1= j;
                    }
                }
            }
            return returnArr;
        }
 
        public int[] TwoSum2(int[] nums, int target)
        {
            int[] returnAtrr = new int[2];
            int index = 0;
            for (int i = 0; i < nums.Length; i++)
            {
                Console.WriteLine("index[{0}] + index[{1}] : {2} ", index, i, nums[index] + nums[i]);
                if (nums[index] + nums[i] == target)
                {
                    returnAtrr[0= index;
                    returnAtrr[1= i;
                    break;
                }
 
                if (returnAtrr[0== 0 && returnAtrr[1== 0 && i == nums.Length-1)
                {
                    index++;
                    i = 0;
                }
            }
 
            return returnAtrr;
        }
    }
}
cs
[Palindrome Number]05-14  (0) 2019.05.14
[TwoSum]05-04  (0) 2019.05.04
<선택정렬 알고리즘>수정1  (0) 2019.03.29
<선택정렬>  (0) 2019.03.29
코딩테스트  (0) 2019.03.27
:
◀ PREV : [1] : [2] : NEXT ▶