無題
Posted: 2010年2月10日(水) 09:34
cで迷路を解こうとしているのですがうまくいっていません。
何かいい探索プログラムはありませんか?
とりあえず今のソースをのっけます。
#include "maze.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAZE_X (40)
#define MAZE_Y (40)
static CurrentState getCurrent ( void );
static void initMaze ( int, int, CurrentState );
static CurrentState findNext ( CurrentState );
static int mazeHeight = 0; // 迷路のサイズ
static int mazeWidth = 0;
static int x, y;
static int search[MAZE_X][MAZE_Y];
static CurrentState pos = { 0, 0, DirDown, MapStart }; // 現在位置
static int a = 0; //経路探索状況
// プレーヤー情報を持つ構造体
// "蒲田"の部分のみ、変更すること
static PlayerInfo player = { "YON" , initMaze, findNext, getCurrent };
// プレーヤーの情報を返す関数
// "Kamada"の部分以外、変更しないこと
PlayerInfo* getyonInfo ( void )
{
return &player;
}
// 現在位置の情報を返す関数
// 変更しないこと
static CurrentState getCurrent ( void )
{
return pos;
}
// 初期化の関数
static void initMaze ( int col, int row, // 迷路のサイズ
CurrentState start ) // スタート位置
{
// この下の3行は、変更しないこと。
// このほかに必要なものがあれば、追加することは可。
mazeWidth = col;
mazeHeight = row;
pos = start;
x = pos.x;
y = pos.y;
}
// 次に進む場所を計算する関数
// 次に進む場所を返すようになっていれば、変更可
static CurrentState findNext ( CurrentState pos )
{
CurrentState next = pos;
switch ( next.dir ){
case DirUp : next.y--; break;
case DirDown : next.y++; break;
case DirLeft : next.x--; break;
case DirRight : next.x++; break;
}
// 次に進もうとしているセルの状態をチェックする
if ( next.status = canPassThrough ( next ) ) {
pos = next;
} else {
// 上
pos.status = pos.status;
if (search[x][y-1]== MapWall)
{
pos.dir -= DirUp;
pos.status = MapAisle;
} else {
if (search[x][y-1]== MapAisle) {
pos.dir += DirDown;
}
}
// 下
if (search[x][y+1]== MapWall)
{
pos.dir += DirDown;
pos.status = MapAisle;
} else {
if (search[x][y+1]== MapAisle) {
pos.dir -= DirUp;
}
}
// 左
if (search[x-1][y]== MapWall)
{
pos.dir -= DirLeft;
pos.status = MapAisle;
} else {
if (search[x-1][y]== MapAisle) {
pos.dir += DirRight;
}
}
// 右
if (search[x+1][y]== MapWall)
{
pos.dir += DirRight;
pos.status = MapAisle;
} else {
if (search[x+1][y]== MapAisle) {
pos.dir -= DirLeft;
}
}
}
return pos;
}
何かいい探索プログラムはありませんか?
とりあえず今のソースをのっけます。
#include "maze.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAZE_X (40)
#define MAZE_Y (40)
static CurrentState getCurrent ( void );
static void initMaze ( int, int, CurrentState );
static CurrentState findNext ( CurrentState );
static int mazeHeight = 0; // 迷路のサイズ
static int mazeWidth = 0;
static int x, y;
static int search[MAZE_X][MAZE_Y];
static CurrentState pos = { 0, 0, DirDown, MapStart }; // 現在位置
static int a = 0; //経路探索状況
// プレーヤー情報を持つ構造体
// "蒲田"の部分のみ、変更すること
static PlayerInfo player = { "YON" , initMaze, findNext, getCurrent };
// プレーヤーの情報を返す関数
// "Kamada"の部分以外、変更しないこと
PlayerInfo* getyonInfo ( void )
{
return &player;
}
// 現在位置の情報を返す関数
// 変更しないこと
static CurrentState getCurrent ( void )
{
return pos;
}
// 初期化の関数
static void initMaze ( int col, int row, // 迷路のサイズ
CurrentState start ) // スタート位置
{
// この下の3行は、変更しないこと。
// このほかに必要なものがあれば、追加することは可。
mazeWidth = col;
mazeHeight = row;
pos = start;
x = pos.x;
y = pos.y;
}
// 次に進む場所を計算する関数
// 次に進む場所を返すようになっていれば、変更可
static CurrentState findNext ( CurrentState pos )
{
CurrentState next = pos;
switch ( next.dir ){
case DirUp : next.y--; break;
case DirDown : next.y++; break;
case DirLeft : next.x--; break;
case DirRight : next.x++; break;
}
// 次に進もうとしているセルの状態をチェックする
if ( next.status = canPassThrough ( next ) ) {
pos = next;
} else {
// 上
pos.status = pos.status;
if (search[x][y-1]== MapWall)
{
pos.dir -= DirUp;
pos.status = MapAisle;
} else {
if (search[x][y-1]== MapAisle) {
pos.dir += DirDown;
}
}
// 下
if (search[x][y+1]== MapWall)
{
pos.dir += DirDown;
pos.status = MapAisle;
} else {
if (search[x][y+1]== MapAisle) {
pos.dir -= DirUp;
}
}
// 左
if (search[x-1][y]== MapWall)
{
pos.dir -= DirLeft;
pos.status = MapAisle;
} else {
if (search[x-1][y]== MapAisle) {
pos.dir += DirRight;
}
}
// 右
if (search[x+1][y]== MapWall)
{
pos.dir += DirRight;
pos.status = MapAisle;
} else {
if (search[x+1][y]== MapAisle) {
pos.dir -= DirLeft;
}
}
}
return pos;
}