Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (173.84 KB, 13 trang )
Công nghệ phần mềm
GVHD: Ts. Lê Văn Hưng
State initialState; // Khởi tạo trạng thái ban đầu
initialState.board = 0; // trạng thái ban đầu =0
for (int i=0; i < 16; ++i)
{
if (initialBoard[i] <= '9')
setValue(ref initialState.board, i, initialBoard[i] - '0');
else
setValue(ref initialState.board, i, initialBoard[i] - 'A' + 10);
}
- Tương ứng với trạng thái bắt đầu ở hình 7 ta hiển thị trạng thái bắt
đầu trên chương trình như sau:
Trang 10
Công nghệ phần mềm
GVHD: Ts. Lê Văn Hưng
Hình 8
3.3. THUẬT TOÁN IDS-A*
- Trong chương trình, thuật toán được xây dựng trên một hàm:
bool dfs_search(ref State state, int maxScore, ref Stack
{
const string digits = "0123456789ABCDEF";
if (state.score > maxScore)
return false;
if (state.board == 0x0FEDCBA987654321L)
{
result.Push('\0');
return true;
}
State newState = new State();
if ((state.emptyPos & 0x3) != 0) //co the dich trai
{
if (makeMove(ref newState, ref state, maxScore,
state.emptyPos - 1))
{
result.Push(digits[newState.lastMoved]);
if (dfs_search(ref newState, maxScore, ref result))
return true;
result.Pop();
}
}
if ((state.emptyPos & 0x3) != 3) // co the dich phai
{
if (makeMove(ref newState, ref state, maxScore,
state.emptyPos + 1))
{
result.Push(digits[newState.lastMoved]);
if (dfs_search(ref newState, maxScore, ref result))
return true;
Trang 11
Công nghệ phần mềm
GVHD: Ts. Lê Văn Hưng
result.Pop();
}
}
if ((state.emptyPos >> 2) != 0) // co the dich len
{
if (makeMove(ref newState, ref state, maxScore,
state.emptyPos - 4))
{
result.Push(digits[newState.lastMoved]);
if (dfs_search(ref newState, maxScore, ref result))
return true;
result.Pop();
}
}
if ((state.emptyPos >> 2) != 3) //co the dich xuong
{
if (makeMove(ref newState, ref state, maxScore,
state.emptyPos + 4))
{
result.Push(digits[newState.lastMoved]);
if (dfs_search(ref newState, maxScore, ref result))
return true;
result.Pop();
}
}
return false;
}
// Hàm đánh giá
if (Math.Abs((toMove & 0x3) - targetCol) +
Math.Abs((toMove >> 2) - targetRow) <
Math.Abs((lastState.emptyPos & 0x3) - targetCol) +
Math.Abs((lastState.emptyPos >> 2) - targetRow))
{
++newState.score;
}
Trang 12
Công nghệ phần mềm
GVHD: Ts. Lê Văn Hưng
public String idas_search(string initialBoard)
{
//khoi tao ban co
State initialState;
initialState.board = 0;
for (int i=0; i < 16; ++i)
{
if (initialBoard[i] <= '9')
setValue(ref initialState.board, i, initialBoard[i] '0');
else
setValue(ref initialState.board, i, initialBoard[i] 'A' + 10);
}
initialState.emptyPos = (char)initialBoard.IndexOf('0');
initialState.score = 0;
initialState.lastMoved = (char)0;
// Sau mỗi trạng thái kết quả được lưu vào stack mang tên result
Stack
int maxScore = 0;
while (!dfs_search(ref initialState, maxScore, ref result))
++maxScore;
return getContent(result);
}
- Trong thuật toán trên các bước khi thực hiện sẽ gọi tới những hàm
liên quan khác trong chương trình gồm có các hàm dịch bít, hàm kiểm tra
các trạng thái của bài toán…
4. CHƯƠNG TRÌNH
Trang 13