正解
做法
(详细解答在后面。)
#include<bits/stdc++.h>
using namespace std;
int n,a,b,c,d;
bool vis[1000][1000][2];
int ans=1;
int vis2[1000][1000];
struct N{
int x,y,next;
};
void bfs()
{
pair<int,int> d1[8]={{a,b},{-a,b},{-a,-b},{a,-b},{b,a},{b,-a},{-b,a},{-b,-a}};
pair<int,int> d2[8]={{c,d},{-c,d},{-c,-d},{c,-d},{d,c},{d,-c},{-d,c},{-d,-c}};
queue<N> q;
q.push({1,1,0});
q.push({1,1,1});
vis[1][1][0]=true;
vis[1][1][1]=true;
while(!q.empty())
{
N p=q.front();
q.pop();
if(p.next==1)
{
for(int i=0;i<8;i++)
{
int nx=p.x+d1[i].first,ny=p.y+d1[i].second;
if(nx>=1&&nx<=n&&ny>=1&&ny<=n&&!vis[nx][ny][0])
{
vis[nx][ny][0]=true;
q.push({nx,ny,0});
if(vis2[nx][ny]==0)
{
vis2[nx][ny]=true;
ans++;
}
}
}
}
else
{
for(int i=0;i<8;i++)
{
int nx=p.x+d2[i].first,ny=p.y+d2[i].second;
if(nx>=1&&nx<=n&&ny>=1&&ny<=n&&!vis[nx][ny][1])
{
vis[nx][ny][1]=true;
q.push({nx,ny,1});
if(vis2[nx][ny]==0)
{
vis2[nx][ny]=true;
ans++;
}
}
}
}
}
cout<<ans<<endl;
}
int main()
{
cin>>n>>a>>b>>c>>d;
bfs();
return 0;
}详解:
的格子 初始在 (代码中是索引从1到)
有两种方式
从当前瓷砖移动到一个距离当前瓷砖沿一个轴方向a格、另一轴方向b格的瓷砖。
从当前瓷砖移动到一个距离当前瓷砖沿一个轴方向c格、另一轴方向d格的瓷砖。
必须交替着来的, 开始选哪一种,两种都可以。
也就说明了只有两种情况:1,2,1,2,1,2,1,2… 和2,1,2,1,2,1,2,1。
结构体中用一个next变量。来进入下一种方案是哪个。
最后暴力搜索即可。