题目链接:点击查看
题目大意:给出斗地主的规则,以及最终分数,求出最少需要进行几局游戏
题目分析:bfs爆搜即可,因为数据范围比较小,我一开始没多想,直接18种情况全部打上去,交了一发T掉了。。bfs被卡T?于是就剪枝,剪了半天还是T,最后拿出秘密武器,用无序map+600进制维护了vis数组,就A了,因为不想出现负数,而且a,b,c的数据范围都比较小,直接都加上300变成正数然后按照600进制处理即可,没什么细节。。裸题一个。
不过虽然过了,但还是跑了800多ms,看到有的人交了30多ms的就有点在意,去网上搜了搜,发现,原来只需要维护一个二维数组即可,因为题目中保证了a+b+c恒等于0,所以枚举a和b的情况,c的情况就已经知道了,所以只需要对a和b进行操作就好了,懒得再写代码了,下次看到这种题目应该会留个印象了,挂代码吧,主要是学到了一波如何用二维思想实现三维的题目吧(虽然我还是用三维莽过去的。。)
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cctype>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const int N=1e6+100;
int n,a,b,c;
const int bb[][3]=
{
//win
2,-1,-1,//A
4,-2,-2,
6,-3,-3,
-1,2,-1,//B
-2,4,-2,
-3,6,-3,
-1,-1,2,//C
-2,-2,4,
-3,-3,6,
//lose
-2,1,1,//A
-4,2,2,
-6,3,3,
1,-2,1,//B
2,-4,2,
3,-6,3,
1,1,-2,//C
2,2,-4,
3,3,-6
};
unordered_map<int,int>vis;
struct Node
{
int step;
int a,b,c;
};
int getnum(int a,int b,int c)
{
return a*600*600+b*600+c;
}
int bfs()
{
a+=300;
b+=300;
c+=300;
queue<Node>q;
vis.clear();
Node temp;
temp.a=300;
temp.b=300;
temp.c=300;
temp.step=0;
q.push(temp);
while(!q.empty())
{
Node cur=q.front();
q.pop();
// cout<<cur.step<<' '<<cur.a<<' '<<cur.b<<' '<<cur.c<<endl;
// cout<<cur.step<<endl;
if(cur.step>n)
break;
for(int i=0;i<18;i++)
{
Node next;
next.a=cur.a+bb[i][0];
next.b=cur.b+bb[i][1];
next.c=cur.c+bb[i][2];
if(vis[getnum(next.a,next.b,next.c)])
continue;
if(next.a>600||next.b>600||next.c>600)
continue;
if(next.a<0||next.b<0||next.c<0)
continue;
vis[getnum(next.a,next.b,next.c)]=1;
next.step=cur.step+1;
q.push(next);
if(next.a==a&&next.b==b&&next.c==c)
return next.step;
}
}
return -1;
}
int main()
{
scanf("%d%d%d%d",&n,&a,&b,&c);
printf("%d\n",bfs());
return 0;
}