思路:同为bfs求解最少步数,问题在于状态如何更好地枚举。
无法开256^3的数组,利用题中2*2格中必有*的条件,改矩阵图为存边的图,点则变为144个,可存数组。
无法开256^3的数组,利用题中2*2格中必有*的条件,改矩阵图为存边的图,点则变为144个,可存数组。
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
#include <utility>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cctype>
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define LL long long
#define mod 1000000007
#define INF 1000000007
#define eps 1e-5
#define PI 3.1415926535898
using namespace std;
//-------------------------CHC------------------------------//
const int maxw = 16 + 5;
const int maxn = 144 + 5;
const int dx[] = { 1, -1, 0, 0, 0 };
const int dy[] = { 0, 0, 1, -1, 0 };
char G[maxw][maxw];
int newG[maxn][5], degree[maxn];
int w, h, n;
int s[3], t[3];
struct Node {
int a, b, c;
Node(int a, int b, int c): a(a), b(b), c(c) { }
};
int d[maxn][maxn][maxn];
bool conflict(int a, int b, int na, int nb) { return na == nb || (a == nb) && (b == na); }
int bfs() {
CLEAR(d, -1);
queue<Node> q;
q.push(Node(s[0], s[1], s[2]));
d[s[0]][s[1]][s[2]] = 0;
while (q.size()) {
Node u = q.front(); q.pop();
if (u.a == t[0] && u.b == t