<commit_before>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <stack>
#include <algorithm>
using namespace std;

const int Max = 501;

const char CH_EXTEND = ',';
const char CH_CLOSED = '.';
const char CH_PATH = '*';

int maze_col;
int maze_row;
char maze_map[Max][Max];

bool closed_set[Max][Max];
bool extend_set[Max][Max];
// preceding direction
int dir_pre[Max][Max];

const int DIR_NUM = 8;
const int DIR_X[] = {-1, 1,  0, 0, -1, -1, 1, 1};
const int DIR_Y[] = { 0, 0, -1, 1,  1, -1, 1,-1};

struct Position {
	int x, y;
	int step;
	Position() {}
	Position(int a, int b):x(a), y(b), step(0) {}
	Position(int a, int b, int c):x(a), y(b), step(c) {}
	bool operator == (const Position &a) const {
		return (x == a.x && y == a.y);
	}
};
Position pos_start, pos_goal;

void DisplayMaze()
{
	for (int i = 0; i < maze_row; ++i) {
		for (int j = 0; j < maze_col; ++j) {
			if (closed_set[i][j] && maze_map[i][j] == ' ') {
				printf("%c", CH_CLOSED);
			} else if (extend_set[i][j] && maze_map[i][j] == ' ') {
				printf("%c", CH_EXTEND);
			} else {
				printf("%c", maze_map[i][j]);
			}
		}
		if (i == 0) {
			printf("\t closed node sign: %c ", CH_CLOSED);
		} else if (i == 1) {
			printf("\t extend node sign: %c ", CH_EXTEND);
		} else if (i == 2) {
			printf("\t   path node sign: %c ", CH_PATH);
		}
		puts("");
	}
	puts("");
}

void Delay()
{
	int M = 1, N = 10000;
	for (int i = 0; i < M; ++i)
		for (int j = 0; j < N; ++j);
}

void Dfs_prepare()
{
	memset(closed_set, false, sizeof(closed_set));
}

int Dfs(Position top)
{
    if (top == pos_goal) return top.step;
    closed_set[top.x][top.y] = true;
    //system("cls"); DisplayMaze(); Delay(); //system("pause");
    for (int i = 0; i <= DIR_NUM; ++i) {
        int tx = top.x + DIR_X[i];
        int ty = top.y + DIR_Y[i];
        if (maze_map[tx][ty] == 'o') continue;
        if (extend_set[tx][ty]) continue;
        extend_set[tx][ty] = true;
        dir_pre[tx][ty] = i;
        int ret = Dfs(Position(tx, ty, top.step + 1));
        if (ret >= 0) return ret;
        // backtracking
        //extend_set[tx][ty] = false;
    }
    return -1;
}

int main ()
{
	//freopen("map.txt","r",stdin); freopen("ans.txt","w",stdout);

	while (scanf("%d,%d", &maze_col, &maze_row) != EOF) {
		memset(maze_map, 0, sizeof(maze_map));
		// read in maze map
		for (int i = 0; i < maze_row; ++i) {
			getchar();
			for (int j = 0; j < maze_col; ++j) {
				scanf("%c", &maze_map[i][j]);
				if (maze_map[i][j] == 's') {
					pos_start.x = i;
					pos_start.y = j;
				} else if (maze_map[i][j] == 'e') {
					pos_goal.x = i;
					pos_goal.y = j;
				}
			}
		}

		clock_t st = clock();
		Dfs_prepare();
		// search
		int minstep = Dfs(pos_start);
		clock_t et = clock();
		printf("search cost time: %lfs\n", (double)(et - st)/CLOCKS_PER_SEC);

		if (minstep == -1) {
			puts("This maze have no solution");
			continue;
		} else {
			printf("The solution of minimal steps is %d : \n", minstep);
			Position tmp = pos_goal;
			// get path
			while (true) {
				int i = dir_pre[tmp.x][tmp.y];
				tmp.x -= DIR_X[i];
				tmp.y -= DIR_Y[i];
				if (tmp == pos_start) break;
				maze_map[tmp.x][tmp.y] = CH_PATH;
			}
			DisplayMaze();
		}
	}
	return 0 ;
}
<commit_msg>bug fixed.<commit_after>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <stack>
#include <algorithm>
using namespace std;

const int Max = 501;

const char CH_EXTEND = ',';
const char CH_CLOSED = '.';
const char CH_PATH = '*';

int maze_col;
int maze_row;
char maze_map[Max][Max];

bool closed_set[Max][Max];
//bool extend_set[Max][Max];
// preceding direction
int dir_pre[Max][Max];

const int DIR_NUM = 8;
const int DIR_X[] = {-1, 1,  0, 0, -1, -1, 1, 1};
const int DIR_Y[] = { 0, 0, -1, 1,  1, -1, 1,-1};

struct Position {
	int x, y;
	int step;
	Position():x(0), y(0), step(0) {}
	Position(int a, int b):x(a), y(b), step(0) {}
	Position(int a, int b, int c):x(a), y(b), step(c) {}
	bool operator == (const Position &a) const {
		return (x == a.x && y == a.y);
	}
};
Position pos_start, pos_goal;

void DisplayMaze()
{
	for (int i = 0; i < maze_row; ++i) {
		for (int j = 0; j < maze_col; ++j) {
			if (closed_set[i][j] && maze_map[i][j] == ' ') {
				printf("%c", CH_CLOSED);
//			} else if (extend_set[i][j] && maze_map[i][j] == ' ') {
//				printf("%c", CH_EXTEND);
			} else {
				printf("%c", maze_map[i][j]);
			}
		}
		if (i == 0) {
			printf("\t closed node sign: %c ", CH_CLOSED);
		} else if (i == 1) {
			printf("\t extend node sign: %c ", CH_EXTEND);
		} else if (i == 2) {
			printf("\t   path node sign: %c ", CH_PATH);
		}
		puts("");
	}
	puts("");
}

void Delay()
{
	int M = 1, N = 10000;
	for (int i = 0; i < M; ++i)
		for (int j = 0; j < N; ++j);
}

void Dfs_prepare()
{
	memset(closed_set, false, sizeof(closed_set));
}

int Dfs(Position top)
{
    if (top == pos_goal) return top.step;
    closed_set[top.x][top.y] = true;
    system("cls"); DisplayMaze(); Delay(); //system("pause");
    for (int i = 0; i < DIR_NUM; ++i) {
        int tx = top.x + DIR_X[i];
        int ty = top.y + DIR_Y[i];
        if (maze_map[tx][ty] == 'o') continue;
        if (closed_set[tx][ty]) continue;
        dir_pre[tx][ty] = i;
        int ret = Dfs(Position(tx, ty, top.step + 1));
        if (ret >= 0) return ret;
        // backtracking
        //closed_set[tx][ty] = false;
    }
    return -1;
}

int main ()
{
	//freopen("map.txt","r",stdin); freopen("ans.txt","w",stdout);

	while (scanf("%d,%d", &maze_col, &maze_row) != EOF) {
		memset(maze_map, 0, sizeof(maze_map));
		// read in maze map
		for (int i = 0; i < maze_row; ++i) {
			getchar();
			for (int j = 0; j < maze_col; ++j) {
				scanf("%c", &maze_map[i][j]);
				if (maze_map[i][j] == 's') {
					pos_start.x = i;
					pos_start.y = j;
				} else if (maze_map[i][j] == 'e') {
					pos_goal.x = i;
					pos_goal.y = j;
				}
			}
		}

		clock_t st = clock();
		Dfs_prepare();
		// search
		int minstep = Dfs(pos_start);
		clock_t et = clock();
		printf("search cost time: %lfs\n", (double)(et - st)/CLOCKS_PER_SEC);

		if (minstep == -1) {
			puts("This maze have no solution");
			continue;
		} else {
			printf("The solution of minimal steps is %d : \n", minstep);
			Position tmp = pos_goal;
			// get path
			while (true) {
				int i = dir_pre[tmp.x][tmp.y];
				tmp.x -= DIR_X[i];
				tmp.y -= DIR_Y[i];
				if (tmp == pos_start) break;
				maze_map[tmp.x][tmp.y] = CH_PATH;
			}
			DisplayMaze();
		}
	}
	return 0 ;
}

{'fraction_non_alphanumeric': 0.20938794293603313, 'fraction_numerical': 0.013652400674950146, 'mean_word_length': 2.8262910798122065, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6786223', 'n_tokens_mistral': 3091, 'n_tokens_neox': 2770, 'n_words': 713}