写的比较复杂,跑了4秒。。
采用BFS搜索起始状态到目标状态的最短路径,其中如果有禁止状态则跳过
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <cctype>
#include <algorithm>
#include <cmath>
#include <deque>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <iomanip>
using namespace std;
///
#define INF 0xffffff7
#define maxn 10210
///
int vis[20][20][20][20];
bool flag;
int cnt, fbnum;//cnt为记录最短路径长度,fbnum为禁止状态个数
struct state{
int num1, num2, num3, num4;
void operator= (const state & t1)
{
num1 = t1.num1;
num2 = t1.num2;
num3 = t1.num3;
num4 = t1.num4;
}
};
state states[maxn];
state myState;
state iSate, iTarget;
bool finish(state my, state t)
{
if (my.num1 == t.num1 && my.num2 == t.num2 && my.num3 == t.num3 && my.num4 == t.num4)
return true;
return false;
}
void bfs()
{
int i, j;
int hp = 0, tp = 1, lc = 1;
//如果到达目标,返回
state temp = myState;
queue<state> qu;
cnt = 0;
qu.push(temp);
vis[temp.num1][temp.num2][temp.num3][temp.num4] = 1;
while (!qu.empty())
{
temp = qu.front();
qu.pop();
hp++;
if (finish(temp, iTarget))
{
flag = true;
return;
}
state t;
//8个不同的状态
//状态1
bool putin = true;
t = temp;
t.num1 = (t.num1 - 1 + 10) % 10;
for (i = 0; i < fbnum; i++)
if (t.num1 == states[i].num1 && t.num2 == states[i].num2 && t.num3 == states[i].num3 && t.num4 == states[i].num4)
{
putin = false;
break;
}
if (putin)
{
if (vis[t.num1][t.num2][t.num3][t.num4] != 1)
{
qu.push(t);
vis[t.num1][t.num2][t.num3][t.num4] = 1;
tp++;
}
}
//状态2
putin = true;
t = temp;
t.num1 = (t.num1 + 1) % 10;
for (i = 0; i < fbnum; i++)
if (t.num1 == states[i].num1 && t.num2 == states[i].num2 && t.num3 == states[i].num3 && t.num4 == states[i].num4)
{
putin = false;
break;
}
if (putin)
{
if (vis[t.num1][t.num2][t.num3][t.num4] != 1)
{
qu.push(t);
vis[t.num1][t.num2][t.num3][t.num4] = 1;
tp++;
}
}
//状态3
putin = true;
t = temp;
t.num2 = (t.num2 - 1 + 10) % 10;
for (i = 0; i < fbnum; i++)
if (t.num1 == states[i].num1 && t.num2 == states[i].num2 && t.num3 == states[i].num3 && t.num4 == states[i].num4)
{
putin = false;
break;
}
if (putin)
{
if (vis[t.num1][t.num2][t.num3][t.num4] != 1)
{
qu.push(t);
vis[t.num1][t.num2][t.num3][t.num4] = 1;
tp++;
}
}
//状态4
putin = true;
t = temp;
t.num2 = (t.num2 + 1) % 10;
for (i = 0; i < fbnum; i++)
if (t.num1 == states[i].num1 && t.num2 == states[i].num2 && t.num3 == states[i].num3 && t.num4 == states[i].num4)
{
putin = false;
break;
}
if (putin)
{
if (vis[t.num1][t.num2][t.num3][t.num4] != 1)
{
qu.push(t);
vis[t.num1][t.num2][t.num3][t.num4] = 1;
tp++;
}
}
//状态5
putin = true;
t = temp;
t.num3 = (t.num3 - 1 + 10) % 10;
for (i = 0; i < fbnum; i++)
if (t.num1 == states[i].num1 && t.num2 == states[i].num2 && t.num3 == states[i].num3 && t.num4 == states[i].num4)
{
putin = false;
break;
}
if (putin)
{
if (vis[t.num1][t.num2][t.num3][t.num4] != 1)
{
qu.push(t);
vis[t.num1][t.num2][t.num3][t.num4] = 1;
tp++;
}
}
//状态6
putin = true;
t = temp;
t.num3 = (t.num3 + 1) % 10;
for (i = 0; i < fbnum; i++)
if (t.num1 == states[i].num1 && t.num2 == states[i].num2 && t.num3 == states[i].num3 && t.num4 == states[i].num4)
{
putin = false;
break;
}
if (putin)
{
if (vis[t.num1][t.num2][t.num3][t.num4] != 1)
{
qu.push(t);
vis[t.num1][t.num2][t.num3][t.num4] = 1;
tp++;
}
}
//状态7
putin = true;
t = temp;
t.num4 = (t.num4 - 1 + 10) % 10;
for (i = 0; i < fbnum; i++)
if (t.num1 == states[i].num1 && t.num2 == states[i].num2 && t.num3 == states[i].num3 && t.num4 == states[i].num4)
{
putin = false;
break;
}
if (putin)
{
if (vis[t.num1][t.num2][t.num3][t.num4] != 1)
{
qu.push(t);
vis[t.num1][t.num2][t.num3][t.num4] = 1;
tp++;
}
}
//状态8
putin = true;
t = temp;
t.num4 = (t.num4 + 1) % 10;
for (i = 0; i < fbnum; i++)
if (t.num1 == states[i].num1 && t.num2 == states[i].num2 && t.num3 == states[i].num3 && t.num4 == states[i].num4)
{
putin = false;
break;
}
if (putin)
{
if (vis[t.num1][t.num2][t.num3][t.num4] != 1)
{
qu.push(t);
vis[t.num1][t.num2][t.num3][t.num4] = 1;
tp++;
}
}
//
if (hp == lc)
{
cnt++;
lc = tp;
}
}
}
int main()
{
///
int i, j, k;
int ncases;
cin >> ncases;
while (ncases--)
{
memset(vis, -1, sizeof(vis));
cnt = INF;
flag = false;
//读入初始状态和目标状态
cin >> iSate.num1 >> iSate.num2 >> iSate.num3 >> iSate.num4;
cin >> iTarget.num1 >> iTarget.num2 >> iTarget.num3 >> iTarget.num4;
//禁止状态个数
cin >> fbnum;
//读入禁止状态
for (i = 0; i < fbnum; i++)
{
cin >> states[i].num1 >> states[i].num2 >> states[i].num3 >> states[i].num4;
}
myState = iSate;
bfs();
if (flag)
cout << cnt << endl;
else
cout << -1 << endl;
}
///
return 0;
}