#include <stdio.h> #include <queue> #pragma warning(disable:4996) using namespace std; typedef struct _block { int x; int y; int idx; }block; queue<block> myQueue; int a, b, min, cnt, map[103][103]; int x[8] = { -2, -1, 1, 2, 2, 1, -1, -2 }; int y[8] = { -1, -2, -2, -1, 1, 2, 2, 1 }; int main() { FILE *fin = fopen("input.txt", "rt"), *fout = fopen("output.txt", "wt"); block start, end; int flag = 0; // input fscanf(fin, "%d %d", &a, &b); fscanf(fin, "%d %d %d %d", &start.x, &start.y, &end.x, &end.y); // init for (int i = 1; i <= a; i++) { for (int j = 1; j <= b; j++) map[i][j] = 1; // 방문 안 한곳 } // algo start.idx = 0; myQueue.push(start); map[start.y][start.x] = 2; // 갔던 길은 2로 표시 while (!myQueue.empty()) { block tmp_block_out; block tmp_block_in; tmp_block_out = myQueue.front(); myQueue.pop(); //printf("pop되는 front는 (%d, %d)\n", tmp_block_out.x, tmp_block_out.y); for (int i = 0; i < 8; i++) { // 벽을 만나면... int posx = tmp_block_out.x+x[i]; int posy = tmp_block_out.y + y[i]; if (!(posx>0 && posx <= a && posy > 0 && posy <= b)) // !!중요 continue; if (map[posx][posy]!=1) continue; tmp_block_in.x = posx; tmp_block_in.y = posy; tmp_block_in.idx = tmp_block_out.idx + 1; // end를 만났을때 if (posx== end.x && posy == end.y) { fprintf(fout, "%d\n", tmp_block_in.idx); return 0; } myQueue.push(tmp_block_in); map[posx][posy] = 2; // 방문 한 곳 } } } |