-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNeed.cs
77 lines (66 loc) · 1.91 KB
/
Need.cs
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 N_Puzzle_Game
{
public class Need
{
public int[] dy = { 1, 0, -1, 0 };
public int[] dx = { 0, -1, 0, 1 };
public int N { set; get; }
public int[,] goal, initial_state;
public int[,] get_destination()
{
int[,] c_state = new int[N, N];
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
c_state[i, j] = (i * N + j + 1) % (N * N);
return c_state;
}
public void copy(int[,] c_state, int[,] state)
{
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
c_state[i, j] = state[i, j];
}
public bool is_goal(int[,] state)
{
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
if (state[i, j] != goal[i, j])
return false;
return true;
}
public string to_string(int[,] state)
{
string ans = "";
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
ans += (char)(48 + state[i, j]);
return ans;
}
public int get_cost(int[,] state)
{
int cost = 0;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
if (state[i, j] != goal[i, j])
cost++;
return cost;
}
public bool is_safe(int x, int y)
{
return x >= 0 && x < N && y >= 0 && y < N;
}
public void set_goal(int[,] c_state)
{
goal = c_state;
}
public void set_state(int[,] c_state)
{
initial_state = c_state;
}
}
}