您的位置:首页 > 其它

ZOJ 1091 依然是广搜哒~~

2017-07-30 11:11 316 查看
   从昨天中午开始写,到晚上柑橘差不多了提交的时候一直都是SF T_T~不过今天再看了一眼发现是visi数组写成8×8了,,所以错了!!!今天要努力啦!!!
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;

typedef struct node {
int X;
int Y;
int step;
} pos;

queue<pos> Q;
int vist[9][9];

int jump[9][2] = {{0, 0},  {-1, -2}, {-2, -1}, {-2, 1}, {-1, 2},
{1, -2}, {2, -1},  {2, 1},   {1, 2}};

int main(void) {
char x1, x2;
int y1, y2, a, b;
pos r;

pos arr_f_Q[100000];
while (cin >> x1 >> y1 >> x2 >> y2) {
int count = -1;
memset(vist, 0, sizeof(vist));
while (!Q.empty())
Q.pop();

a = x1 - 'a' + 1;
b = x2 - 'a' + 1;

r.X = a;
r.Y = y1;
r.step = 0;
Q.push(r);
while (!Q.empty()) {
count++;
arr_f_Q[count] = Q.front();
Q.pop();
if (arr_f_Q[count].X == b && arr_f_Q[count].Y == y2) {
break;
}

for (int i = 1; i <= 8; i++) {
r.X = arr_f_Q[count].X + jump[i][0];
r.Y = arr_f_Q[count].Y + jump[i][1];

if (r.X >= 1 && r.X <= 8 && r.Y >= 1 && r.Y <= 8 && !vist[r.X][r.Y]) {
vist[r.X][r.Y] = 1;
r.step = arr_f_Q[count].step + 1;
Q.push(r);
}
}
}
cout << "To get from " << x1 << y1 << " to " << x2 << y2 << " takes "
<< arr_f_Q[count].step << " knight moves." << endl;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: