第一次做双向广搜,这花的时间真是够久的。
看别人博客,在知道要把棋子排序之后思路就很清晰了,但是代码实现不了…
原因是我把一对中间变量放到循环外面了还看不出来!
而且在代码只相差变量名字的时候,复制完了总忘记改名字!
细心啊,细心啊!不耐心一点好好地写代码才会浪费更多的时间!!!
这个悲惨的故事教会我,能直接给目标变量做修改就别通过中间变量来实现。
此题还让我还学会了减小空间可以用char类型来存储标记。
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1401
代码如下:
#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
#include<set>
using namespace std;
typedef pair<int, int> PII;
const int dr[] = { -1,1,0,0 };
const int dc[] = { 0,0,-1,1 };
char visited[8][8][8][8][8][8][8][8];
set<PII>ss;
bool check(int x, int y) {
if (x < 0 || x > 7 || y < 0 || y > 7) return false;
return true;
}
struct Cnode {
int x, y;
};
struct node {
Cnode p[4];
int step;
};
bool cmp(const Cnode& p1, const Cnode& p2) {
if (p1.x != p2.x)return p1.x < p2.x;
return p1.y < p2.y;
}
void push(const Cnode p[4], char tip) {
visited[p[0].x][p[0].y][p[1].x][p[1].y][p[2].x][p[2].y][p[3].x][p[3].y] = tip;
}
char vised(const Cnode p[4]) {
return visited[p[0].x][p[0].y][p[1].x][p[1].y][p[2].x][p[2].y][p[3].x][p[3].y];
}
int main(void) {
node head, front;
while (scanf("%d%d", &head.p[0].x, &head.p[0].y) == 2) {
memset(visited, 0, sizeof(visited));
for (int i = 1; i < 4; i++) scanf("%d%d", &head.p[i].x, &head.p[i].y);
for (int i = 0; i < 4; i++) scanf("%d%d", &front.p[i].x, &front.p[i].y);
for (int i = 0; i < 4; i++) {
head.p[i].x--; head.p[i].y--;
front.p[i].x--; front.p[i].y--;
}
sort(head.p, head.p + 4, cmp);
sort(front.p, front.p + 4, cmp);
push(head.p, '1');
push(front.p, '2');
head.step = 0;
front.step = 0;
queue<node>q, t;
q.push(head);
t.push(front);
int flag = 0;
while (!q.empty() || !t.empty()) {
if (!q.empty()) {
head = q.front(); q.pop();
if (head.step >= 4)continue;
if (flag || vised(head.p) == '2') {
flag = 1; break;
}
ss.clear();
for (int i = 0; i < 4; i++) {
ss.insert(PII(head.p[i].x, head.p[i].y));
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
int row = head.p[i].x;//这里的row和col我直接放
int col = head.p[i].y;//在外层循环下面没看出来!
row += dr[j];
col += dc[j];
if (!check(row, col))continue;
if (ss.count(PII(row, col))) {
row += dr[j];
col += dc[j];
if (!check(row, col))continue;
if (ss.count(PII(row, col)))continue;
}
node next = head;
next.step++;
next.p[i].x = row;
next.p[i].y = col;
sort(next.p, next.p + 4, cmp);
if (flag || vised(next.p) == '2') {
flag = 1; break;
}
else if (vised(next.p) == '1')continue;
push(next.p, '1');
q.push(next);
}
}
}
if (!t.empty()) {
front = t.front(); t.pop();
if (front.step >= 4)continue;
if (flag || vised(front.p) == '1') {
flag = 1; break;
}
ss.clear();
for (int i = 0; i < 4; i++) {
ss.insert(PII(front.p[i].x, front.p[i].y));
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
int row = front.p[i].x;
int col = front.p[i].y;
row += dr[j];
col += dc[j];
if (!check(row, col))continue;
if (ss.count(PII(row, col))) {
row += dr[j];
col += dc[j];
if (!check(row, col))continue;
if (ss.count(PII(row, col)))continue;
}
node next = front;
next.step++;
next.p[i].x = row;
next.p[i].y = col;
sort(next.p, next.p + 4, cmp);
if (flag || vised(next.p) == '1') {
flag = 1; break;
}
else if (vised(next.p) == '2')continue;
push(next.p, '2');
t.push(next);
}
}
}
}
if (flag)printf("YES\n");
else printf("NO\n");
}
return 0;
}